fix triple rewrite in style

This commit is contained in:
velzie 2024-10-11 15:46:22 -04:00
parent 257756f998
commit 5ca5dd1f78
2 changed files with 42 additions and 43 deletions

View file

@ -12,7 +12,6 @@ const cssProperties = [
"border-image-source", "border-image-source",
"cursor", "cursor",
]; ];
// const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"];
export default function (client: ScramjetClient) { export default function (client: ScramjetClient) {
client.Proxy("CSSStyleDeclaration.prototype.setProperty", { client.Proxy("CSSStyleDeclaration.prototype.setProperty", {
@ -40,4 +39,39 @@ export default function (client: ScramjetClient) {
return unrewriteCss(ctx.get()); return unrewriteCss(ctx.get());
}, },
}); });
client.Trap("HTMLElement.prototype.style", {
get(ctx) {
// unfortunate and dumb hack. we have to trap every property of this
// since the prototype chain is fucked
const style = ctx.get() as CSSStyleDeclaration;
return new Proxy(style, {
get(t, p) {
const v = Reflect.get(t, p);
if (typeof v === "function") {
return new Proxy(v, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, style, argArray);
},
});
}
return unrewriteCss(v);
},
set(t, p, v) {
if (p == "cssText" || v == "" || typeof v !== "string") {
return Reflect.set(t, p, v);
}
return Reflect.set(t, p, rewriteCss(v, client.meta));
},
});
},
set(ctx, v: string) {
// this will actually run the trap for cssText. don't rewrite it here
ctx.set(v);
},
});
} }

View file

@ -52,7 +52,7 @@ export default function (client: ScramjetClient, self: typeof window) {
for (const element of attrObject[attr]) { for (const element of attrObject[attr]) {
const descriptor = nativeGetOwnPropertyDescriptor( const descriptor = nativeGetOwnPropertyDescriptor(
element.prototype, element.prototype,
attr attr,
); );
Object.defineProperty(element.prototype, attr, { Object.defineProperty(element.prototype, attr, {
get() { get() {
@ -80,7 +80,7 @@ export default function (client: ScramjetClient, self: typeof window) {
base: new URL(client.url.origin), base: new URL(client.url.origin),
origin: new URL(client.url.origin), origin: new URL(client.url.origin),
} as URLMeta, } as URLMeta,
true true,
); );
} else if (["srcset", "imagesrcset"].includes(attr)) { } else if (["srcset", "imagesrcset"].includes(attr)) {
value = rewriteSrcset(value, client.meta); value = rewriteSrcset(value, client.meta);
@ -165,41 +165,6 @@ export default function (client: ScramjetClient, self: typeof window) {
}, },
}); });
client.Trap("HTMLElement.prototype.style", {
get(ctx) {
// unfortunate and dumb hack. we have to trap every property of this
// since the prototype chain is fucked
const style = ctx.get() as CSSStyleDeclaration;
return new Proxy(style, {
get(t, p) {
const v = Reflect.get(t, p);
if (typeof v === "function") {
return new Proxy(v, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, style, argArray);
},
});
}
// todo properly unrewrite whatever
return v;
},
set(t, p, v) {
if (v == "" || typeof v !== "string") {
return Reflect.set(t, p, v);
}
return Reflect.set(t, p, rewriteCss(v, client.meta));
},
});
},
set(ctx, v: string) {
ctx.set(rewriteCss(v, client.meta));
},
});
client.Trap("Element.prototype.innerHTML", { client.Trap("Element.prototype.innerHTML", {
set(ctx, value: string) { set(ctx, value: string) {
let newval; let newval;
@ -218,8 +183,8 @@ export default function (client: ScramjetClient, self: typeof window) {
return atob( return atob(
client.natives["Element.prototype.getAttribute"].call( client.natives["Element.prototype.getAttribute"].call(
ctx.this, ctx.this,
"data-scramjet-script-source-src" "data-scramjet-script-source-src",
) ),
); );
} }
if (ctx.this instanceof self.HTMLStyleElement) { if (ctx.this instanceof self.HTMLStyleElement) {
@ -309,7 +274,7 @@ export default function (client: ScramjetClient, self: typeof window) {
ctx.args[0], ctx.args[0],
client.cookieStore, client.cookieStore,
client.meta, client.meta,
false false,
); );
} }
}, },
@ -321,7 +286,7 @@ export default function (client: ScramjetClient, self: typeof window) {
ctx.args[0], ctx.args[0],
client.cookieStore, client.cookieStore,
client.meta, client.meta,
true true,
); );
}, },
}); });
@ -332,7 +297,7 @@ export default function (client: ScramjetClient, self: typeof window) {
ctx.args[0], ctx.args[0],
client.cookieStore, client.cookieStore,
client.meta, client.meta,
false false,
); );
}, },
}); });