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