[英]Chrome extension to check a link and redirect to another website
我正在構建一個擴展程序,在該擴展程序中我想阻止來自chrome / firefox瀏覽器的某些網站URL。
可以說我有一個URLS列表,我希望將其列入黑名單。 因此,每當chrome用戶想要加入他們時,該擴展名就會重定向到我選擇的另一個URL(在代碼中,我將確定我想要的URL)
通過一些研究,我設法為CHROME做了這件事
manifest.json
{
"name": "URL Block",
"description": "Redirect to another site",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"webRequest",
"*://facebook.com/*",
"*://www.facebook.com/*",
"*://apple.com/*",
"*://www.apple.com/*",
"*://iptorrents.com/*",
"*://www.iptorrents.com/*",
"webRequestBlocking"
]
}
background.js
var host = "http://www.google.com";
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};
},
{
urls: [
"*://facebook.com/*",
"*://www.facebook.com/*",
"*://apple.com/*",
"*://www.apple.com/*",
"*://iptorrents.com/*",
"*://www.iptorrents.com/*"
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
因此,此擴展程序非常適合我嘗試執行的操作。 但是現在我有一些問題。
題:
可以說,我希望擴展名重定向到2個不同的URL(不僅像上面的示例中那樣,不僅重定向到google.com。)[這意味着,當我放置URL:www.facebook.com並按Enter時,擴展名將將某些標簽重定向到www.google.com並打開一個新標簽以重定向到www.abc.com]
我不知道如何在Chrome瀏覽器中執行此操作,但是使用Firefox,您可以這樣做:
如何僅在Firefox的一個選項卡中更改用戶代理? 在該解決方案中,而不是使用httpChannel.setRequestHeader
代替httpChannel.redirectTo
您可以在此處閱讀有關redirectTo的信息: https : //developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel#redirectTo%28%29
您既可以重定向選項卡,也可以打開一個新選項卡,這似乎是您想要實現的目標。
就這么簡單(在onBeforeRequest
偵聽器中):
function(details) {
chrome.tabs.create({
url: "your 2nd URL",
active: true // Change to false if you want it to open in the background
// and see the docs for more options
});
return {redirectUrl: "your 1st URL" };
}
但是,您可能不想每次訪問URL時都打開一個新選項卡。 注意您的types
聲明:
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
這意味着每次任何頁面嘗試從這些站點加載資源時,都會彈出一個新選項卡。 因此,最好按類型進行過濾:
function(details) {
if(details.type == "main_frame") {
chrome.tabs.create({
url: "your 2nd URL",
active: true
});
}
return {redirectUrl: "your 1st URL" };
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.