Greasemonkey:向URL添加参数

编程入门 行业动态 更新时间:2024-10-14 14:15:14
本文介绍了Greasemonkey:向URL添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想编写一个Greasemonkey / userscript,自动将 pact 添加到以 pay.reddit/ 所以它会自动将我重定向到移动版。

I'd like to write a Greasemonkey/userscript that automatically adds pact to URLs starting with pay.reddit/ so It automatically redirects me to the mobile version.

我去过查看类似的用户脚本,特别是这个用户脚本: userscripts/scripts/review/112568试图弄清楚如何编辑替换模式,但我缺乏这个领域的技能。

I've been looking at similar userscripts, particularly this one: userscripts/scripts/review/112568 trying to figure out how to edit the replacement pattern, but I lack skills in this domain.

如何编写一个从 pay.reddit/* 到 pay.reddit/*pact ?

谢谢

推荐答案

脚本应该做这些事情:

  • 检测当前URL是否已经到达紧凑型站点。
  • 如有必要,请加载页面的精简版本。
  • 小心锚网址(以片段或哈希(#... ))
  • 将不需要的页面保留在浏览器历史记录之外,以便后退按钮正常工作。只会记住 pact 网址。
  • 运行 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 pact 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/* // @run-at document-start // @grant none // ==/UserScript== var oldUrlPath = window.location.pathname; /*--- Test that "pact" is at end of URL, excepting any "hashes" or searches. */ if ( ! /\pact$/.test (oldUrlPath) ) { var newURL = window.location.protocol + "//" + window.location.host + oldUrlPath + "pact" + 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添加参数

    本文发布于:2023-11-26 02:22:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1632268.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:参数   Greasemonkey   URL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!