gpt4 book ai didi

javascript - Chrome.webrequest.onBeforeRequest 多次调用自身

转载 作者:行者123 更新时间:2023-11-28 04:03:53 25 4
gpt4 key购买 nike

我正在使用 JavaScript API 开发一个 Chrome 扩展程序,其中我尝试在 URL 与 Web 请求 URL 模式匹配时重定向该 URL

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"]
);

ma​​nifest.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"
}
}

问题:在 Web 请求上检查的相同 URL 模式(即 API 输出 URL)具有“www.answerme.com/quetion/78878#787878”和 Web 请求 URL有“www.answerme.com/quetion/searchquery”

在这种情况下,当我们获取 www.answerme.com/questions/searchquery 时,页面将重定向到 http://www.answerme.com/questions/7847859# 748555,但问题是:我用来检查 URL 的模式有 * 来指示 URL 可以以任何协议(protocol)开头,并且其第一个属性以 quetions 开头>。这与重定向 URL http://www.answerme.com/questions/7847859#7848488 匹配,因此会陷入僵局(重复重定向)。我再次收到相同的 URL 模式请求(redirect url= www.asswerme.com/quetion/7878#7878 并且 list 文件上的 webrequest url 有 www.asswerme.com/quetion/*),因此我决定检查 URL 是否以数字结尾(网络请求 list ),以便我可以轻松地将它们重定向到 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"}
}

关于javascript - Chrome.webrequest.onBeforeRequest 多次调用自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46864162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com