这取决于服务器响应重定向使用chrome.webRequest用户 [英] Redirecting user using chrome.webRequest depending on server response

查看:131
本文介绍了这取决于服务器响应重定向使用chrome.webRequest用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来我发现自己在一个catch 22。

It seems I have found myself in a catch 22.

我使用的是的Chrome Web请求在扩展事件得到的网址所有的web请求为这样的:

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}
                  }
              }
});

然后,我想用PHP来获取URL的类别来查询我的服务器。如果有一类1,我想阻止它,如果它有一个类0,我想允许请求通过。

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.

我看到最大的问题是,因为有一个在的getCategory功能的AJAX请求,它不返回一个值,只需执行继续正常。我需要找到一个方法来阻止执行,直到返回实际收到莫名其妙,但我不太清楚如何做到这一点,同时仍然能够重定向URL返回到侦听功能。

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.

同步的要求会不同的是,你不能设置一个暂停时以同步AJAX请求几乎工作。不理想 - 因此,如果我们的服务器宕机,每个人的网络请求都将被考虑范畴受阻。

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
}

我已经使用像目前正在这里使用的.done()函数AJAX绑定尝试。我也试着寻找方法,使同步请求超时和使用。当()。那么()函数的组合,但我无法找到一个方法来从这些返回值在正确的范围内。

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.

有没有一种方法可以让我得到了Ajax请求外部函数这个值?我见过尝试一切有错误的返回范围。有谁知道这是某种可能吗?

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?

有关重组的code想法也接受了! (只要它会与chrome.webRequest)

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

推荐答案

你不会得到太多出这一点。

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

在你理想的情况下,Chrome浏览器已经在内部建立了特殊的例外的的接受承诺为基础的返回值。

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

我怀疑是这样的话,但你可以随时检查API文档中提及接受的承诺或一个thenable。

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

本更可能的解决方案是,你将需要拉动1类请求整个列表到您的客户端code

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

这将不得不投入一个对象/数组中的程序,或放在里面的localStorage
由于您使用的是Chrome扩展,我将假设的localStorage 可能是可用的,在这里。

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.

此外,虽然的localStorage 将工作,WebSQL(死)和索引型数据库将无法工作,因为他们也是异步访问(即使数据被保存在那里,通过客户机的浏览器)。

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.

这篇关于这取决于服务器响应重定向使用chrome.webRequest用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆