diff options
author | SimonBrazell <simon@brazell.com.au> | 2020-05-08 22:40:07 +1000 |
---|---|---|
committer | SimonBrazell <simon@brazell.com.au> | 2020-05-08 22:40:07 +1000 |
commit | 8f82745733ed063a9a14d3176abb59160ded0bc9 (patch) | |
tree | d1743a74d10aa665a978fbcf171ef15122179310 /assets | |
parent | Fixes #45 - some usersnames caught in instagramReservedPaths (diff) | |
download | libredirect-8f82745733ed063a9a14d3176abb59160ded0bc9.zip |
Closes #36 - added option to persist Invidious prefs
Diffstat (limited to 'assets')
-rw-r--r-- | assets/persist-invidious-prefs.js | 30 | ||||
-rw-r--r-- | assets/remove-twitter-sw.js | 44 |
2 files changed, 74 insertions, 0 deletions
diff --git a/assets/persist-invidious-prefs.js b/assets/persist-invidious-prefs.js new file mode 100644 index 00000000..4c13a310 --- /dev/null +++ b/assets/persist-invidious-prefs.js @@ -0,0 +1,30 @@ +'use strict'; + +window.browser = window.browser || window.chrome; + +function getCookie() { + let ca = document.cookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1, c.length); + if (c.indexOf('PREFS=') == 0) { + return JSON.parse( + decodeURIComponent(c.substring('PREFS='.length, c.length)) + ) + }; + } + return {}; +} + +browser.storage.sync.get( + ['alwaysProxy', 'videoQuality', 'invidiousDarkMode', 'persistInvidiousPrefs'], + (result) => { + if (result.persistInvidiousPrefs) { + const prefs = getCookie(); + prefs.local = result.alwaysProxy; + prefs.quality = result.videoQuality; + prefs.dark_mode = result.invidiousDarkMode; + document.cookie = `PREFS=${encodeURIComponent(JSON.stringify(prefs))}`; + } + } +); \ No newline at end of file diff --git a/assets/remove-twitter-sw.js b/assets/remove-twitter-sw.js new file mode 100644 index 00000000..d13de3e1 --- /dev/null +++ b/assets/remove-twitter-sw.js @@ -0,0 +1,44 @@ +'use strict'; + +const nitterDefault = 'https://nitter.net'; + +let disableNitter; +let nitterInstance; + +window.browser = window.browser || window.chrome; + +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'], + (result) => { + if (!result.removeTwitterSW) { + disableNitter = result.disableNitter; + nitterInstance = result.nitterInstance || nitterDefault; + 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 (!disableNitter && url.host !== nitterInstance) { + const redirect = redirectTwitter(url); + console.info( + 'Redirecting', `"${url.href}"`, '=>', `"${redirect}"` + ); + window.location = redirect; + } + } + } +); |