libredirect/background.js

234 lines
7.2 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';
const googleMapsRegex = /(google).*(\/maps)/;
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
chrome.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-01-14 10:48:37 +01:00
chrome.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
2020-02-23 06:19:32 +01:00
// https://github.com/tankaru/ReplaceG2O
function deg2rad(deg) {
return deg / 360 * 2 * Math.PI;
}
function rad2deg(rad) {
return rad / 2 / Math.PI * 360.0;
}
function getTileNumber(lat, lon, zoom) {
xtile = Math.round((lon + 180.0) / 360.0 * 2 ** zoom);
ytile = Math.round((1 - Math.log(Math.tan(deg2rad(lat)) + 1 / (Math.cos(deg2rad(lat)))) / Math.PI) / 2 * 2 ** zoom);
return [xtile, ytile];
}
function getLonLat(xtile, ytile, zoom) {
n = 2 ** zoom;
lon = xtile / n * 360.0 - 180.0;
lat = rad2deg(Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n))));
return [lon, lat];
}
function LonLat_to_bbox(lat, lon, zoom) {
width = 600;
height = 450;
tile_size = 256;
[xtile, ytile] = getTileNumber(lat, lon, zoom);
xtile_s = (xtile * tile_size - width / 2) / tile_size;
ytile_s = (ytile * tile_size - height / 2) / tile_size;
xtile_e = (xtile * tile_size + width / 2) / tile_size;
ytile_e = (ytile * tile_size + height / 2) / tile_size;
[lon_s, lat_s] = getLonLat(xtile_s, ytile_s, zoom);
[lon_e, lat_e] = getLonLat(xtile_e, ytile_e, zoom);
return [lon_s, lat_s, lon_e, lat_e];
}
//make a bbox, 0.001 should be modified based on zoom value
function bbox(lat, lon, zoom) {
return LonLat_to_bbox(lat, lon, zoom);
}
function zoom(satelliteZoom) {
return -1.4436 * Math.log(satelliteZoom) + 28.7;
}
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
return chrome.runtime.getURL('assets/iframe_api.js');
} else if (url.pathname.match(/www-widgetapi/)) {
// Redirect requests for YouTube Player API to local files instead
return chrome.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-23 06:19:32 +01:00
function redirectGoogleMaps(url, type) {
let query = '';
let lat = '';
let lon = '';
let zoom = '';
if (url.pathname.match(latLngZoomRegex)) {
[, lat, lon, zoom] = url.pathname.match(latLngZoomRegex);
}
if (type === 'main_frame') {
if (url.pathname.includes('data=')) {
const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
return `${osmInstance}/?mlat=${mlat.replace('!3d', '')}&mlon=${mlon.replace('!4d', '')}#map=${zoom}/${lat}/${lon}`;
} else if (url.search.includes('q=')) {
query = encodeURI(url.searchParams.get('q'));
} else {
query = url.pathname.split('/')[3];
}
return `${osmInstance}/search?query=${query}#map=${zoom}/${lat}/${lon}`;
} else if (type === 'sub_frame' && url.pathname.match(latLngZoomRegex)) {
const [mapleft, mapbottom, mapright, maptop] = bbox(Number(lat), Number(lon), zoom(z));
if (url.pathname.includes('data=')) {
const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
return `${osmInstance}/export/embed.html?bbox=${mapleft}%2C${mapbottom}'%2C'${mapright}'%2C'${maptop}'&layer=mapnik&marker=${mlat}%2C${mlon}'`;
}
return `${osmInstance}/export/embed.html?bbox=${mapleft}%2C${mapbottom}'%2C'${mapright}'%2C'${maptop}'&layer=mapnik'`;
}
}
2019-09-20 12:45:58 +02:00
chrome.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 = {
redirectUrl: redirectGoogleMaps(url, details.type)
};
}
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-01-14 10:48:37 +01:00
urls: ["<all_urls>"],
types: [
"main_frame",
"sub_frame",
"script"
]
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
);