Add parameters to the URL (redirect) via a Greasemonkey/Tampermonkey/Userscript

后端 未结 2 1787
长情又很酷
长情又很酷 2020-11-28 15:08

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

2条回答
  •  渐次进展
    2020-11-28 15:26

    The example script you showed is using a regex to manipulate the window's location:

    replace(/^https?:\/\/(www\.)?twitter.com/, 'https://mobile.twitter.com');
    

    Unsurprisingly, this replaces https://www.twitter.com and http://twitter.com etc. with https://mobile.twitter.com.

    Your situation is slightly different, because you want to append a string to your url if it matches some regex. Try:

    var url = window.location.href;
    var redditPattern = /^https:\/\/pay.reddit.com\/.*/;
    // Edit: To prevent multiple redirects:
    var compactPattern = /\.compact/;
    if (redditPattern.test(url)
        && !compactPattern.test(url)) {
        window.location.href = url + '.compact';
    }
    

    See: http://jsfiddle.net/RichardTowers/4VjdZ/3 for test case.

提交回复
热议问题