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) {
if (typeof prop === "symbol") return Reflect.has(target, prop);
if (prop.startsWith("data-scramjet-")) return false;
if (map[prop]?.name?.startsWith("data-scramjet-")) return false;
if (prop.startsWith("scramjet-data-")) return false;
if (map[prop]?.name?.startsWith("scramjet-data-")) return false;
return Reflect.has(target, prop);
},

View file

@ -134,7 +134,7 @@ export default function (client: ScramjetClient, self: typeof window) {
if (ruleList) {
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);
nativeSetAttribute.call(
ctx.this,
`data-scramjet-${ctx.args[1]}`,
`scramjet-data-${ctx.args[1]}`,
value
);
}
@ -166,8 +166,8 @@ export default function (client: ScramjetClient, self: typeof window) {
apply(ctx) {
const [name] = ctx.args;
if (ctx.fn.call(ctx.this, `data-scramjet-${name}`)) {
ctx.return(ctx.fn.call(ctx.this, `data-scramjet-${name}`));
if (ctx.fn.call(ctx.this, `scramjet-data-${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) {
const scriptSource = client.natives[
"Element.prototype.getAttribute"
].call(ctx.this, "data-scramjet-script-source-src");
].call(ctx.this, "scramjet-data-script-source-src");
if (scriptSource) {
return atob(scriptSource);

View file

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