'How to execute executeScript before completing onBeforeRequest?

I have a function that intercepts the request and changes the url based on the data from the page

function my_func(requestDetails) {
  var count="0";
  chrome.tabs.executeScript(null, { //Add jquery
        file: "jquery-3.6.0.min.js",
  });
  
  chrome.tabs.executeScript(null, { //Get count
            file: "test.js",
    }, function (result) {
        count=result;     
        console.log("Result "+result);  
  });

  console.log("After "+count);
  return {
    redirectUrl : "https://another.site/ext.php?count="+count
  };  
}

Intercepts the request and calls my_func

chrome.webRequest.onBeforeRequest.addListener(
  my_func,
  {
    urls: ['https://www.site.ru/command'] // or <all_urls>
  }, ["blocking"]
);

test.js Takes text from select and returns it

test=$('select[ng-model="operation.test"] option:selected').text();
test;

When queried in the console, I get this

After 0
Result 1

chrome.tabs.executeScript terminates after executing my_func's return, and I need to do the opposite somehow. What can I do?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source