美好时光。
我有一些有趣的想法,在输入推送后需要更改URL#34;输入"用户。
我的清单文件:
{
"name": "The Pirate Bay_2",
"description": "Redirect The Pirate Bay to a different host",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_title": "Saving Studio generator",
"default_popup": "popup.html"
},
"background": {"scripts":["redirect.js"]},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["background.js"]
}
],
"permissions":
[
"webRequest",
"https://www.amazon.com/",
"webRequestBlocking",
"tabs",
"activeTab"
]
}
我的redirect.js文件:
var host = "https://2ch.hk/b/";
chrome.tabs.query({
'active': true, 'currentWindow': true
}, function (tabs) {
var url = tabs[0].url;
host = host+url;
console.log(url);
});
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (localStorage.check_box == "true"){
console.log("start_1");
return {redirectUrl: host};
}
},
{
urls: ["https://www.amazon.com/" ],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
主要是如何获取输入的网址,通过某些正则表达式模式更改,返回生成的网址以重定向到它。怎么可能这样做?如何在 onBeforeRequest 中插入 chrome.tabs.query 或者这个还有其他方法吗?
非常感谢
答案 0 :(得分:0)
我必须使用同样异步的chrome storage API做类似的事情。我所做的是将Web请求侦听器放在异步函数的回调中。所以在你的情况下:
var host = "https://2ch.hk/b/";
chrome.tabs.query(
{
'active': true, 'currentWindow': true
},
function (tabs) {
var url = tabs[0].url;
host = host+url;
console.log(url);
}
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (localStorage.check_box == "true"){
console.log("start_1");
return {redirectUrl: host};
}
},
{
urls: ["https://www.amazon.com/" ],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
);
我还没有对此进行过测试,但这是我在需要使用存储API时所做的,所以试一试。