diff --git a/README.md b/README.md index 58e43f7..7bbb025 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,22 @@ This was originally a [Tampermonkey](https://www.tampermonkey.net/) userscript b Copy [script.js](./script.js) into Tampermonkey. -## Chrome extension +## Browser Extensions -To load the browser extension, clone this repo and load [extensions/chrome](./extensions/chrome/). +The browser extensions are currently not packaged or published anywhere. + +### Chrome + +To load the chrome extension, clone this repo and load [extensions/chrome](./extensions/chrome/). + +### Firefox + +To load the firefox extension, clone this repo and load [extensions/firefox](./extensions/firefox/). + +Go to `about:addons`, and ensure that all permissions are granted. If this is not done, the extension will not have permission to execute content scripts. ## Updating redirects -The chrome extension will update itself by periodically fetching the latest [redirects.json](redirects.json). +The browser extensions will update the list of redirects by periodically fetching the latest [redirects.json](redirects.json). Open a pull request to this repo to update the redirects. diff --git a/extensions/firefox/background.js b/extensions/firefox/background.js new file mode 100644 index 0000000..78b6fd8 --- /dev/null +++ b/extensions/firefox/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) => { + browser.storage.local.set({ + "rfd-redirects": res, + }); + }); +} + +chrome.runtime.onInstalled.addListener(() => { + updateRedirects(); +}); + +setInterval(updateRedirects, 1 * 60 * 60 * 1000); diff --git a/extensions/firefox/content.js b/extensions/firefox/content.js new file mode 100644 index 0000000..8946a53 --- /dev/null +++ b/extensions/firefox/content.js @@ -0,0 +1,32 @@ +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"); + + browser.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/firefox/icon.png b/extensions/firefox/icon.png new file mode 100644 index 0000000..ca71db2 Binary files /dev/null and b/extensions/firefox/icon.png differ diff --git a/extensions/firefox/manifest.json b/extensions/firefox/manifest.json new file mode 100644 index 0000000..8bb31ca --- /dev/null +++ b/extensions/firefox/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": { + "scripts": ["background.js"] + }, + "permissions": ["scripting", "storage"], + "host_permissions": ["*://forums.redflagdeals.com/*"] +}