1

I have an extansion which optimizes my job. In the contentScript file it scans one page, gets needed information and sends it to background file with chrome.runtime.sendMessage and waits for result to change page. In the background file it opens new pages, passes information and runs script on new pages (example of background code)

chrome.tabs.create({url: "https://link.com/shop/search?search=" + artLink}, tab => {
                        chrome.tabs.executeScript(tab.id, {code: `let quantity = ${request.quantity};
                                                                  let price = ` + JSON.stringify(request.price) + `;
                                                                  let art = ` + JSON.stringify(art) +`;`}, function (){
                            chrome.tabs.executeScript(tab.id, {file: `buycd.js`}, function (result) {
                                sendResponse({result: result, id: request.id, tabID: tab.id}); 
                                //chrome.tabs.remove(tab.id);
                            });
                        })

                    });  

Script on new pages does some work, gathers some info and passes it back to background file and then to contentScript. But now i have a problem. I have to use in injected code setTimeout and result that this code sends is always undefines. Example:

let result = 0;
function runAction(){
 //gather info
 //do some work
 result = info;
}
setTimeout(runAction,1000);
result; //sends the result to background file
  • 1
    Asynchronous code doesn't work like that. The last `result` is sent before the callback runs. This approach won't work. Use extension [messaging](https://developer.chrome.com/extensions/messaging) instead. – wOxxOm Nov 05 '20 at 15:49

0 Answers0