Chrome.webrequest.onBeforeRequest多次调用自己

时间:2017-10-21 14:14:01

标签: javascript url google-chrome-extension

我正在使用JavaScript API处理Chrome扩展程序,我在尝试重定向网址与webrequest网址匹配时

how chrome.webrequest.onebforerequest calling itsself multiple times

backgorund.js

        chrome.webRequest.onBeforeRequest.addListener(
            function(details) {
                var host="https://api.answerme.com/2.2/questions/345797";
                var x = new XMLHttpRequest();
                x.open('GET',host,false);

                x.onload = function() {
                // this function gives me URL and store into "redirectAnswerUrl" global variable (working fine)
                setRedirectedToAnswer(x.responseText);
                };      
                x.send();
         //this working fine output:-www.answerme.com/quetion/78784#45545
         alert("redirectAnswerUrl"+redirectAnswerUrl);
                if(redirectAnswerUrl!=""){
                  //but this redirectAnswerUrl has same pattern of default urls that is above output and manifest url  
                 return {redirectUrl: redirectAnswerUrl}
                }

            },
            {
                 //setting url which we gonna match during first load 
                 urls: [
                    "*://answerme.com/questions/*",
                    "*://www.answerme.com/questions/*"
                 ]
            },
            ["blocking"]
        );

的manifest.json

{
    "name": "Demo",
    "description": "Demo",
    "version": "0.1",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "logo.png",
        "default_popup": "window.html"
    },
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "activeTab", "tabs", "webRequest",
        "*://answerme.com/questions/*",
        "*://www.answerme.com/questions/*"
        "webRequestBlocking", "storage"
    ],
    "icons": {
        "16": "logo.png",
        "128": "logo.png"
    }
}

问题:在webrequest上检查的相同网址格式是API输出网址" www.answerme.com/quetion / 78878#787878&# 34;和webrequest网址已经" www.answerme.com/quetion / searchquery"

在这种情况下,当我们获得www.answerme.com/questions/searchquery时,页面将被重定向到http://www.answerme.com/questions/7847859#748555,但问题是:我用来检查网址的模式有*来表示URL可以从任何协议开始,其第一个属性以quetions开头。这与重定向网址http://www.answerme.com/questions/7847859#7848488匹配,因此会变为死锁(重复重定向)。我再次获得相同的网址格式请求(redirect url= www.asswerme.com/quetion/7878#7878 and webrequest url on manifest file has www.asswerme.com/quetion/*),因此我决定检查网址是否以数字(网络广告清单)结尾,以便我可以轻松地将其重定向到{ {1}}没有任何含糊之处。

2 个答案:

答案 0 :(得分:0)

您可以使用RegExp检查details.url是否仅以数字字符结尾

if (!/^\d+$/.test(details.url.split("/").pop())) {
  // `details.url` does not end with only digits, do other stuff
} else {
  return {redirectUrl: "http://www.example.com/add/7847859"}
}

答案 1 :(得分:0)

我得到解决方案定义redirectAnswerUrl="";并检查条件是否redirectAnswerUrl==""进入其他重定向

    chrome.webRequest.onBeforeRequest.addListener(
                function(details) {
                if(redirectAnswerUrl==""){
                    var host="https://api.answerme.com/2.2/questions/345797";
                    var x = new XMLHttpRequest();
                    x.open('GET',host,false);

                    x.onload = function() {
                    // this function gives me URL and store into "redirectAnswerUrl" global variable (working fine)
                    setRedirectedToAnswer(x.responseText);
                    };      
                    x.send();
             //this working fine output:-www.answerme.com/quetion/78784#45545
             alert("redirectAnswerUrl"+redirectAnswerUrl);
                    if(redirectAnswerUrl!=""){
                      //but this redirectAnswerUrl has same pattern of default urls that is above output and manifest url  
                     return {redirectUrl: redirectAnswerUrl}
                    }else{
                  return {redirectUrl: details.url}
}

                },
                {
                     //setting url which we gonna match during first load 
                     urls: [
                        "*://answerme.com/questions/*",
                        "*://www.answerme.com/questions/*"
                     ]
                },
                ["blocking"]
            );