如何编写更改 url 的脚本
How to program a script that changes url
我想制作一个主要更改页面 url 的 tampermonkey 脚本。我想做的是查看 url 中是否有 "youtube.com",如果没有,则应将 /youtube.com 添加到 url。
这方面的一个例子是:
起始网站:www.website.com/watch8dzjad8
更改后的网站:www.website.com/youtube.com/watch8dzjad8
如果有帮助,那么脚本将在 tampermonkey 中完成,以便在特定网站上扫描 link 并在可以时添加 /youtube.com'找不到它,因为它不会工作,它真的会帮助我不要每天复制和粘贴 /youtube.com 10 次,以及学习如何使用 URL JavaScript。提前致谢
if( !location.host.match(/youtube.com/) )
location= "/youtube.com"+ location.pathname
但您应该将此行为限制在特定站点,而不仅仅是所有非 youtube 的域,例如:
if( location.href.match(/website.com\/watch/) )
location= "/youtube.com"+ location.pathname
说明
location.href.match(/网站.com/watch/)
- location.host 是页面的域 (www.website.com)
location.href是页面的完整URL(http://www.website.com/watch8dzjad8)
匹配 测试字符串是否遵循给定的模式
location= "/youtube.com"+ location.pathname
- 设置位置意味着打开给定的URL
- location.pathname给出URL(/watch8dzjad8)
的路径
所以如果访问页面的URL(http://www.website.com/watch8dzjad8)包含字符串"website.com/watch",则打开"/youtube.com" + "/watch8dzjad8".
因为域是一样的,一个相对的URL就够了,浏览器知道和当前页面是同一个域。
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
试试这个
function getQueryValue( myUrl ){
myUrl = newUrl.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]" + myUrl + "=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( location.href);
if( results == null )
return "";
else
return results;
}
//current url
var curUrl = location.href;
//new url
var newUrl = getQueryValue( "curUrl" );
//redirect to new page
location.href = newUrl;
}
我想制作一个主要更改页面 url 的 tampermonkey 脚本。我想做的是查看 url 中是否有 "youtube.com",如果没有,则应将 /youtube.com 添加到 url。
这方面的一个例子是:
起始网站:www.website.com/watch8dzjad8
更改后的网站:www.website.com/youtube.com/watch8dzjad8
如果有帮助,那么脚本将在 tampermonkey 中完成,以便在特定网站上扫描 link 并在可以时添加 /youtube.com'找不到它,因为它不会工作,它真的会帮助我不要每天复制和粘贴 /youtube.com 10 次,以及学习如何使用 URL JavaScript。提前致谢
if( !location.host.match(/youtube.com/) )
location= "/youtube.com"+ location.pathname
但您应该将此行为限制在特定站点,而不仅仅是所有非 youtube 的域,例如:
if( location.href.match(/website.com\/watch/) )
location= "/youtube.com"+ location.pathname
说明
location.href.match(/网站.com/watch/)
- location.host 是页面的域 (www.website.com)
location.href是页面的完整URL(http://www.website.com/watch8dzjad8)
匹配 测试字符串是否遵循给定的模式
location= "/youtube.com"+ location.pathname
- 设置位置意味着打开给定的URL
- location.pathname给出URL(/watch8dzjad8) 的路径
所以如果访问页面的URL(http://www.website.com/watch8dzjad8)包含字符串"website.com/watch",则打开"/youtube.com" + "/watch8dzjad8".
因为域是一样的,一个相对的URL就够了,浏览器知道和当前页面是同一个域。
https://developer.mozilla.org/en-US/docs/Web/API/Window/location https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
试试这个
function getQueryValue( myUrl ){
myUrl = newUrl.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]" + myUrl + "=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( location.href);
if( results == null )
return "";
else
return results;
}
//current url
var curUrl = location.href;
//new url
var newUrl = getQueryValue( "curUrl" );
//redirect to new page
location.href = newUrl;
}