about summary refs log tree commit diff stats
path: root/background.js
diff options
context:
space:
mode:
Diffstat (limited to 'background.js')
-rw-r--r--background.js101
1 files changed, 20 insertions, 81 deletions
diff --git a/background.js b/background.js
index 70fc0c2b..dd620d86 100644
--- a/background.js
+++ b/background.js
@@ -8,7 +8,7 @@ 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)/;
 const osmDefault = 'https://openstreetmap.org';
-const googleMapsRegex = /(google).*(\/maps)/;
+const googleMapsRegex = /https?:\/\/((www|maps)\.)?(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;
 
@@ -21,7 +21,9 @@ let disableInvidious;
 let disableBibliogram;
 let disableOsm;
 
-chrome.storage.sync.get(
+window.browser = window.browser || window.chrome;
+
+browser.storage.sync.get(
   [
     'nitterInstance',
     'invidiousInstance',
@@ -44,7 +46,7 @@ chrome.storage.sync.get(
   }
 );
 
-chrome.storage.onChanged.addListener(changes => {
+browser.storage.onChanged.addListener(changes => {
   if ('nitterInstance' in changes) {
     nitterInstance = changes.nitterInstance.newValue || nitterDefault;
   }
@@ -71,66 +73,16 @@ chrome.storage.onChanged.addListener(changes => {
   }
 });
 
-// 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');
+    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
-    return chrome.runtime.getURL('assets/www-widgetapi.js');
+    return browser.runtime.getURL('assets/www-widgetapi.js');
   } else {
     // Proxy video through the server
     url.searchParams.append('local', true);
@@ -156,35 +108,27 @@ function redirectInstagram(url) {
   }
 }
 
-function redirectGoogleMaps(url, type) {
-  let query = '';
+function redirectGoogleMaps(url) {
+  // Do not redirect embedded maps
+  if (url.pathname.includes('/embed')) {
+    return;
+  }
   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];
-    }
+  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 {
+    const query = encodeURI(url.searchParams.get('q')) || 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'`;
   }
 }
 
-chrome.webRequest.onBeforeRequest.addListener(
+browser.webRequest.onBeforeRequest.addListener(
   details => {
     const url = new URL(details.url);
     let redirect;
@@ -209,7 +153,7 @@ chrome.webRequest.onBeforeRequest.addListener(
     } else if (url.href.match(googleMapsRegex)) {
       if (!disableOsm) {
         redirect = {
-          redirectUrl: redirectGoogleMaps(url, details.type)
+          redirectUrl: redirectGoogleMaps(url)
         };
       }
     }
@@ -222,12 +166,7 @@ chrome.webRequest.onBeforeRequest.addListener(
     return redirect;
   },
   {
-    urls: ["<all_urls>"],
-    types: [
-      "main_frame",
-      "sub_frame",
-      "script"
-    ]
+    urls: ["<all_urls>"]
   },
   ['blocking']
 );