diff options
| author | SimonBrazell <simon@brazell.com.au> | 2020-07-12 11:26:48 +1000 |
|---|---|---|
| committer | SimonBrazell <simon@brazell.com.au> | 2020-07-12 20:25:31 +1000 |
| commit | 745c5babcf9104924631185b2ea3f235c45d270a (patch) | |
| tree | 457df4cd62a541e1b65a82eb1ca2f9b1f28fbd06 /assets/javascript/remove-twitter-sw.js | |
| parent | 0892d0c30ac973b2f01eb678af88f4dd0fc8531b (diff) | |
| download | privacy-redirect-745c5babcf9104924631185b2ea3f235c45d270a.tar.gz privacy-redirect-745c5babcf9104924631185b2ea3f235c45d270a.tar.bz2 privacy-redirect-745c5babcf9104924631185b2ea3f235c45d270a.zip | |
Improve exceptions (whitelist), i18n (fr), etc.
- Closes #69
- Closes #70
- Fixes #71
- Closes #72 - added fr l10n
- Fixes #73
- Implement additional Invidious params (#66)
Diffstat (limited to 'assets/javascript/remove-twitter-sw.js')
| -rw-r--r-- | assets/javascript/remove-twitter-sw.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/assets/javascript/remove-twitter-sw.js b/assets/javascript/remove-twitter-sw.js new file mode 100644 index 0000000..d9d3ce3 --- /dev/null +++ b/assets/javascript/remove-twitter-sw.js @@ -0,0 +1,71 @@ +'use strict'; + +const nitterDefault = 'https://nitter.net'; + +let disableNitter; +let nitterInstance; +let redirectBypassFlag; +let exceptions; + +window.browser = window.browser || window.chrome; + +function isNotException(url) { + return !exceptions.some(regex => (regex.test(url.href))); +} + +function shouldRedirect(url) { + return !redirectBypassFlag && + isNotException(url) && + !disableNitter && + url.host !== nitterInstance && + !url.pathname.includes('/home'); +} + +function redirectTwitter(url) { + if (url.host.split('.')[0] === 'pbs') { + return `${nitterInstance}/pic/${encodeURIComponent(url.href)}`; + } else if (url.host.split('.')[0] === 'video') { + return `${nitterInstance}/gif/${encodeURIComponent(url.href)}`; + } else { + return `${nitterInstance}${url.pathname}${url.search}`; + }; +} + +browser.storage.sync.get( + [ + 'nitterInstance', + 'disableNitter', + 'removeTwitterSW', + 'redirectBypassFlag', + 'exceptions' + ], + (result) => { + redirectBypassFlag = result.redirectBypassFlag; + browser.storage.sync.set({ + redirectBypassFlag: false + }); + if (!result.removeTwitterSW) { + disableNitter = result.disableNitter; + nitterInstance = result.nitterInstance || nitterDefault; + exceptions = result.exceptions ? result.exceptions.map(e => { + return new RegExp(e); + }) : []; + navigator.serviceWorker.getRegistrations().then(registrations => { + for (let registration of registrations) { + if (registration.scope === 'https://twitter.com/') { + registration.unregister(); + console.log('Unregistered Twitter SW', registration); + } + } + }); + const url = new URL(window.location); + if (shouldRedirect()) { + const redirect = redirectTwitter(url); + console.info( + 'Redirecting', `"${url.href}"`, '=>', `"${redirect}"` + ); + window.location = redirect; + } + } + } +); |
