2019-10-07 13:59:31 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const invidiousDefault = 'https://invidio.us';
|
2020-03-21 10:34:32 +01:00
|
|
|
const youtubeDomains = [
|
|
|
|
'm.youtube.com',
|
|
|
|
'youtube.com',
|
|
|
|
'img.youtube.com',
|
|
|
|
'www.youtube.com',
|
|
|
|
'youtube-nocookie.com',
|
|
|
|
'www.youtube-nocookie.com',
|
|
|
|
'youtu.be',
|
|
|
|
's.ytimg.com',
|
|
|
|
];
|
2020-02-11 23:35:58 +01:00
|
|
|
const nitterDefault = 'https://nitter.net';
|
2020-03-21 10:34:32 +01:00
|
|
|
const twitterDomains = [
|
|
|
|
'twitter.com',
|
|
|
|
'www.twitter.com',
|
|
|
|
'mobile.twitter.com',
|
|
|
|
'pbs.twimg.com',
|
|
|
|
'video.twimg.com',
|
|
|
|
];
|
2020-02-01 04:17:51 +01:00
|
|
|
const bibliogramDefault = 'https://bibliogram.art';
|
|
|
|
const instagramRegex = /((www|about|help)\.)?instagram\.com/;
|
2020-02-25 12:18:22 +01:00
|
|
|
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-27 02:18:50 +01:00
|
|
|
const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/;
|
2020-02-23 06:19:32 +01:00
|
|
|
const dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g;
|
2020-02-27 02:18:50 +01:00
|
|
|
const placeRegex = /\/place\/(.*)\//;
|
2020-02-25 12:18:22 +01:00
|
|
|
const travelModes = {
|
|
|
|
'driving': 'fossgis_osrm_car',
|
|
|
|
'walking': 'fossgis_osrm_foot',
|
|
|
|
'bicycling': 'fossgis_osrm_bike',
|
|
|
|
'transit': 'fossgis_osrm_car' // not implemented on OSM, default to car.
|
|
|
|
};
|
2020-02-27 02:18:50 +01:00
|
|
|
const layers = {
|
|
|
|
'none': 'S',
|
|
|
|
'transit': 'T',
|
|
|
|
'traffic': 'S', // not implemented on OSM, default to standard.
|
|
|
|
'bicycling': 'C'
|
|
|
|
}
|
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;
|
2020-03-10 06:03:28 +01:00
|
|
|
let nitterInstance;
|
|
|
|
let invidiousInstance;
|
|
|
|
let bibliogramInstance;
|
|
|
|
let osmInstance;
|
|
|
|
let alwaysProxy;
|
2020-03-21 10:34:32 +01:00
|
|
|
let onlyEmbeddedVideo;
|
|
|
|
let videoQuality;
|
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',
|
2020-02-11 23:35:58 +01:00
|
|
|
'disableBibliogram',
|
2020-03-21 10:34:32 +01:00
|
|
|
'disableOsm',
|
|
|
|
'alwaysProxy',
|
|
|
|
'onlyEmbeddedVideo',
|
|
|
|
'videoQuality'
|
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;
|
2020-03-10 06:03:28 +01:00
|
|
|
alwaysProxy = result.alwaysProxy;
|
2020-03-21 10:34:32 +01:00
|
|
|
onlyEmbeddedVideo = result.onlyEmbeddedVideo;
|
|
|
|
videoQuality = result.videoQuality;
|
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;
|
|
|
|
}
|
2020-03-10 06:03:28 +01:00
|
|
|
if ('alwaysProxy' in changes) {
|
|
|
|
alwaysProxy = changes.alwaysProxy.newValue;
|
|
|
|
}
|
2020-03-21 10:34:32 +01:00
|
|
|
if ('onlyEmbeddedVideo' in changes) {
|
|
|
|
onlyEmbeddedVideo = changes.onlyEmbeddedVideo.newValue;
|
|
|
|
}
|
|
|
|
if ('videoQuality' in changes) {
|
|
|
|
videoQuality = changes.videoQuality.newValue;
|
|
|
|
}
|
2019-10-07 13:59:31 +02:00
|
|
|
});
|
2019-09-20 12:45:58 +02:00
|
|
|
|
2020-02-27 02:18:50 +01:00
|
|
|
function addressToLatLng(address, callback) {
|
2020-02-25 12:18:22 +01:00
|
|
|
const xmlhttp = new XMLHttpRequest();
|
|
|
|
xmlhttp.onreadystatechange = () => {
|
|
|
|
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (xmlhttp.status === 200) {
|
|
|
|
const json = JSON.parse(xmlhttp.responseText)[0];
|
|
|
|
if (json) {
|
2020-02-25 13:41:01 +01:00
|
|
|
callback(
|
|
|
|
`${json.lat}%2C${json.lon}`,
|
|
|
|
`${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`
|
|
|
|
);
|
2020-02-25 12:18:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.info("Error: Status is " + xmlhttp.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xmlhttp.open(
|
|
|
|
'GET',
|
|
|
|
`https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
xmlhttp.send();
|
|
|
|
}
|
|
|
|
|
2020-03-21 10:34:32 +01:00
|
|
|
function redirectYouTube(url, type) {
|
|
|
|
if (disableInvidious) {
|
2020-02-09 01:08:04 +01:00
|
|
|
return null;
|
2020-03-21 10:34:32 +01:00
|
|
|
}
|
|
|
|
if (url.pathname.match(/iframe_api/)) {
|
2020-02-19 13:25:33 +01:00
|
|
|
// 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');
|
2020-02-19 13:25:33 +01:00
|
|
|
} 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');
|
2020-02-09 01:08:04 +01:00
|
|
|
} else {
|
2020-03-10 06:03:28 +01:00
|
|
|
// Proxy video through the server if enabled by user
|
|
|
|
if (alwaysProxy) {
|
|
|
|
url.searchParams.append('local', true);
|
|
|
|
}
|
2020-03-21 10:34:32 +01:00
|
|
|
if (videoQuality) {
|
|
|
|
url.searchParams.append('quality', videoQuality);
|
|
|
|
}
|
|
|
|
if (onlyEmbeddedVideo && type !== 'sub_frame') {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-09 01:08:04 +01:00
|
|
|
return `${invidiousInstance}${url.pathname}${url.search}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirectTwitter(url) {
|
2020-03-21 10:34:32 +01:00
|
|
|
if (disableNitter) {
|
2020-02-11 23:35:58 +01:00
|
|
|
return null;
|
2020-03-21 10:34:32 +01:00
|
|
|
}
|
|
|
|
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)}`;
|
2020-02-11 23:35:58 +01:00
|
|
|
} else {
|
|
|
|
return `${nitterInstance}${url.pathname}${url.search}`;
|
|
|
|
}
|
2020-02-09 01:08:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function redirectInstagram(url) {
|
2020-03-21 10:34:32 +01:00
|
|
|
if (disableBibliogram) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-01 04:17:51 +01:00
|
|
|
if (url.pathname === '/' || url.pathname.match(instagramPathsRegex)) {
|
2020-02-09 01:08:04 +01:00
|
|
|
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) {
|
2020-03-21 10:34:32 +01:00
|
|
|
if (disableOsm) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-25 13:41:01 +01:00
|
|
|
let redirect;
|
2020-02-24 09:22:10 +01:00
|
|
|
let mapCentre = '';
|
2020-02-27 02:18:50 +01:00
|
|
|
let params = '';
|
|
|
|
// Set map centre if present
|
|
|
|
if (url.pathname.match(mapCentreRegex)) {
|
|
|
|
const [, lat, lon, zoom] = url.pathname.match(mapCentreRegex);
|
2020-02-24 09:22:10 +01:00
|
|
|
mapCentre = `#map=${zoom}/${lat}/${lon}`;
|
2020-02-25 12:18:22 +01:00
|
|
|
} else if (url.search.includes('center=')) {
|
|
|
|
const [lat, lon] = url.searchParams.get('center').split(',');
|
2020-02-27 02:18:50 +01:00
|
|
|
mapCentre = `#map=${url.searchParams.get('zoom') || '17'}/${lat}/${lon}`;
|
|
|
|
// Set default zoom if mapCentre not present
|
|
|
|
} else {
|
|
|
|
params = '&zoom=17';
|
2020-02-23 06:19:32 +01:00
|
|
|
}
|
2020-02-27 02:18:50 +01:00
|
|
|
// Set map layer
|
|
|
|
params = `${params}&layers=${layers[url.searchParams.get('layer')] || layers['none']}`;
|
|
|
|
// Handle Google Maps Embed API
|
2020-02-25 12:18:22 +01:00
|
|
|
if (url.pathname.includes('/embed')) {
|
2020-02-27 02:18:50 +01:00
|
|
|
const query = url.searchParams.get('q') || url.searchParams.get('query');
|
2020-02-25 12:18:22 +01:00
|
|
|
let marker, bbox;
|
2020-02-27 02:18:50 +01:00
|
|
|
addressToLatLng(query, (coords, boundingbox) => {
|
2020-02-25 12:18:22 +01:00
|
|
|
marker = coords;
|
|
|
|
bbox = boundingbox;
|
|
|
|
});
|
2020-02-25 13:41:01 +01:00
|
|
|
redirect = `${osmInstance}/export/embed.html?bbox=${bbox}&layer=mapnik&marker=${marker}`;
|
2020-02-27 02:18:50 +01:00
|
|
|
// Handle Google Maps Directions
|
2020-02-25 12:18:22 +01:00
|
|
|
} else if (url.pathname.includes('/dir')) {
|
|
|
|
const travelMode = travelModes[url.searchParams.get('travelmode')] || travelModes['driving'];
|
|
|
|
let origin;
|
2020-02-27 02:18:50 +01:00
|
|
|
addressToLatLng(url.searchParams.get('origin'), coords => {
|
2020-02-25 12:18:22 +01:00
|
|
|
origin = coords;
|
|
|
|
});
|
|
|
|
let destination;
|
2020-02-27 02:18:50 +01:00
|
|
|
addressToLatLng(url.searchParams.get('destination'), coords => {
|
2020-02-25 12:18:22 +01:00
|
|
|
destination = coords;
|
|
|
|
});
|
2020-02-27 02:18:50 +01:00
|
|
|
redirect = `${osmInstance}/directions?engine=${travelMode}&route=${origin}%3B${destination}${mapCentre}${params}`;
|
|
|
|
// Get marker from data attribute
|
|
|
|
} else if (url.pathname.includes('data=') && url.pathname.match(dataLatLngRegex)) {
|
2020-02-24 04:19:56 +01:00
|
|
|
const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
|
2020-02-27 02:18:50 +01:00
|
|
|
redirect = `${osmInstance}/?mlat=${mlat.replace('!3d', '')}&mlon=${mlon.replace('!4d', '')}${mapCentre}${params}`;
|
|
|
|
// Get marker from ll param
|
|
|
|
} else if (url.searchParams.has('ll')) {
|
2020-02-24 09:22:10 +01:00
|
|
|
const [mlat, mlon] = url.searchParams.get('ll').split(',');
|
2020-02-27 02:18:50 +01:00
|
|
|
redirect = `${osmInstance}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`;
|
|
|
|
// Get marker from viewpoint param.
|
|
|
|
} else if (url.searchParams.has('viewpoint')) {
|
|
|
|
const [mlat, mlon] = url.searchParams.get('viewpoint').split(',');
|
|
|
|
redirect = `${osmInstance}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`;
|
|
|
|
// Use query as search if present.
|
2020-02-24 04:19:56 +01:00
|
|
|
} else {
|
2020-02-27 02:18:50 +01:00
|
|
|
let query;
|
|
|
|
if (url.searchParams.has('q')) {
|
|
|
|
query = url.searchParams.get('q');
|
|
|
|
} else if (url.searchParams.has('query')) {
|
|
|
|
query = url.searchParams.get('query');
|
|
|
|
} else if (url.pathname.match(placeRegex)) {
|
|
|
|
query = url.pathname.match(placeRegex)[1];
|
|
|
|
}
|
|
|
|
redirect = `${osmInstance}/${query ? 'search?query=' + query : ''}${mapCentre || '#'}${params}`;
|
2020-02-25 13:41:01 +01:00
|
|
|
}
|
2020-02-27 02:18:50 +01:00
|
|
|
|
2020-02-25 13:41:01 +01:00
|
|
|
return redirect;
|
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-03-21 10:34:32 +01:00
|
|
|
if (youtubeDomains.includes(url.host)) {
|
|
|
|
redirect = {
|
|
|
|
redirectUrl: redirectYouTube(url, details.type)
|
|
|
|
};
|
|
|
|
} else if (twitterDomains.includes(url.host)) {
|
|
|
|
redirect = {
|
|
|
|
redirectUrl: redirectTwitter(url)
|
|
|
|
};
|
2020-02-01 04:17:51 +01:00
|
|
|
} else if (url.host.match(instagramRegex)) {
|
2020-03-21 10:34:32 +01:00
|
|
|
redirect = {
|
|
|
|
redirectUrl: redirectInstagram(url)
|
|
|
|
};
|
2020-02-23 06:19:32 +01:00
|
|
|
} else if (url.href.match(googleMapsRegex)) {
|
2020-03-21 10:34:32 +01:00
|
|
|
redirect = {
|
|
|
|
redirectUrl: redirectGoogleMaps(url)
|
|
|
|
};
|
2019-09-20 12:45:58 +02:00
|
|
|
}
|
2020-02-09 01:08:04 +01:00
|
|
|
if (redirect && redirect.redirectUrl) {
|
2020-02-25 12:18:22 +01:00
|
|
|
console.info(
|
2020-02-11 23:35:58 +01:00
|
|
|
'Redirecting', `"${url.href}"`, '=>', `"${redirect.redirectUrl}"`
|
2020-02-01 04:17:51 +01:00
|
|
|
);
|
2020-02-25 12:18:22 +01:00
|
|
|
console.info('Details', details);
|
2020-01-14 10:48:37 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
);
|