1

Hey trying to redirect from page1 to page2 (within tab, not open new ones) at a specific time.

manifest.json:

{
    "name": "Google",
    "description": "Test",
    "version": "1.0",
    "manifest_version": 2,
    "background": {"scripts":["script.js"]},
    "permissions": [
        "alarms",
        "webRequest",
        "*://www.google.com/*",
        "webRequestBlocking"
    ]
}

script.js:

var host = "https://www.facebook.com/"
chrome.alarms.onAlarm.addListener(function(alarm){
    return {redirectUrl: host}
})

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        return chrome.alarms.create("redirect", {when: Date.now() + 5000})
    },{
        urls: [
            "*://www.google.com/*"
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    ["blocking"]
)

code to redirect was taken from This Question, code to use timer was taken from This Question

EDIT: Solution thanks to Xan's answer below

manifest.json

{
    "name": "Google",
    "description": "Test",
    "version": "1.0",
    "manifest_version": 2,
    "content_scripts": [{
        "matches": ["*://www.google.com/*"],
        "js": ["script.js"]
    }]
}

script.js (modify setTimeout with Date.now() to get correct wait)

setTimeout(function() {
  window.location = "https://www.facebook.com/"
}, 5000)
Community
  • 1
  • 1
user2255757
  • 756
  • 1
  • 6
  • 24
  • The code looks like a random mix of things, not sure if that was the `trying` part :-) Also, clarify please what do you mean by `redirect`: the already opened tabs should be redirected? or all new requests to page1 should be redirected? BTW [declarativeWebRequest](https://developer.chrome.com/extensions/declarativeWebRequest) might be useful. – wOxxOm Sep 29 '15 at 13:25
  • not too familiar with javascript on this end. all tabs which open a form of google should be reloaded to facebook. – user2255757 Sep 29 '15 at 13:33

1 Answers1

2

That won't work, and it's not what you want anyway.

chrome.webRequest.onBeforeRequest refers to what to decide regarding a network request before it's even sent.

First off, it stalls the network request waiting for a decision. As such, it does NOT allow a redirect to happen asynchronously - you have to decide right now, not in 5 seconds.

Second, it seems like you want the page to load, but in 5 seconds be redirected somewhere else - not keep "loading" for 5 seconds. Then webRequest is the wrong place to look.

What you want is a content script, a piece of JS code that will execute in the context of a tab after it loads.

I'll leave the manifest fields an exercise for the reader, and this code will work:

// Content script
setTimeout(function() {
  window.location = "https://example.com/";
}, 5000);
Xan
  • 74,770
  • 16
  • 179
  • 206
  • Thank you, nice to know some people like to educate newcomers instead of just downvoting – user2255757 Sep 29 '15 at 13:59
  • @user2255757 Speaking of education - please roll back your edit to the question, and post your final solution as a separate answer. – Xan Sep 29 '15 at 14:27