Redirecting user using chrome.webRequest depending on server response

纵饮孤独 提交于 2019-12-23 03:35:10

问题


It seems I have found myself in a catch 22.

I am using the Chrome Web Request event in an extension to get the URL of all web requests as such:

chrome.webRequest.onBeforeRequest.addListener(
          function(request) { 
                var cat = getCategory(request.url);
                if(cat == "1") {
                       return {redirectUrl: "http://google.com"}
                  } else {
                      return {cancel: false}
                  }
              }
});

Then, I want to query my server using PHP to get the URL’s category. If it has a category of 1, I want to block it, if it has a category of 0, I want to allow the request to go through.

The big issue I’m seeing is that because there’s an AJAX request in the getCategory function, it doesn’t return a value and execution just continues as normal. I need to find a way to block execution until a return is actually received somehow but I’m not quite sure how to do that while still being able to return the redirect URL to the listener function.

Synchronous requests would almost work, except that you can’t set a timeout with a synchronous AJAX request. As such, if our server goes down, everyone’s web requests will be blocked regardless of category -- not ideal.

function getCategory(url) {
    request = $.ajax( // params);
    request.done(function)
// this code executes even before the done block finishes
}

I’ve tried using AJAX bindings like the .done() function currently being used here. I’ve also tried finding ways to make a synchronous request timeout and using .when().then() function combinations but I’m unable to find a way to return the value in the right scope from in those.

Is there a way I can get this value from the ajax request to the outer function? Everything I’ve seen to try has the wrong return scope. Does anyone know if this is somehow possible?

Ideas about restructuring the code also accepted! (As long as it will work with chrome.webRequest)


回答1:


You're not going to get much out of this.

In your ideal scenario, Chrome has internally built that particular exception to also accept promise-based return values.

I doubt that's the case, but you could always check the API documentation for mention of accepting a promise or a "thenable".

The more-likely solution is that you will need to make pulling that whole list of "category-1" requests into your client-side code.

This will either have to be put into an Object/Array in the program, or placed inside of localStorage.
Because you're using a Chrome extension, I'm going to assume that localStorage may well be available, here.

Also, while localStorage will work, WebSQL(dead) and IndexedDB will not work, because they're also asynchronous access (even though the data is stored right there, by the client's browser).

Those are your options, at this point.



来源:https://stackoverflow.com/questions/21396309/redirecting-user-using-chrome-webrequest-depending-on-server-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!