Skip to content Skip to sidebar Skip to footer

Chrome Extension Timed Redirection

What I'm trying to achieve is make a chrome extension run in the background, and every minute it'll redirect to google.com and then a minute later redirect to stackoverflow.com and

Solution 1:

Alternate solution (I am leaving url selection part out).

background.html:

var timer = setInterval(function() {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.update(tab.id, {url: "http://google.com"});
    });
}, 60000);

//stop
chrome.browserAction.onClicked.addListener(function(){
    clearInterval(timer);
});

Solution 2:

You could also just write a content script that you would inject, but it's easy enough that I just stored it in a variable. I would recommend looking at the chrome.tab API, as Google does a very good job of documenting their API for developers.

In your background page:

varREDIRECTION_SCRIPT_A = "window.location.href='http://www.google.com'";
varREDIRECTION_SCRIPT_B = "window.location.href='http://bit.ly/m2TXqC'";
var toGoogle = true;
var intervalId;

chrome.browserAction.onClicked.addListener(function() {
  clearInterval(intervalId);
});

// Execute redirection script on current page// Note that you can select any tab based on its ID by replacing// null belowfunctionannoyUser() {
  console.log("test");
  chrome.tabs.executeScript(null, {code:
    (toGoogle ? REDIRECTION_SCRIPT_A : REDIRECTION_SCRIPT_B) });
  toGoogle = !toGoogle;
}

// Do once a minute ad infinitum
intervalId = setInterval(annoyUser, 5000);

In your manifest.json:

{
  ...
  "permissions": ["http://*/*", "tabs"],
  ...
}

Post a Comment for "Chrome Extension Timed Redirection"