How can I redirect some page with javascript in Greasemonkey? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-27 06:10:13

问题


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 ?


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/3168944/how-can-i-redirect-some-page-with-javascript-in-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!