@@ -113,23 +113,11 @@ function findSelfSequence(
113113 }
114114}
115115
116- type ServerboundMethods = {
117- [ K in keyof Serverbound ] : (
118- tab : Tab ,
119- arg : Serverbound [ K ] [ 0 ]
120- ) => Promise < Serverbound [ K ] [ 1 ] > ;
121- } ;
122- const scramjetipcserverbound : ServerboundMethods = {
123- setCookie : async ( tab , { cookie, url } ) => {
124- console . log ( "setCookie" , cookie , url , tab ) ;
125- return undefined ;
126- } ,
127- blobData : async ( tab , { id, data } ) => {
128- return undefined ;
129- } ,
130- } ;
116+ let sjIpcListeners = new Map < string , ( msg : any ) => Promise < any > > ( ) ;
117+ const sjIpcSyncPool = new Map < number , ( val : any ) => void > ( ) ;
118+ let sjIpcCounter = 0 ;
131119
132- addEventListener ( "message" , ( e ) => {
120+ addEventListener ( "message" , async ( e ) => {
133121 if ( ! e . data || ! ( "$scramjetipc$type" in e . data ) ) return ;
134122 const type = e . data . $scramjetipc$type ;
135123 if ( type === "request" ) {
@@ -146,19 +134,27 @@ addEventListener("message", (e) => {
146134 return null ;
147135 } ;
148136
149- const tab = findTab ( e . source as Window ) ! ;
150- const fn = ( scramjetipcserverbound as any ) [ method ] ;
137+ // const tab = findTab(e.source as Window)!;
138+ const fn = sjIpcListeners . get ( method ) ;
151139 if ( fn ) {
152- fn ( tab , message ) . then ( ( response : any ) => {
153- e . source ! . postMessage ( {
154- $scramjetipc$type : "response" ,
155- $scramjetipc$token : token ,
156- $scramjetipc$message : response ,
157- } ) ;
140+ const response = await fn ( message ) ;
141+ e . source ! . postMessage ( {
142+ $scramjetipc$type : "response" ,
143+ $scramjetipc$token : token ,
144+ $scramjetipc$message : response ,
158145 } ) ;
159146 } else {
160147 console . error ( "Unknown scramjet ipc method" , method ) ;
161148 }
149+ } else if ( type === "response" ) {
150+ const token = e . data . $scramjetipc$token ;
151+ const message = e . data . $scramjetipc$message ;
152+
153+ const cb = sjIpcSyncPool . get ( token ) ;
154+ if ( cb ) {
155+ cb ( message ) ;
156+ sjIpcSyncPool . delete ( token ) ;
157+ }
162158 }
163159} ) ;
164160
@@ -177,15 +173,9 @@ const getInjectScripts: ScramjetInterface["getInjectScripts"] = (
177173 let counter = 0;
178174
179175 let syncPool = new Map();
176+ let listeners = new Map();
180177
181- const scramjetipcclientboundmethods = {
182- setCookie: async ({ cookie, url }) => {
183- console.log("[clientbound] setCookie", cookie, url);
184- return undefined;
185- }
186- };
187-
188- addEventListener("message", (e) => {
178+ addEventListener("message", async (e) => {
189179 if (!e.data || !("$scramjetipc$type" in e.data)) return;
190180 const type = e.data.$scramjetipc$type;
191181 if (type === "response") {
@@ -202,14 +192,13 @@ const getInjectScripts: ScramjetInterface["getInjectScripts"] = (
202192 const message = e.data.$scramjetipc$message;
203193 const token = e.data.$scramjetipc$token;
204194
205- const fn = scramjetipcclientboundmethods[ method] ;
195+ const fn = listeners.get( method) ;
206196 if (fn) {
207- fn(message).then((response) => {
208- e.source.postMessage({
209- $scramjetipc$type: "response",
210- $scramjetipc$token: token,
211- $scramjetipc$message: response,
212- });
197+ const response = await fn(message);
198+ e.source.postMessage({
199+ $scramjetipc$type: "response",
200+ $scramjetipc$token: token,
201+ $scramjetipc$message: response,
213202 });
214203 } else {
215204 console.error("Unknown scramjet ipc clientbound method", method);
@@ -220,7 +209,9 @@ const getInjectScripts: ScramjetInterface["getInjectScripts"] = (
220209 const client = $scramjetLoadClient().loadAndHook({
221210 interface: {
222211 getInjectScripts: ${ getInjectScripts . toString ( ) } ,
223- onClientbound: function() { return undefined; },
212+ onClientbound: function(type, callback) {
213+ listeners.set(type, callback);
214+ },
224215 sendServerbound: async function(type, msg) {
225216 const token = counter++;
226217 target.postMessage({
@@ -262,9 +253,33 @@ const getInjectScripts: ScramjetInterface["getInjectScripts"] = (
262253} ;
263254setInterface ( {
264255 onServerbound : ( type , listener ) => {
256+ sjIpcListeners . set ( type , listener ) ;
257+ } ,
258+ sendClientbound : async ( type , msg ) => {
259+ // TODO: the fetchandler needs an abstracted concept of clients so it can manually decide which one to send to
260+ for ( let tab of browser . tabs ) {
261+ if ( ! tab . frame . frame . contentWindow ) continue ;
262+ const token = sjIpcCounter ++ ;
263+
264+ const recurseSend = ( win : Window ) => {
265+ win . postMessage (
266+ {
267+ $scramjetipc$type : "request" ,
268+ $scramjetipc$method : type ,
269+ $scramjetipc$token : token ,
270+ $scramjetipc$message : msg ,
271+ } ,
272+ "*"
273+ ) ;
274+ for ( let i = 0 ; i < win . frames . length ; i ++ ) {
275+ recurseSend ( win . frames [ i ] ) ;
276+ }
277+ } ;
278+
279+ recurseSend ( tab . frame . frame . contentWindow ) ;
280+ }
265281 return undefined ;
266282 } ,
267- sendClientbound : async ( type , msg ) => { } ,
268283 getInjectScripts,
269284 getWorkerInjectScripts : ( meta , js , config , type ) => {
270285 const module = type === "module" ;
0 commit comments