1

I am aware that the present Chrome Extension framework for some reason do not support this. Ref Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=625860

However, I am looking for a solution / hack / whatever which might be able to help me circumvent this problem.

cabm
  • 11
  • 1

1 Answers1

0

I have been able to solve the issue using a HACK by placing a sleep of 1 sec (Kept configurable) to the JS execution pipeline.

background.js

var SLEEP_TIME_MS = 1000;

chrome.webRequest.onBeforeRequest.addListener(details => {
        // THE NATIVE CALL HERE
        connectToNativeForSomething({ "text": "SOME_IDENTIFIER" });
        //THE HACK
        threadSleep(SLEEP_TIME_MS);

},
    { urls: ['https://*/UNIQUE_IDENTIFIER*'] },
    ['blocking']
);

function threadSleep(miliseconds) {
    var currentTime = new Date().getTime();
    while (currentTime + miliseconds >= new Date().getTime()) {
        // DO NOTHING HERE
    }
}

function connectToNativeForSomething(message) {
    chrome.runtime.sendNativeMessage(hostName, message, function (response) {
    if (chrome.runtime.lastError) {
        alert("ERROR: " + chrome.runtime.lastError.message);
    } else {
        console.log('Received message from native app: ' + JSON.stringify(response));
        // DO SOMETHING WITH THE RESPONSE HERE
    }
    });
}

Reference SO URL: Pause JS Execution

cabm
  • 11
  • 1