6

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.

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.

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

Thanks

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
mp04
  • 160
  • 1
  • 1
  • 6

2 Answers2

12

The script should do these things:

  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.

To that end, this script works:

// ==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);
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Well done! Userscript uploaded to https://userscripts.org/scripts/show/133891 . Many thanks – mp04 May 21 '12 at 10:31
  • Found a use case where it doesn't work: submitting links to reddit with the bookmarklet. Go to any page, and click your 'submit on reddit' bookmarklet (code : `javascript:location.href='http://www.reddit.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)`) and it's stuck in a loop, keeps adding .compact.compact.. and so on at the end of the URL. Minor glitch, though. – mp04 May 21 '12 at 10:37
  • Oops! My bad, I don't actually use Reddit, and didn't test/consider the `search` component of the URL. Corrected that in the updated answer. – Brock Adams May 21 '12 at 12:10
0

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.

RichardTowers
  • 4,682
  • 1
  • 26
  • 43
  • looks like it's not working. I've installed and enabled the userscript in greasemonkey, but it does not redirect me (test url: https://pay.reddit.com/r/linux/). I've uploaded the script to https://pad.lqdn.fr/p/redditmobileredirect if you want to check what could be wrong, maybe some greasemonkey-specific trick? – mp04 May 20 '12 at 17:40
  • Sorry, my fault for posting untested code. See updated answer. – RichardTowers May 20 '12 at 17:48
  • Almost there... the redirection works, but it keeps redirecting after the correct URL has been reached. I end up with something like https://pay.reddit.com/r/linux.compact.compact.compact.compact.compact and so on. Btw, thanks for updating your answer – mp04 May 20 '12 at 19:45
  • The `location.href` is supposed to be read-only in some browsers. Using `location` to redirect would be better cross-browser. See http://stackoverflow.com/a/275102/1331430. Also you just have to add a check in the beginning of your script to prevent it from triggering multiple times: `if (window.location.href.toString().indexOf(".compact") != -1) return false;` – Fabrício Matté May 20 '12 at 23:25
  • Answer updated again to prevent multiple redirects. All in all though, I think @Brock Adams' solution is more robust. – RichardTowers May 21 '12 at 06:45
  • Upvoted @Brock Adam's script, it's faster (doesn't even load the bad page before redirecting) and addresses some side issues like putting the bad page in history. Thanks to you both, very instructive answers! – mp04 May 21 '12 at 10:16