繁体   English   中英

Chrome.webrequest.onBeforeRequest多次调用自身

[英]Chrome.webrequest.onBeforeRequest calling itself multiple times

我正在使用JavaScript API开发Chrome扩展程序,在其中与Webrequest URLs模式匹配时,我试图重定向URL

chrome.webrequest.onebforerequest如何多次调用自身

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"
    }
}

问题 :正在对作为API输出URL的webrequest进行检查的相同URL模式具有“ www.answerme.com/quetion / 78878#787878”和webrequest URL具有“ www.answerme.com/quetion / searchquery”

在这种情况下,当我们获得www.answerme.com/questions/searchquery ,页面将被重定向到http://www.answerme.com/questions/7847859#748555 ,但是问题是:我用来检查的模式URL具有*表示URL可以以任何协议开头,并且其第一个属性以quetions 这与重定向URL http://www.answerme.com/questions/7847859#7848488匹配,因此这成为死锁(反复重定向)。 我再次收到相同的URL模式请求( redirect url= www.asswerme.com/quetion/7878#7878 and webrequest url on manifest file has www.asswerme.com/quetion/*再次redirect url= www.asswerme.com/quetion/7878#7878 and webrequest url on manifest file has www.asswerme.com/quetion/* ),因此我决定检查URL不以数字(网络请求清单)结尾,因此我可以毫无歧义地轻松将它们重定向到http://www.answerme.com/questions/7847859#14524

您可以使用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"}
}

我得到了解决方案定义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"]
            );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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