feat: service worker preload scripts for improved extensions support (#44411)

* feat: preload scripts for service workers

* feat: service worker IPC

* test: service worker preload scripts and ipc
This commit is contained in:
Sam Maddock 2025-01-31 09:32:45 -05:00 committed by GitHub
parent bc22ee7897
commit 26da3c5d6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
67 changed files with 2103 additions and 298 deletions

View file

@ -0,0 +1,16 @@
const { contextBridge, ipcRenderer } = require('electron');
let result;
try {
result = contextBridge.executeInMainWorld({
func: () => ({
chromeType: typeof chrome,
id: globalThis.chrome?.runtime.id,
manifest: globalThis.chrome?.runtime.getManifest()
})
});
} catch (error) {
console.error(error);
}
ipcRenderer.invoke('preload-extension-result', result);

View file

@ -0,0 +1,3 @@
const { ipcRenderer } = require('electron');
ipcRenderer.send('ping');

View file

@ -0,0 +1,34 @@
const { contextBridge, ipcRenderer } = require('electron');
const evalTests = {
evalConstructorName: () => globalThis.constructor.name
};
const tests = {
testSend: (name, ...args) => {
ipcRenderer.send(name, ...args);
},
testInvoke: async (name, ...args) => {
const result = await ipcRenderer.invoke(name, ...args);
return result;
},
testEvaluate: (testName, args) => {
const func = evalTests[testName];
const result = args
? contextBridge.executeInMainWorld({ func, args })
: contextBridge.executeInMainWorld({ func });
return result;
}
};
ipcRenderer.on('test', async (_event, uuid, name, ...args) => {
console.debug(`running test ${name} for ${uuid}`);
try {
const result = await tests[name]?.(...args);
console.debug(`responding test ${name} for ${uuid}`);
ipcRenderer.send(`test-result-${uuid}`, { error: false, result });
} catch (error) {
console.debug(`erroring test ${name} for ${uuid}`);
ipcRenderer.send(`test-result-${uuid}`, { error: true, result: error.message });
}
});

View file

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<body>
<script>
navigator.serviceWorker.register('sw-logs.js', {
scope: location.pathname.split('/').slice(0, 2).join('/') + '/'
})
</script>
</body>
</html>