通过Greasemonkey/Tampermonkey/Userscript将参数添加到URL(重定向) [英] Add parameters to the URL (redirect) via a Greasemonkey/Tampermonkey/Userscript
问题描述
我想编写一个Greasemonkey/用户脚本,该脚本会自动将.compact
添加到以 https://开头的URL中pay.reddit.com/,因此它会自动将我重定向到移动版本.
I'd like to write a Greasemonkey/userscript that automatically adds .compact
to URLs starting with https://pay.reddit.com/ so It automatically redirects me to the mobile version.
我一直在寻找类似的用户脚本,尤其是这个用户脚本: https://userscripts.org/scripts/review/112568 试图弄清楚如何编辑替换模式,但我在此领域缺乏技能.
I've been looking at similar userscripts, particularly this one: https://userscripts.org/scripts/review/112568 trying to figure out how to edit the replacement pattern, but I lack skills in this domain.
如何编写一个Greasemonkey脚本,将我从https://pay.reddit.com/*
重定向到https://pay.reddit.com/*.compact
?
How do I write a Greasemonkey script that redirects me from https://pay.reddit.com/*
to https://pay.reddit.com/*.compact
?
谢谢
推荐答案
脚本应执行以下操作:
- 检测当前URL是否已到达压缩站点.
- 如有必要,加载页面的精简版.
- 当心锚定" URL(它们以片段"或哈希"结尾(
) )并对其进行说明. - 将不需要的页面保留在浏览器历史记录之外,以便使后退"按钮正常工作.仅会记住
.compact
URL. - 通过在
document-start
运行,在这种情况下该脚本可以提供更好的性能.
- Detect if the current URL is already to the compact site.
- Load the compact version of the page if necessary.
- Beware of "anchor" URLS (they end with "fragments" or "hashes" (
#...
) ) and account for them. - Keep the unwanted pages out of the browser history so that the back button works well. Only
.compact
URL's will be remembered. - By running at
document-start
, the script can give better performance in this case.
为此,此脚本有效:
// ==UserScript==
// @name _Reddit, ensure compact site is used
// @match *://*.reddit.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
var oldUrlPath = window.location.pathname;
/*--- Test that ".compact" is at end of URL, excepting any "hashes"
or searches.
*/
if ( ! /\.compact$/.test (oldUrlPath) ) {
var newURL = window.location.protocol + "//"
+ window.location.host
+ oldUrlPath + ".compact"
+ window.location.search
+ window.location.hash
;
/*-- replace() puts the good page in the history instead of the
bad page.
*/
window.location.replace (newURL);
}
这篇关于通过Greasemonkey/Tampermonkey/Userscript将参数添加到URL(重定向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!