Chad
Chad

Reputation: 2455

Redirect faster with Greasemonkey (or similar userscript engine)?

I'm using Greasemonkey to redirect certain URLs to another but I would like to redirect before the URL to be redirect loads.

Currently I'm using this simple script:

//==UserScript==
// @name Redirect Google
// @description Redirect Google to Yahoo!
// @include http://*.google.com/*
//==/UserScript==
window.location.replace("http://www.yahoo.com")

In the above, Google appears for a second and then redirected to Google. I want to go yahoo immediately. Is it possible, and how?

Upvotes: 6

Views: 7392

Answers (4)

user12638282
user12638282

Reputation: 109

It is possible to do this with Userscripts for Safari using the declarativeNetRequest API.

// ==UserScript==
// @name Redirect Google
// @description Redirect Google to Yahoo!
// @run-at request
// ==/UserScript==

{
    "id": 1,
    "priority": 1,
    "action": {
        "type": "redirect",
        "redirect": {
            "transform": { "scheme": "https", "host": "www.yahoo.com" }
        }
    },
    "condition": { "urlFilter": "||google.com", "resourceTypes": ["main_frame"] }
}

You can also use a regex filter if preferred, e.g.:

// ==UserScript==
// @name Redirect Google
// @description Redirect Google to Yahoo!
// @run-at request
// ==/UserScript==

{
    "id": 1,
    "priority": 1,
    "action": {
        "type": "redirect",
        "redirect": {
            "transform": { "host": "www.yahoo.com" }
        }
    },
    "condition": { "regexFilter": "^https://www\\.google\\.com/example$", "resourceTypes": ["main_frame"] }
}

More info here.

I haven't found a solution yet for Greasemonkey, Tampermonkey or Violentmonkey aside from @user873792's answer (which isn't as performant as the above example or using another add-on like Redirector for Firefox).

Upvotes: 0

user873792
user873792

Reputation: 323

You can use @run-at document-start in the metadata blockDoc.
EG:

//==UserScript==
// @name Redirect Google
// @description Redirect Google to Yahoo!
// @include http://*.google.com/*
// @run-at document-start
//==/UserScript==
window.location.replace ("http://www.yahoo.com")


The script will run before any document begins loading, thus before any scripts run or images load.

Upvotes: 12

erikvold
erikvold

Reputation: 16518

The Redirector add-on for Firefox is what you should use for user defined redirects in Firefox instead of Greasemonkey.

Upvotes: 1

Brock Adams
Brock Adams

Reputation: 93473

Greasemonkey is not the right tool for immediate redirects. (You are doing this for honest purposes, right?)

The easiest way, since you seem to have access to the victim's computer, is to change the hosts file.

(1) Add two entries/lines with these values (windows syntax):

74.6.117.48 google.com
74.6.117.48 www.google.com

(2) Restart the browser. For IE, you may also need to flush the DNS cache (ipconfig /flushdns at the command prompt).

(3) Uninstall or disable that GM script.

(4) Google will now be redirected to Yahoo search.


Alternatively, you could write a browser extension/add-on. But if this is for legitimate, non-prank use; the best, simplest, most flexible (and honest) approach is to leave the Greasemonkey script as is.

Upvotes: 2

Related Questions