Chrome扩展程序检查链接并重定向到其他网站 [英] Chrome extension to check a link and redirect to another website

查看:252
本文介绍了Chrome扩展程序检查链接并重定向到其他网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以说我有一个URLS列表,我的URL列表是我自己的,想把它变成黑名单。
因此,无论何时chrome用户想要加入它们,该扩展将重定向到我选择的另一个URL(在代码中,我将确定我想要的URL)。

通过一些研究,我设法为CHROME制作了这一张

>

  {
name:URL Block,
description:重定向到其他网站,
version:1.0,
manifest_version:2,
background:{
scripts:[
background.js$ [b $ b]
},
权限:[
webRequest,
*://facebook.com/*,
*: //www.facebook.com/*,
*://apple.com/*,
*://www.apple.com/*,
*://iptorrents.com/*,
*://www.iptorrents.com/*,
webRequestBlocking
]
}

背景.js

  var host =http://www.google.com; 
chrome.webRequest.onBeforeRequest.addListener(
function(details){
return {redirectUrl:host + details.url.match(/ ^ https https::\ / \ / [^ \ /] +([\S\s] *)/)[1]};
},
{
网址:[
*:// facebook.com/*,
*://www.facebook.com/*,
*://apple.com/*,
*:// www.apple.com/*,
*://iptorrents.com/*,
*://www.iptorrents.com/*

类型:[main_frame,sub_frame,stylesheet,script,image,object,xmlhttprequest,other]
},
[blocking ]
);

所以这个扩展对于我试图做的事情来说是完美的。
但现在我有一些问题。

问题:

让说我想要扩展名重定向到2个不同的URL
(并且不仅在google.com中,如上面的示例中所示)。
[也就是说,当我把URL放到www.facebook.com和按回车,分机会将某些标签重定向到www.google.com并打开新标签以重定向到www.abc.com]

解决方案

我不知道如何在Chrome中做到这一点,但使用firefox你可以这样做:

< a href =https://stackoverflow.com/questions/22361720/how-can-i-change-the-user-agent-in-just-one-tab-of-firefox/22370569#22370569>我怎样才能在该解决方案中更改Firefox中的用户代理?,而不是 httpChannel.setRequestHeader 执行 httpChannel.redirectTo 你可以在这里阅读redirectTo: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel#redirectTo%28%29


I am building one extension in which i want to block some website urls from my chrome/firefox browser.

Lets say that i have a list of URLS that i want to make it a blacklist. So whenever the chrome user wants to join them, the extension will redirect to another URL of my choice (inside the code i will determine which URL i want it to be)

Through some research i managed to make for CHROME this one

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

So this extension works perfect for what im trying to do. But now i have some questions.

QUESTION:

Lets say that i want the extension to redirect to 2 different URL (and not only in google.com as in my example above.) [Which means, when i put URL: www.facebook.com and press the enter, the extension will redirect the certain tab to www.google.com AND OPEN A NEW TAB to redirect to www.abc.com]

解决方案

I don't know how to do it in chrome but with firefox you do it like this:

How can I change the User Agent in just one tab of Firefox? in that solution instead of httpChannel.setRequestHeader do httpChannel.redirectTo you can read about redirectTo here: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel#redirectTo%28%29

这篇关于Chrome扩展程序检查链接并重定向到其他网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆