|
9 | 9 | unrewriteUrl, |
10 | 10 | type URLMeta, |
11 | 11 | setInterface, |
| 12 | + type ScramjetInterface, |
12 | 13 | } from "@mercuryworkshop/scramjet/bundled"; |
13 | 14 |
|
14 | 15 | import scramjetWASM from "../../scramjet/dist/scramjet.wasm.wasm?url"; |
@@ -90,11 +91,53 @@ const cfg = { |
90 | 91 | }; |
91 | 92 |
|
92 | 93 | 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 | +}; |
93 | 135 | 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, |
98 | 141 | }); |
99 | 142 | export const bare = new BareClient( |
100 | 143 | new LibcurlClient({ |
|
0 commit comments