Proactively remove Twitter servie worker

This commit is contained in:
SimonBrazell 2020-03-29 11:34:46 +11:00
parent 1f205990eb
commit f5eac66d70
4 changed files with 77 additions and 2 deletions

44
content-script.js Normal file
View File

@ -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;
}
}
}
);

View File

@ -1,7 +1,7 @@
{
"name": "Privacy Redirect",
"description": "Redirects Twitter, YouTube, Instagram & Google Maps requests to privacy friendly alternatives.",
"version": "1.1.16",
"version": "1.1.17",
"manifest_version": 2,
"background": {
"scripts": [
@ -31,6 +31,21 @@
"128": "images/icon128.png"
}
},
"content_scripts": [
{
"matches": [
"*://twitter.com/*",
"*://www.twitter.com/*",
"*://mobile.twitter.com/*",
"*://pbs.twimg.com/*",
"*://video.twimg.com/*"
],
"js": [
"content-script.js"
],
"run_at": "document_start"
}
],
"options_ui": {
"page": "pages/options/options.html",
"open_in_tab": false

View File

@ -111,6 +111,15 @@
</select>
</section>
<section class="options settings_block">
<div class="onoffswitch switch" aria-label="Proactively remove Twitter service worker">
<h1>Proactively remove Twitter service worker</h1>
<input aria-hidden="true" id="remove-twitter-sw" type="checkbox" checked>&nbsp;
<label for="remove-twitter-sw" class="checkbox-label">
</label>
</div>
</section>
<script src="./options.js"></script>
</body>

View File

@ -11,6 +11,7 @@ let disableOsm = document.querySelector('#disable-osm');
let alwaysProxy = document.querySelector('#always-proxy');
let onlyEmbeddedVideo = document.querySelector('#only-embed');
let videoQuality = document.querySelector('#video-quality');
let removeTwitterSW = document.querySelector('#remove-twitter-sw');
window.browser = window.browser || window.chrome;
@ -26,7 +27,8 @@ browser.storage.sync.get(
'disableOsm',
'alwaysProxy',
'onlyEmbeddedVideo',
'videoQuality'
'videoQuality',
'removeTwitterSW'
],
result => {
nitterInstance.value = result.nitterInstance || '';
@ -40,6 +42,7 @@ browser.storage.sync.get(
alwaysProxy.checked = result.alwaysProxy;
onlyEmbeddedVideo.checked = result.onlyEmbeddedVideo;
videoQuality.value = result.videoQuality || '';
removeTwitterSW.checked = !result.removeTwitterSW;
}
);
@ -123,3 +126,7 @@ videoQuality.addEventListener('change', event => {
videoQuality: event.target.options[videoQuality.selectedIndex].value
});
});
removeTwitterSW.addEventListener('change', event => {
browser.storage.sync.set({ removeTwitterSW: !event.target.checked });
});