使用 GreaseMonkey 更改 URL
Change the URL using GreaseMonkey
我想用 Greasemonkey 脚本更改页面的当前 URL。
我的意思是,我在位置:“http://www.mywebsite.com/video/old/*.mkv" and I want to move to "http://www.mywebsite.com/video/new/*.mkv”。所以基本上,我只想更改 URL 中的 "new" 中的 "old"。
我找到了这段代码:
var oldUrlPath = window.location.pathname;
/*--- Test that ".compact" is at end of URL, excepting any "hashes"
or searches.
*/
var newURL = window.location.protocol + "//"
+ window.location.host
+ oldURLPath
+ window.location.search
+ window.location.hash
;
/*-- replace() puts the good page in the history instead of the
bad page.
*/
window.location.replace (newURL);
但我不知道如何用我想要的新URL路径替换旧URL路径。
我想我必须使用 replace() 但我不确定(而且我尝试的所有代码都不起作用,我一定没有正确使用它,因为我不熟悉 ReGex)。
感谢您的回答
我认为正则表达式没有任何必要。看起来你可以 replace()
old
和 new
.
var oldURL = "http://www.mywebsite.com/video/old/*.mkv";
location.href = oldURL.replace('old','new');
我想用 Greasemonkey 脚本更改页面的当前 URL。
我的意思是,我在位置:“http://www.mywebsite.com/video/old/*.mkv" and I want to move to "http://www.mywebsite.com/video/new/*.mkv”。所以基本上,我只想更改 URL 中的 "new" 中的 "old"。
我找到了这段代码:
var oldUrlPath = window.location.pathname;
/*--- Test that ".compact" is at end of URL, excepting any "hashes"
or searches.
*/
var newURL = window.location.protocol + "//"
+ window.location.host
+ oldURLPath
+ window.location.search
+ window.location.hash
;
/*-- replace() puts the good page in the history instead of the
bad page.
*/
window.location.replace (newURL);
但我不知道如何用我想要的新URL路径替换旧URL路径。 我想我必须使用 replace() 但我不确定(而且我尝试的所有代码都不起作用,我一定没有正确使用它,因为我不熟悉 ReGex)。
感谢您的回答
我认为正则表达式没有任何必要。看起来你可以 replace()
old
和 new
.
var oldURL = "http://www.mywebsite.com/video/old/*.mkv";
location.href = oldURL.replace('old','new');