通过 Greasemonkey/Tampermonkey/Userscript 将参数添加到 URL(重定向) [英] Add parameters to the URL (redirect) via a Greasemonkey/Tampermonkey/Userscript

查看:37
本文介绍了通过 Greasemonkey/Tampermonkey/Userscript 将参数添加到 URL(重定向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 Greasemonkey/用户脚本,自动将 .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.

如何编写将我从 https://pay.reddit.com/* 重定向到 https://pay.reddit.com/*.compact<的 Greasemonkey 脚本/代码> ?

How do I write a Greasemonkey script that redirects me from https://pay.reddit.com/* to https://pay.reddit.com/*.compact ?

谢谢

推荐答案

脚本应该做这些事情:

  1. 检测当前 URL 是否已经指向压缩站点.
  2. 如有必要,加载页面的精简版本.
  3. 当心锚"网址(它们以 片段"或哈希"结尾(#...) ) 并对它们进行说明.
  4. 将不需要的页面从浏览器历史记录中排除,以便后退按钮正常工作.只会记住 .compact URL.
  5. 通过在 document-start 运行,脚本可以在这种情况下提供更好的性能.
  1. Detect if the current URL is already to the compact site.
  2. Load the compact version of the page if necessary.
  3. Beware of "anchor" URLS (they end with "fragments" or "hashes" (#...) ) and account for them.
  4. Keep the unwanted pages out of the browser history so that the back button works well. Only .compact URL's will be remembered.
  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆