fix: migrate from data-scramjet to scramjet-data

This commit is contained in:
Percs 2024-11-10 13:37:56 -06:00
parent 21a112bd45
commit 1b50dda5c5
3 changed files with 14 additions and 14 deletions

View file

@ -52,8 +52,8 @@ export default function (client: ScramjetClient, self: typeof window) {
}, },
has(target, prop) { has(target, prop) {
if (typeof prop === "symbol") return Reflect.has(target, prop); if (typeof prop === "symbol") return Reflect.has(target, prop);
if (prop.startsWith("data-scramjet-")) return false; if (prop.startsWith("scramjet-data-")) return false;
if (map[prop]?.name?.startsWith("data-scramjet-")) return false; if (map[prop]?.name?.startsWith("scramjet-data-")) return false;
return Reflect.has(target, prop); return Reflect.has(target, prop);
}, },

View file

@ -134,7 +134,7 @@ export default function (client: ScramjetClient, self: typeof window) {
if (ruleList) { if (ruleList) {
ctx.args[1] = ruleList.fn(value, client.meta, client.cookieStore); ctx.args[1] = ruleList.fn(value, client.meta, client.cookieStore);
ctx.fn.call(ctx.this, `data-scramjet-${ctx.args[0]}`, value); ctx.fn.call(ctx.this, `scramjet-data-${ctx.args[0]}`, value);
} }
}, },
}); });
@ -155,7 +155,7 @@ export default function (client: ScramjetClient, self: typeof window) {
ctx.args[2] = ruleList.fn(value, client.meta, client.cookieStore); ctx.args[2] = ruleList.fn(value, client.meta, client.cookieStore);
nativeSetAttribute.call( nativeSetAttribute.call(
ctx.this, ctx.this,
`data-scramjet-${ctx.args[1]}`, `scramjet-data-${ctx.args[1]}`,
value value
); );
} }
@ -166,8 +166,8 @@ export default function (client: ScramjetClient, self: typeof window) {
apply(ctx) { apply(ctx) {
const [name] = ctx.args; const [name] = ctx.args;
if (ctx.fn.call(ctx.this, `data-scramjet-${name}`)) { if (ctx.fn.call(ctx.this, `scramjet-data-${name}`)) {
ctx.return(ctx.fn.call(ctx.this, `data-scramjet-${name}`)); ctx.return(ctx.fn.call(ctx.this, `scramjet-data-${name}`));
} }
}, },
}); });
@ -193,7 +193,7 @@ export default function (client: ScramjetClient, self: typeof window) {
if (ctx.this instanceof self.HTMLScriptElement) { if (ctx.this instanceof self.HTMLScriptElement) {
const scriptSource = client.natives[ const scriptSource = client.natives[
"Element.prototype.getAttribute" "Element.prototype.getAttribute"
].call(ctx.this, "data-scramjet-script-source-src"); ].call(ctx.this, "scramjet-data-script-source-src");
if (scriptSource) { if (scriptSource) {
return atob(scriptSource); return atob(scriptSource);

View file

@ -78,14 +78,14 @@ export function unrewriteHtml(html: string) {
function traverse(node: ChildNode) { function traverse(node: ChildNode) {
if ("attribs" in node) { if ("attribs" in node) {
for (const key in node.attribs) { for (const key in node.attribs) {
if (key == "data-scramjet-script-source-src") { if (key == "scramjet-data-script-source-src") {
if (node.children[0] && "data" in node.children[0]) if (node.children[0] && "data" in node.children[0])
node.children[0].data = atob(node.attribs[key]); node.children[0].data = atob(node.attribs[key]);
continue; continue;
} }
if (key.startsWith("data-scramjet-")) { if (key.startsWith("scramjet-data-")) {
node.attribs["data-scramjet-".length] = node.attribs[key]; node.attribs["scramjet-data-".length] = node.attribs[key];
delete node.attribs[key]; delete node.attribs[key];
} }
} }
@ -216,14 +216,14 @@ function traverseParsedHtml(
else { else {
node.attribs[attr] = v; node.attribs[attr] = v;
} }
node.attribs[`data-scramjet-${attr}`] = value; node.attribs[`scramjet-data-${attr}`] = value;
} }
} }
} }
} }
for (const [attr, value] of Object.entries(node.attribs)) { for (const [attr, value] of Object.entries(node.attribs)) {
if (eventAttributes.includes(attr)) { if (eventAttributes.includes(attr)) {
node.attribs[`data-scramjet-${attr}`] = attr; node.attribs[`scramjet-data-${attr}`] = attr;
node.attribs[attr] = rewriteJs( node.attribs[attr] = rewriteJs(
value as string, value as string,
`(inline ${attr} on element)`, `(inline ${attr} on element)`,
@ -244,8 +244,8 @@ function traverseParsedHtml(
node.children[0] !== undefined node.children[0] !== undefined
) { ) {
let js = node.children[0].data; let js = node.children[0].data;
// node.attribs[`data-scramjet-script-source-src`] = btoa(js); // node.attribs[`scramjet-data-script-source-src`] = btoa(js);
node.attribs["data-scramjet-script-source-src"] = bytesToBase64( node.attribs["scramjet-data-script-source-src"] = bytesToBase64(
new TextEncoder().encode(js) new TextEncoder().encode(js)
); );
const htmlcomment = /<!--[\s\S]*?-->/g; const htmlcomment = /<!--[\s\S]*?-->/g;