Pause chrome.webRequest.onBeforeRequest until message | Chrome extension

Viewed 96

I am working on a chrome extension and I am having trouble pausing web requests until message is received from Script.js.

chrome.webRequest.onBeforeRequest.addListener(function (e){

    chrome.tabs.executeScript(null, {code:"var eventURL = JSON.parse('" + encodeToPassToContentScript(e) + "');"}, function(){
        chrome.tabs.executeScript(null, { code:`(async function() { 
            //console.log('E URL', eventURL.url);
            if (eventURL.url.match("some condition")){
        
                var iframe = document.createElement('iframe');
                iframe.src = url ;
                document.body.prepend(iframe);
                iframe.addEventListener("load", function() {
                console.log('Iframe loaded');
                // Gather data
                chrome.runtime.sendMessage(extraData, function(response){
                    console.log('extraData', extraData);
                console.log('response sent', response);
                });
            
              
        });
            }
    })()`}, () => {
          new Promise(resolve => {
            chrome.runtime.onMessage.addListener(function listener(result) {
              chrome.runtime.onMessage.removeListener(listener);
              console.log('RES', result)
              resolve(result);
            });
          }).then(result => {
          
            console.log('RES', result);
          });
        });


    })






        return {cancel: false}; 
    

}, {
    urls:["<all_urls>"],
    types:["xmlhttprequest"]
}, ["blocking","requestBody"]);

I have the code above. My goal is filtering web request -> prepending iframe to the current tab, gathering some data from Iframe -> sending message to background.js -> continue the web request.

0 Answers
Related