Closes #64, closes #38 - Fix whitelist & Twitter `/home` exception

This commit is contained in:
SimonBrazell 2020-06-08 11:50:12 +10:00
parent b21610202f
commit 0892d0c30a
5 changed files with 35 additions and 17 deletions

View File

@ -12,7 +12,7 @@ Allows for setting custom [Nitter](https://github.com/zedeus/nitter/wiki/Instanc
## Build ## Build
1. `npm install --global web-ext` 1. `npm install --global web-ext`
2. `web-ext build` 2. `web-ext build --overwrite-dest`
3. See `web-ext-artifacts/` for outputs. 3. See `web-ext-artifacts/` for outputs.
## License ## License

View File

@ -4,9 +4,23 @@ const nitterDefault = 'https://nitter.net';
let disableNitter; let disableNitter;
let nitterInstance; let nitterInstance;
let redirectBypassFlag;
let whitelist;
window.browser = window.browser || window.chrome; window.browser = window.browser || window.chrome;
function isNotWhitelisted(url) {
return !whitelist.some(regex => (regex.test(url.href)));
}
function shouldRedirect(url) {
return !redirectBypassFlag &&
isNotWhitelisted(url) &&
!disableNitter &&
url.host !== nitterInstance &&
!url.pathname.includes('/home');
}
function redirectTwitter(url) { function redirectTwitter(url) {
if (url.host.split('.')[0] === 'pbs') { if (url.host.split('.')[0] === 'pbs') {
return `${nitterInstance}/pic/${encodeURIComponent(url.href)}`; return `${nitterInstance}/pic/${encodeURIComponent(url.href)}`;
@ -20,13 +34,16 @@ function redirectTwitter(url) {
browser.storage.sync.get( browser.storage.sync.get(
['nitterInstance', 'disableNitter', 'removeTwitterSW', 'redirectBypassFlag'], ['nitterInstance', 'disableNitter', 'removeTwitterSW', 'redirectBypassFlag'],
(result) => { (result) => {
const redirectBypassFlag = result.redirectBypassFlag; redirectBypassFlag = result.redirectBypassFlag;
browser.storage.sync.set({ browser.storage.sync.set({
redirectBypassFlag: false redirectBypassFlag: false
}); });
if (!result.removeTwitterSW) { if (!result.removeTwitterSW) {
disableNitter = result.disableNitter; disableNitter = result.disableNitter;
nitterInstance = result.nitterInstance || nitterDefault; nitterInstance = result.nitterInstance || nitterDefault;
whitelist = result.whitelist ? result.whitelist.map(e => {
return new RegExp(e.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
}) : [];
navigator.serviceWorker.getRegistrations().then(registrations => { navigator.serviceWorker.getRegistrations().then(registrations => {
for (let registration of registrations) { for (let registration of registrations) {
if (registration.scope === 'https://twitter.com/') { if (registration.scope === 'https://twitter.com/') {
@ -36,7 +53,7 @@ browser.storage.sync.get(
} }
}); });
const url = new URL(window.location); const url = new URL(window.location);
if (!redirectBypassFlag && !disableNitter && url.host !== nitterInstance && !url.pathname.includes('/home')) { if (shouldRedirect()) {
const redirect = redirectTwitter(url); const redirect = redirectTwitter(url);
console.info( console.info(
'Redirecting', `"${url.href}"`, '=>', `"${redirect}"` 'Redirecting', `"${url.href}"`, '=>', `"${redirect}"`

View File

@ -86,7 +86,6 @@ let onlyEmbeddedVideo;
let videoQuality; let videoQuality;
let invidiousDarkMode; let invidiousDarkMode;
let whitelist; let whitelist;
let redirectBypassFlag;
window.browser = window.browser || window.chrome; window.browser = window.browser || window.chrome;
@ -119,7 +118,9 @@ browser.storage.sync.get(
onlyEmbeddedVideo = result.onlyEmbeddedVideo; onlyEmbeddedVideo = result.onlyEmbeddedVideo;
videoQuality = result.videoQuality; videoQuality = result.videoQuality;
invidiousDarkMode = result.invidiousDarkMode; invidiousDarkMode = result.invidiousDarkMode;
whitelist = result.whitelist ? result.whitelist.map(e => new RegExp(e)) : []; whitelist = result.whitelist ? result.whitelist.map(e => {
return new RegExp(e.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
}) : [];
} }
); );
@ -161,10 +162,9 @@ browser.storage.onChanged.addListener(changes => {
invidiousDarkMode = changes.invidiousDarkMode.newValue; invidiousDarkMode = changes.invidiousDarkMode.newValue;
} }
if ('whitelist' in changes) { if ('whitelist' in changes) {
whitelist = changes.whitelist.newValue.map(e => new RegExp(e)); whitelist = changes.whitelist.newValue.map(e => {
} return new RegExp(e.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
if ('redirectBypassFlag' in changes) { });
redirectBypassFlag = changes.redirectBypassFlag.newValue;
} }
}); });
@ -194,8 +194,9 @@ function addressToLatLng(address, callback) {
xmlhttp.send(); xmlhttp.send();
} }
function isWhitelisted(initiator) { function isWhitelisted(url, initiator) {
return initiator && whitelist.some(regex => (regex.test(initiator.href))); return whitelist.some(regex => (regex.test(url.href))) ||
(initiator && whitelist.some(regex => (regex.test(initiator.href))));
} }
function isFirefox() { function isFirefox() {
@ -203,7 +204,7 @@ function isFirefox() {
} }
function redirectYouTube(url, initiator, type) { function redirectYouTube(url, initiator, type) {
if (disableInvidious || isWhitelisted(initiator)) { if (disableInvidious || isWhitelisted(url, initiator)) {
return null; return null;
} }
if (initiator && (initiator.origin === invidiousInstance || youtubeDomains.includes(initiator.host))) { if (initiator && (initiator.origin === invidiousInstance || youtubeDomains.includes(initiator.host))) {
@ -234,7 +235,7 @@ function redirectYouTube(url, initiator, type) {
} }
function redirectTwitter(url, initiator) { function redirectTwitter(url, initiator) {
if (disableNitter || isWhitelisted(initiator)) { if (disableNitter || isWhitelisted(url, initiator)) {
return null; return null;
} }
if (url.pathname.includes('/home')) { if (url.pathname.includes('/home')) {
@ -258,7 +259,7 @@ function redirectTwitter(url, initiator) {
} }
function redirectInstagram(url, initiator, type) { function redirectInstagram(url, initiator, type) {
if (disableBibliogram || isWhitelisted(initiator)) { if (disableBibliogram || isWhitelisted(url, initiator)) {
return null; return null;
} }
// Do not redirect Bibliogram view on Instagram links // Do not redirect Bibliogram view on Instagram links
@ -278,7 +279,7 @@ function redirectInstagram(url, initiator, type) {
} }
function redirectGoogleMaps(url, initiator) { function redirectGoogleMaps(url, initiator) {
if (disableOsm || isWhitelisted(initiator)) { if (disableOsm || isWhitelisted(url, initiator)) {
return null; return null;
} }
let redirect; let redirect;

View File

@ -1,7 +1,7 @@
{ {
"name": "Privacy Redirect", "name": "Privacy Redirect",
"description": "Redirects Twitter, YouTube, Instagram & Google Maps requests to privacy friendly alternatives.", "description": "Redirects Twitter, YouTube, Instagram & Google Maps requests to privacy friendly alternatives.",
"version": "1.1.33", "version": "1.1.34",
"manifest_version": 2, "manifest_version": 2,
"background": { "background": {
"scripts": [ "scripts": [

View File

@ -316,7 +316,7 @@ li {
@media (prefers-color-scheme: light) { @media (prefers-color-scheme: light) {
body.popup, header, h1, input, select, div.tabcontent, button.tablinks.active { body.popup, header, h1, input, select, div.tabcontent, button.tablinks.active, ul {
background-color: var(--lighter); background-color: var(--lighter);
color: var(--text-secondary); color: var(--text-secondary);
} }