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
1. `npm install --global web-ext`
2. `web-ext build`
2. `web-ext build --overwrite-dest`
3. See `web-ext-artifacts/` for outputs.
## License

View File

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

View File

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

View File

@ -316,7 +316,7 @@ li {
@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);
color: var(--text-secondary);
}