8

Hey, I want to redirect a page when it finish loading...

For example, when google.com finish loading, I want to send a javascript to search something...

How can I do it ?

Bruno 'Shady'
  • 4,348
  • 13
  • 55
  • 73
  • javascript:var%20values%20=%20["mail",%20"pass"];%20$("input").each(function(i){%20$(this).val(values[i]);%20});%20doLogin(); – Bruno 'Shady' Jul 02 '10 at 21:18

3 Answers3

7

This is simply how I would go about redirecting:

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

... of course replacing the Google and Yahoo! URLs with something else. You don't need any external libraries (jQuery) or something complicated like that.

I would not reccomend this as it is more of a nuisance than a help to the end user, however that depends on what the function of the script is.

esqew
  • 42,425
  • 27
  • 92
  • 132
  • with this command, you can change the "http://www.yahoo.com" with "javascript:var%20values%20=%20["mail",%20"pass"];%20$("input").each(function(i){%20$(this).val(values[i]);%20});%20doLogin();"? – Bruno 'Shady' Jul 03 '10 at 00:16
  • Yes, it should work, although I haven't tested it. As far as I know, `window.location` works just like you entered the quoted text into the address bar. ;) – esqew Jul 03 '10 at 00:34
  • didn't worked with the javascript command =/ – Bruno 'Shady' Jul 03 '10 at 01:50
  • 1
    Probably because you have double quotes in your javascript string. Try using this for your command: `"javascript:var%20values%20=%20[\"mail\",%20\"pass\"];%20$(\"input\").each(function(i){%20$(this).val(values[i]);%20});%20doLogin();"` – esqew Jul 03 '10 at 02:17
  • I found a way to put multiple redirects in a single script: see my answer. http://stackoverflow.com/a/14576139/975097 – Anderson Green Jan 29 '13 at 05:20
  • Thank you. I needed to replace the url on the addressbar, and this worked perfectly! – Nav Jan 29 '15 at 08:36
4

This method is the most flexible that I've found so far. It's easy to specify multiple redirects in a single script:

// ==UserScript==
// @name       Redirector
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://*/*
// @copyright  2012+, You
// @run-at document-start
// ==/UserScript==

//Any page on youtube.com is automatically redirected to google.com - you can use this
//function to redirect from any page to any other page.
redirectToPage("http://www.youtube.com/", "http://www.google.com");
//You can put several of these redirects in a single script!
redirectToPage("en.wikipedia.org", "http://www.stackoverflow.com");


function redirectToPage(page1, page2){
if(window.location.href.indexOf(page1) != -1){
    window.location.href = page2;
}
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
4

Use window.location.replace(url) if you want to redirect the user in a way that the current page is forgotten by the back button, because otherwise if you use window.location = url then when the user presses the back button, then the userscript will kick in again and push them back the page that they were just on.

erikvold
  • 15,988
  • 11
  • 54
  • 98