Skip to content

Commit 8df49e8

Browse files
committed
[chrome] implement getInjectScripts for IsolatedFrame
1 parent fa3a199 commit 8df49e8

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

packages/chrome/src/IsolatedFrame.tsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
unrewriteUrl,
1010
type URLMeta,
1111
setInterface,
12+
type ScramjetInterface,
1213
} from "@mercuryworkshop/scramjet/bundled";
1314

1415
import scramjetWASM from "../../scramjet/dist/scramjet.wasm.wasm?url";
@@ -90,11 +91,53 @@ const cfg = {
9091
};
9192

9293
setConfig(cfg);
94+
95+
const getInjectScripts: ScramjetInterface["getInjectScripts"] = (
96+
meta,
97+
handler,
98+
config,
99+
cookieJar,
100+
script
101+
) => {
102+
const dump = JSON.stringify(cookieJar.dump());
103+
const injected = `
104+
self.COOKIE = ${dump};
105+
$scramjetLoadClient().loadAndHook({
106+
interface: {
107+
getInjectScripts: ${getInjectScripts.toString()},
108+
onClientbound: function() { return undefined; },
109+
sendServerbound: async function() {}
110+
},
111+
config: ${JSON.stringify(config)},
112+
transport: null,
113+
});
114+
if ("document" in self && document?.currentScript) {
115+
document.currentScript.remove();
116+
}
117+
`;
118+
119+
// for compatibility purpose
120+
const base64Injected = btoa(
121+
new Uint8Array(new TextEncoder().encode(injected))
122+
.reduce(
123+
(data, byte) => (data.push(String.fromCharCode(byte)), data),
124+
[] as any
125+
)
126+
.join("")
127+
);
128+
129+
return [
130+
script(config.files.wasm),
131+
script(config.files.all),
132+
script("data:application/javascript;base64," + base64Injected),
133+
];
134+
};
93135
setInterface({
94-
onServerbound: (type, fn) => {},
95-
onClientbound: (type, fn) => {},
96-
sendClientbound: (type, message) => {},
97-
sendServerbound: (type, message) => {},
136+
onServerbound: (type, listener) => {
137+
return undefined;
138+
},
139+
sendClientbound: async (type, msg) => {},
140+
getInjectScripts,
98141
});
99142
export const bare = new BareClient(
100143
new LibcurlClient({

0 commit comments

Comments
 (0)