diff --git a/README.md b/README.md index 32917b0..58e43f7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,23 @@ # 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. +rfd-redirect-stripper 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. +This helps prevent broken links from collisions with adblockers. Only use if you are experiencing difficulties opening links. Affiliate links and referrals help support RedFlagDeals.com. + +## Tampermonkey Script + +This was originally a [Tampermonkey](https://www.tampermonkey.net/) userscript before evolving into a browser extension. + +Copy [script.js](./script.js) into Tampermonkey. + +## Chrome extension + +To load the browser extension, clone this repo and load [extensions/chrome](./extensions/chrome/). + +## Updating redirects + +The chrome extension will update itself by periodically fetching the latest [redirects.json](redirects.json). + +Open a pull request to this repo to update the redirects. diff --git a/extensions/chrome/background.js b/extensions/chrome/background.js new file mode 100644 index 0000000..58cd4de --- /dev/null +++ b/extensions/chrome/background.js @@ -0,0 +1,18 @@ +function updateRedirects() { + var URL = + "https://raw.githubusercontent.com/davegallant/rfd-redirect-stripper/main/redirects.json"; + + fetch(URL) + .then((res) => res.json()) + .then((res) => { + chrome.storage.local.set({ + "rfd-redirects": res, + }); + }); +} + +chrome.runtime.onInstalled.addListener(() => { + updateRedirects(); +}); + +setInterval(updateRedirects, 1 * 60 * 60 * 1000); diff --git a/extensions/chrome/content.js b/extensions/chrome/content.js new file mode 100644 index 0000000..f3a2fb2 --- /dev/null +++ b/extensions/chrome/content.js @@ -0,0 +1,30 @@ +function stripRedirect(URL, redirectRegex) { + for (var i = 0; i < redirectRegex.length; i++) { + var rule = redirectRegex[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; +} + +function stripRedirects() { + var Links = document.querySelectorAll("a.postlink, a.autolinker_link"); + + chrome.storage.local.get("rfd-redirects", function (redirectRegex) { + Links.forEach(function (Link) { + var ReferralURL = Link.href; + + Link.href = stripRedirect(ReferralURL, redirectRegex["rfd-redirects"]); + }); + }); +} + +stripRedirects(); diff --git a/extensions/chrome/icon.png b/extensions/chrome/icon.png new file mode 100644 index 0000000..ca71db2 Binary files /dev/null and b/extensions/chrome/icon.png differ diff --git a/extensions/chrome/manifest.json b/extensions/chrome/manifest.json new file mode 100644 index 0000000..e727c73 --- /dev/null +++ b/extensions/chrome/manifest.json @@ -0,0 +1,21 @@ +{ + "manifest_version": 3, + "name": "rfd-redirect-stripper", + "description": "Strip tracking redirects on rfd", + "version": "0.1", + "content_scripts": [ + { + "matches": ["*://forums.redflagdeals.com/*"], + "js": ["content.js"], + "run_at": "document_end" + } + ], + "action": { + "default_icon": "icon.png" + }, + "background": { + "service_worker": "background.js" + }, + "permissions": ["scripting", "storage"], + "host_permissions": ["*://forums.redflagdeals.com/*"] +}