简体   繁体   中英

Use async calls in a blocking webRequest handler in chrome

I'm developing an extension that reads a database (firebase for now) info before redirecting the user to another page.

Right now I have successfully implemented the logic of static redirect by simply doing:

function examineUrl(details) {
  return {redirectUrl: 'https://www.example.com/'};
}

Then I would love to implement the async logic of reading from database. I read in Use asynchronous calls in a blocking webRequest handler (It is a similar question but for firefox) that I can return a Promise, so I try that:

function examineUrl(details) {
    return new Promise(function(resolve, reject) {
        return resolve( {redirectUrl: 'https://www.example.com/'})
    });
}

But there is no redirect action happening in chrome. The documentation for chrome did not mention it https://developer.chrome.com/extensions/webRequest , either. So I wonder if there exists a workaround for this in chrome?

As the comment goes, there is no such support in chrome.

My workaround is to embrace the asynchronous pattern by reading data in a different asynchronous block instead of inside the webRequest handler -- kudos for code restructure for a better implementation!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM