A simple script that automatically redirects Reddit links to a more privacy-focused alternative. libreddit.freedit.eu
I just tested it and it works well with minimal code wow. I don't have any knowledge in making userscripts. I just googled some basic queries and based my code from various examples. This is a learning example. Thanks.
I could explain it to you.
First of all, I changed match to this:
// @match *://*.reddit.com/*
We are going to check hostname and subdomain later anyway, so we don't need big @match structures.
Then I changed window.location.href to window.location.hostname because we only need test check hostname (bold URL part here: httpsㅤ://reddit.com/r/ProgrammerHumor).
Then using regular expression I was checking if current hostname is hostname we need.
There is little explanation to line 21:
We are checking if the line starts and ends with old.reddit.com, or www.reddit.com, or reddit.com. (Also, it can match .reddit.com, but I that's not a problem because that's invalid URL, so browser won't load it)
I also check if location.replace method exists, if it exists, then I'm using it. It works better in some of the cases, than location.href. But if it doesn't exist in the current version of the browser, then I just use location.href because we have no other choice.
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址
You could make your code shorter, here is a shorter solution: