proxy getRegistration

This commit is contained in:
velzie 2024-08-03 11:52:42 -04:00
parent dad499f5c4
commit 3ebbc5e430
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
3 changed files with 14 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import { type MessageC2W } from "../../worker";
export const order = 2; export const order = 2;
export default function (client: ScramjetClient, self: Self) { export default function (client: ScramjetClient, self: Self) {
let fakeregistrations = new WeakSet(); let registration;
client.Proxy("Worklet.prototype.addModule", { client.Proxy("Worklet.prototype.addModule", {
apply(ctx) { apply(ctx) {
@ -16,7 +16,7 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("EventTarget.prototype.addEventListener", { client.Proxy("EventTarget.prototype.addEventListener", {
apply(ctx) { apply(ctx) {
if (fakeregistrations.has(ctx.this)) { if (registration === ctx.this) {
// do nothing // do nothing
ctx.return(undefined); ctx.return(undefined);
} }
@ -25,13 +25,19 @@ export default function (client: ScramjetClient, self: Self) {
client.Proxy("EventTarget.prototype.removeEventListener", { client.Proxy("EventTarget.prototype.removeEventListener", {
apply(ctx) { apply(ctx) {
if (fakeregistrations.has(ctx.this)) { if (registration === ctx.this) {
// do nothing // do nothing
ctx.return(undefined); ctx.return(undefined);
} }
}, },
}); });
client.Proxy("navigator.serviceWorker.getRegistration", {
apply(ctx) {
ctx.return(new Promise((resolve) => resolve(registration)));
},
});
client.Proxy("navigator.serviceWorker.register", { client.Proxy("navigator.serviceWorker.register", {
apply(ctx) { apply(ctx) {
if (ctx.args[0] instanceof URL) ctx.args[0] = ctx.args[0].href; if (ctx.args[0] instanceof URL) ctx.args[0] = ctx.args[0].href;
@ -80,11 +86,9 @@ export default function (client: ScramjetClient, self: Self) {
}, },
} }
); );
fakeregistrations.add(fakeRegistration); registration = fakeRegistration;
ctx.return(new Promise((resolve) => resolve(fakeRegistration))); ctx.return(new Promise((resolve) => resolve(fakeRegistration)));
}, },
}); });
// delete self.navigator.serviceWorker;
} }

View file

@ -47,6 +47,9 @@ export class ScramjetServiceWorkerRuntime {
installing: null, installing: null,
waiting: null, waiting: null,
}; };
// @ts-ignore
this.client.global.ServiceWorkerGlobalScope = this.client.global;
} }
} }

View file

@ -92,7 +92,7 @@ function App() {
} }
iframe { iframe {
border: 2px solid #313131; border: 2px solid #313131;
background-color: #121212; background-color: #fff;
border-radius: 0.5rem; border-radius: 0.5rem;
margin: 1em; margin: 1em;
margin-top: 0.5em; margin-top: 0.5em;