libredirect/background.js

175 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-10-07 13:59:31 +02:00
'use strict';
const invidiousDefault = 'https://invidio.us';
const youtubeRegex = /((www|m)\.)?youtube|ytimg(-nocookie)?\.com/;
const nitterDefault = 'https://nitter.net';
const twitterRegex = /((www|mobile)\.)?twitter\.com/;
2020-02-01 04:17:51 +01:00
const bibliogramDefault = 'https://bibliogram.art';
const instagramRegex = /((www|about|help)\.)?instagram\.com/;
const instagramPathsRegex = /(\/a|\/admin|\/api|\/favicon.ico|\/static|\/imageproxy|\/p|\/u|\/developer|\/about|\/legal|\/explore|\/director)/;
2020-02-23 06:19:32 +01:00
const osmDefault = 'https://openstreetmap.org';
2020-02-24 09:22:10 +01:00
const googleMapsRegex = /https?:\/\/(((www|maps)\.)?(google).*(\/maps)|maps\.(google).*)/;
2020-02-23 06:19:32 +01:00
const latLngZoomRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/;
const dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g;
2019-10-07 13:59:31 +02:00
let nitterInstance;
let invidiousInstance;
2020-02-01 04:17:51 +01:00
let bibliogramInstance;
2020-02-23 06:19:32 +01:00
let osmInstance;
2019-10-07 13:59:31 +02:00
let disableNitter;
let disableInvidious;
2020-02-01 04:17:51 +01:00
let disableBibliogram;
2020-02-23 06:19:32 +01:00
let disableOsm;
2019-10-07 13:59:31 +02:00
2020-02-24 04:19:56 +01:00
window.browser = window.browser || window.chrome;
browser.storage.sync.get(
2020-02-01 04:17:51 +01:00
[
'nitterInstance',
'invidiousInstance',
'bibliogramInstance',
2020-02-23 06:19:32 +01:00
'osmInstance',
2020-02-01 04:17:51 +01:00
'disableNitter',
'disableInvidious',
'disableBibliogram',
2020-02-23 06:19:32 +01:00
'disableOsm'
2020-02-01 04:17:51 +01:00
],
2020-01-14 10:48:37 +01:00
result => {
2019-10-07 13:59:31 +02:00
disableNitter = result.disableNitter;
disableInvidious = result.disableInvidious;
2020-02-01 04:17:51 +01:00
disableBibliogram = result.disableBibliogram;
2020-02-23 06:19:32 +01:00
disableOsm = result.disableOsm;
2019-10-07 13:59:31 +02:00
nitterInstance = result.nitterInstance || nitterDefault;
invidiousInstance = result.invidiousInstance || invidiousDefault;
2020-02-01 04:17:51 +01:00
bibliogramInstance = result.bibliogramInstance || bibliogramDefault;
2020-02-23 06:19:32 +01:00
osmInstance = result.osmInstance || osmDefault;
2019-10-07 13:59:31 +02:00
}
);
2020-02-24 04:19:56 +01:00
browser.storage.onChanged.addListener(changes => {
2019-10-07 13:59:31 +02:00
if ('nitterInstance' in changes) {
nitterInstance = changes.nitterInstance.newValue || nitterDefault;
}
if ('invidiousInstance' in changes) {
invidiousInstance = changes.invidiousInstance.newValue || invidiousDefault;
}
2020-02-01 04:17:51 +01:00
if ('bibliogramInstance' in changes) {
bibliogramInstance = changes.bibliogramInstance.newValue || bibliogramDefault;
}
2020-02-23 06:19:32 +01:00
if ('osmInstance' in changes) {
osmInstance = changes.osmInstance.newValue || osmDefault;
}
2019-10-07 13:59:31 +02:00
if ('disableNitter' in changes) {
disableNitter = changes.disableNitter.newValue;
}
if ('disableInvidious' in changes) {
disableInvidious = changes.disableInvidious.newValue;
}
2020-02-01 04:17:51 +01:00
if ('disableBibliogram' in changes) {
disableBibliogram = changes.disableBibliogram.newValue;
}
2020-02-23 06:19:32 +01:00
if ('disableOsm' in changes) {
disableOsm = changes.disableOsm.newValue;
}
2019-10-07 13:59:31 +02:00
});
2019-09-20 12:45:58 +02:00
function redirectYouTube(url) {
if (url.host.split('.')[0] === 'studio') {
// Avoid redirecting `studio.youtube.com`
return null;
} else if (url.pathname.match(/iframe_api/)) {
// Redirect requests for YouTube Player API to local files instead
2020-02-24 04:19:56 +01:00
return browser.runtime.getURL('assets/iframe_api.js');
} else if (url.pathname.match(/www-widgetapi/)) {
// Redirect requests for YouTube Player API to local files instead
2020-02-24 04:19:56 +01:00
return browser.runtime.getURL('assets/www-widgetapi.js');
} else {
// Proxy video through the server
url.searchParams.append('local', true);
return `${invidiousInstance}${url.pathname}${url.search}`;
}
}
function redirectTwitter(url) {
if (url.host.split('.')[0] === 'tweetdeck') {
// Avoid redirecting `tweetdeck.twitter.com`
return null;
} else {
return `${nitterInstance}${url.pathname}${url.search}`;
}
}
function redirectInstagram(url) {
2020-02-01 04:17:51 +01:00
if (url.pathname === '/' || url.pathname.match(instagramPathsRegex)) {
return `${bibliogramInstance}${url.pathname}${url.search}`;
2020-02-01 04:17:51 +01:00
} else {
// Redirect user profile requests to '/u/...'
2020-02-06 00:19:05 +01:00
return `${bibliogramInstance}/u${url.pathname}${url.search}`;
2020-02-01 04:17:51 +01:00
}
}
2020-02-24 04:19:56 +01:00
function redirectGoogleMaps(url) {
// Do not redirect embedded maps
if (url.pathname.includes('/embed')) {
return;
}
2020-02-24 09:22:10 +01:00
let mapCentre = '';
2020-02-23 06:19:32 +01:00
if (url.pathname.match(latLngZoomRegex)) {
[, lat, lon, zoom] = url.pathname.match(latLngZoomRegex);
2020-02-24 09:22:10 +01:00
mapCentre = `#map=${zoom}/${lat}/${lon}`;
2020-02-23 06:19:32 +01:00
}
2020-02-24 04:19:56 +01:00
if (url.pathname.includes('data=')) {
const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
2020-02-24 09:22:10 +01:00
return `${osmInstance}/?mlat=${mlat.replace('!3d', '')}&mlon=${mlon.replace('!4d', '')}${mapCentre}`;
} else if (url.search.includes('ll=')) {
const [mlat, mlon] = url.searchParams.get('ll').split(',');
return `${osmInstance}/?mlat=${mlat}&mlon=${mlon}${mapCentre}`;
2020-02-24 04:19:56 +01:00
} else {
2020-02-24 09:22:10 +01:00
const query = url.searchParams.get('q') || url.searchParams.get('query') || url.pathname.split('/')[3];
return `${osmInstance}/search?query=${encodeURI(query)}${mapCentre}`;
2020-02-23 06:19:32 +01:00
}
}
2020-02-24 04:19:56 +01:00
browser.webRequest.onBeforeRequest.addListener(
2020-01-14 10:48:37 +01:00
details => {
2020-02-01 04:17:51 +01:00
const url = new URL(details.url);
2020-01-14 10:48:37 +01:00
let redirect;
2020-02-01 04:17:51 +01:00
if (url.host.match(youtubeRegex)) {
2019-10-07 13:59:31 +02:00
if (!disableInvidious) {
2020-01-14 10:48:37 +01:00
redirect = {
redirectUrl: redirectYouTube(url)
2019-10-07 13:59:31 +02:00
};
}
2020-02-01 04:17:51 +01:00
} else if (url.host.match(twitterRegex)) {
2019-10-07 13:59:31 +02:00
if (!disableNitter) {
2020-01-14 10:48:37 +01:00
redirect = {
redirectUrl: redirectTwitter(url)
2020-02-01 04:17:51 +01:00
};
}
} else if (url.host.match(instagramRegex)) {
if (!disableBibliogram) {
redirect = {
redirectUrl: redirectInstagram(url)
2019-10-07 13:59:31 +02:00
};
}
2020-02-23 06:19:32 +01:00
} else if (url.href.match(googleMapsRegex)) {
if (!disableOsm) {
redirect = {
2020-02-24 04:19:56 +01:00
redirectUrl: redirectGoogleMaps(url)
2020-02-23 06:19:32 +01:00
};
}
2019-09-20 12:45:58 +02:00
}
if (redirect && redirect.redirectUrl) {
2020-02-01 04:17:51 +01:00
console.log(
'Redirecting', `"${url.href}"`, '=>', `"${redirect.redirectUrl}"`
2020-02-01 04:17:51 +01:00
);
2020-01-14 10:48:37 +01:00
console.log('Details', details);
}
return redirect;
2019-09-20 12:45:58 +02:00
},
{
2020-02-24 04:19:56 +01:00
urls: ["<all_urls>"]
2019-09-20 12:45:58 +02:00
},
2020-01-14 10:48:37 +01:00
['blocking']
2019-09-20 12:45:58 +02:00
);