diff options
Diffstat (limited to 'assets/javascript')
-rw-r--r-- | assets/javascript/localise.js | 19 | ||||
-rw-r--r-- | assets/javascript/persist-invidious-prefs.js | 30 | ||||
-rw-r--r-- | assets/javascript/remove-twitter-sw.js | 71 |
3 files changed, 120 insertions, 0 deletions
diff --git a/assets/javascript/localise.js b/assets/javascript/localise.js new file mode 100644 index 00000000..e408025d --- /dev/null +++ b/assets/javascript/localise.js @@ -0,0 +1,19 @@ +window.browser = window.browser || window.chrome; + +function localizeHtmlPage() { + // Localize using __MSG_***__ data tags + var data = document.querySelectorAll('[data-localize]'); + + for (var i in data) if (data.hasOwnProperty(i)) { + var obj = data[i]; + var tag = obj.getAttribute('data-localize').toString(); + + var msg = tag.replace(/__MSG_(\w+)__/g, function (_match, v1) { + return v1 ? browser.i18n.getMessage(v1) : null; + }); + + if (msg && msg !== tag) obj.innerHTML = msg; + } +} + +localizeHtmlPage(); \ No newline at end of file diff --git a/assets/javascript/persist-invidious-prefs.js b/assets/javascript/persist-invidious-prefs.js new file mode 100644 index 00000000..4c13a310 --- /dev/null +++ b/assets/javascript/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/javascript/remove-twitter-sw.js b/assets/javascript/remove-twitter-sw.js new file mode 100644 index 00000000..d9d3ce3e --- /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; + } + } + } +); |