From 71cc6c45bf0571765209847baa1555e0b1b33ade Mon Sep 17 00:00:00 2001 From: Dave G Date: Mon, 27 Dec 2021 21:49:38 -0500 Subject: [PATCH] Use named capture groups --- README.md | 7 +++++++ script.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 README.md create mode 100644 script.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..32917b0 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# rfd-redirect-stripper + +This is a [Tampermonkey](https://www.tampermonkey.net/) userscript that strips affiliate redirects from deal links posted on https://forums.redflagdeals.com. + +## Why? + +This can help prevent broken links from collisions with adblockers. I wouldn't suggest using this unless you are experiencing difficulties opening links. Affiliate links and referrals help support RedFlagDeals.com. diff --git a/script.js b/script.js new file mode 100644 index 0000000..106769a --- /dev/null +++ b/script.js @@ -0,0 +1,51 @@ +// ==UserScript== +// @author Dave Gallant +// @description Strip redirect links on forums.redflagdeals.com +// @downloadURL https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/script.js +// @grant none +// @match *://forums.redflagdeals.com/* +// @name RedFlagDeals Redirect Stripper +// @namespace http://tampermonkey.net/ +// @updateURL https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/script.js +// @version 0.0.1 +// ==/UserScript== + +(function() { + 'use strict'; + + var Links = document.querySelectorAll('a.postlink, a.autolinker_link'); + + const REDIRECT_REGEX = [ + { + name: 'Amazon', + pattern: 'www.amazon.ca/gp/redirect.html\\?ie=UTF8&location=(?.*?)(&|ref%3D|%3F)', + }, + { + name: 'Best Buy', + pattern: 'bestbuyca.(.*).net(.*)\\?u=(?.*)', + }, + ]; + + var StripRedirect = function(URL) { + for (var i = 0; i < REDIRECT_REGEX.length; i++) { + var rule = REDIRECT_REGEX[i]; + var result = new RegExp(rule.pattern).exec(URL); + if (result) { + var newURL = result.groups.baseUrl; + try { + return decodeURIComponent(newURL); + } catch (e) { + console.log(e); + return URL; + } + } + } + return URL; + }; + + Links.forEach(function(Link) { + var ReferralURL = Link.href; + Link.href = StripRedirect(ReferralURL); + }); + +})();