1

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]

Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • Yes, please make a separate question for firefox an **highlight** your actual question. See ["How to Ask" guide](http://stackoverflow.com/help/how-to-ask). You already did a good job salvaging the question, but it needs a little push to be cleanly answerable. – Xan Jun 19 '15 at 22:24

2 Answers2

1

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

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323
1

You can both redirect a tab and open a new one, which seems like what you're trying to achieve.

It's as simple as this (in the onBeforeRequest listener):

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" };
}

However, you probably don't want to open a new tab every time a URL is accessed. Note your types declaration:

types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]

That means every time any page tries to load a resource from those sites, you'll get a new tab popping up. So, it's best to filter by type:

function(details) {
  if(details.type == "main_frame") {
    chrome.tabs.create({
      url: "your 2nd URL",
      active: true
    });
  }
  return {redirectUrl: "your 1st URL" };
}
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I put the second code inside the `onBeforeRequest` listener, replace the urls where you mention but i have some problems 1) it opens a new tab but its not redirecting to the page, but in this link here [link](chrome-extension://fkecblnphpabincjjhiognoobljgjpli/www.abc.com) when i try to add abc.com to the 2nd url. 2) the first tab is joining the page that im trying to block. – Konstantinos Natsios Jun 20 '15 at 20:36
  • `chrome.webRequest.onBeforeRequest.addListener( function(details) { if(details.type == "main_frame") { chrome.tabs.create({ url: "www.abc.com", active: true }); } return {redirectUrl: "www.abc.com" }; }, { 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"] );` – Konstantinos Natsios Jun 20 '15 at 20:37
  • and i want to open a new tab whenever i try to go to a blacklisted URL. Thanks a lot for your answer!! – Konstantinos Natsios Jun 20 '15 at 20:38
  • In general, you should make a new question for such code dumps. You need to specify the protocol for `www.abc.com`, make it a full url. If you still have questions post a new one. – Xan Jun 23 '15 at 11:56
  • My question is the same as my post. since the code that you posted is not working for me, is it working for you? thats all i ask about your answer – Konstantinos Natsios Jun 24 '15 at 13:01