Skip to content

Commit

Permalink
feat: improve natives store and proxy element.hasattribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Dec 9, 2024
1 parent f43637f commit f022024
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
46 changes: 45 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class ScramjetClient {
bare: BareClientType;

descriptors: Record<string, PropertyDescriptor> = {};
natives: Record<string, any> = {};
natives: Record<string, any>;
wrapfn: (i: any, ...args: any) => any;

cookieStore = new CookieStore();
Expand Down Expand Up @@ -120,7 +120,51 @@ export class ScramjetClient {
})
);
}
this.natives = new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
} else {
const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);

if (!realTarget) return;

const original = Reflect.get(realTarget, realProp);
target[prop] = original;

return target[prop];
}
},
}
);
this.descriptors = new Proxy(
{},
{
get: (target, prop: string) => {
if (prop in target) {
return target[prop];
} else {
const split = prop.split(".");
const realProp = split.pop();
const realTarget = split.reduce((a, b) => a?.[b], this.global);

if (!realTarget) return;

const original = nativeGetOwnPropertyDescriptor(
realTarget,
realProp
);
target[prop] = original;

return target[prop];
}
},
}
);
// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;
this.meta = {
Expand Down
19 changes: 13 additions & 6 deletions src/client/dom/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { unrewriteUrl, htmlRules, unrewriteHtml } from "../../shared";
import { rewriteCss, rewriteHtml, rewriteJs } from "../../shared";

export default function (client: ScramjetClient, self: typeof window) {
const _nativeGetAttribute = self.Element.prototype.getAttribute;
const nativeSetAttribute = self.Element.prototype.setAttribute;
const nativeHasAttribute = self.Element.prototype.hasAttribute;

const attrObject = {
nonce: [self.HTMLElement],
integrity: [self.HTMLScriptElement, self.HTMLLinkElement],
Expand Down Expand Up @@ -122,7 +118,12 @@ export default function (client: ScramjetClient, self: typeof window) {
return ctx.return(null);
}

if (nativeHasAttribute.call(ctx.this, `scramjet-attr-${name}`)) {
if (
client.natives["Element.prototype.hasAttribute"].call(
ctx.this,
`scramjet-attr-${name}`
)
) {
const attrib = ctx.fn.call(ctx.this, `scramjet-attr-${name}`);
if (attrib === null) return ctx.return("");

Expand All @@ -142,6 +143,12 @@ export default function (client: ScramjetClient, self: typeof window) {
},
});

client.Proxy("Element.prototype.hasAttribute", {
apply(ctx) {
if (ctx.args[0].startsWith("scramjet-attr")) return ctx.return(false);
},
});

client.Proxy("Element.prototype.setAttribute", {
apply(ctx) {
const [name, value] = ctx.args;
Expand Down Expand Up @@ -182,7 +189,7 @@ export default function (client: ScramjetClient, self: typeof window) {

if (ruleList) {
ctx.args[2] = ruleList.fn(value, client.meta, client.cookieStore);
nativeSetAttribute.call(
client.natives["Element.prototype.setAttribute"].call(
ctx.this,
`scramjet-attr-${ctx.args[1]}`,
value
Expand Down
4 changes: 1 addition & 3 deletions src/client/shared/requests/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { ScramjetClient } from "../../client";
export default function (client: ScramjetClient, self: Self) {
let worker;
if (self.Worker && flagEnabled("syncxhr", client.url)) {
worker = new (client.natives["Worker"] ? client.natives["Worker"] : Worker)(
config.files.sync
);
worker = new client.natives["Worker"](config.files.sync);
}
const ARGS = Symbol("xhr original args");
const HEADERS = Symbol("xhr headers");
Expand Down

0 comments on commit f022024

Please sign in to comment.