Greasemonkey:向URL添加参数 [英] Greasemonkey: add parameters to URL
本文介绍了Greasemonkey:向URL添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个Greasemonkey / userscript,自动将 .compact
添加到以 https://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.
如何编写一个从<$ c $重定向的greasemonkey脚本c> https://pay.reddit.com/* 到 https://pay.reddit.com/*.compact
?
谢谢
推荐答案
脚本应该做这些事情:
- 检测当前URL是否已经到达紧凑型站点。
- 如有必要,请加载页面的精简版本。
- 小心锚网址(以片段或哈希(
#...
)) - 将不需要的页面保留在浏览器历史记录之外,以便后退按钮正常工作。只会记住
.compact
网址。 - 运行
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:向URL添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文