[英]How do you redirect a custom generated URL in javascript?
我正在嘗試編寫一個供個人使用的擴展程序。 我想要獲取 url 的Background.js文件 - 從其中檢測一個整數 - 將其重定向到一個新的 URL,並在其間連接字符串。 但它似乎不起作用(我是新手)。 這是個人使用的一個非常具體的案例。 到目前為止,這是我的示例代碼(當然 URL 是示例):
const abc = "https://www.google.com/anc/wddsd/";
const xyz = "/tracking/file/subsystem";
var res = url.split('/')[4]; //can I even access the URL before the function begins?
假設有一個數字出現在/wddsd/
,我想從原始 URL 中提取它並將其放置在重定向的 URL 中。
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {
redirectUrl:
abc + res + xyz
};
},
{
urls: [
"*//www.google.com/*"
],
types: [
"main_frame",
"sub_frame",
"stylesheet",
"script",
"image",
"object",
"xmlhttprequest",
"other"
]
},
["blocking"]
);
更新:我的manifest.json
的權限是:
"permissions": ["webRequest", "webRequestBlocking", "*://google.com/*"]
嘿,任何人在未來遇到這個問題,我自己修復了它,基本上是替換
var res = url.split('/')[4];
和
var res = details.url.split('/')[4];
並將其放入函數中。 萬歲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.