Added partial FacilMap support #70

This commit is contained in:
ManeraKai 2022-03-07 06:37:58 +03:00
parent cb0d538cf5
commit 926ed4bb5c
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
6 changed files with 148 additions and 38 deletions

View File

@ -3,9 +3,16 @@ import commonHelper from './common.js'
const targets = /^https?:\/\/(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/; const targets = /^https?:\/\/(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/;
let redirects = { let redirects = {
'osm': {
"normal": [ "normal": [
"https://openstreetmap.org" "https://openstreetmap.org"
] ]
},
'facil': {
"normal": [
"https://facilmap.org"
]
}
}; };
const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/; const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/;
const dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g; const dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g;
@ -16,7 +23,7 @@ const travelModes = {
bicycling: "fossgis_osrm_bike", bicycling: "fossgis_osrm_bike",
transit: "fossgis_osrm_car", // not implemented on OSM, default to car. transit: "fossgis_osrm_car", // not implemented on OSM, default to car.
}; };
const layers = { const osmLayers = {
none: "S", none: "S",
transit: "T", transit: "T",
traffic: "S", // not implemented on OSM, default to standard. traffic: "S", // not implemented on OSM, default to standard.
@ -50,88 +57,167 @@ const getDisable = () => disable;
function setDisable(val) { function setDisable(val) {
disable = val; disable = val;
browser.storage.local.set({ disableMaps: disable }) browser.storage.local.set({ disableMaps: disable })
console.log("disableMaps: ", disable)
} }
function isMaps(url, initiator) { let frontend;
if (disable) return false; const getFrontend = () => frontend;
if (initiator && initiator.host === "earth.google.com") return false; function setFrontend(val) {
return url.href.match(targets); frontend = val;
} browser.storage.local.set({ mapsFrontend: frontend })
console.log("mapsFrontend: ", frontend)
};
function redirect(url, initiator) {
if (disable) return;
if (initiator && initiator.host === "earth.google.com") return;
if (!url.href.match(targets)) return;
function redirect(url) {
let redirect; let redirect;
let link = commonHelper.getRandomInstance(redirects.normal); let randomInstance
let mapCentre = ""; if (frontend == 'osm') randomInstance = commonHelper.getRandomInstance(redirects.osm.normal);
if (frontend == 'facil') randomInstance = commonHelper.getRandomInstance(redirects.facil.normal);
let mapCentre;
let params = ""; let params = "";
// Set map centre if present // Set map centre if present
if (url.pathname.match(mapCentreRegex)) { if (url.pathname.match(mapCentreRegex)) {
const [, lat, lon, zoom] = url.pathname.match(mapCentreRegex); const [, lat, lon, zoom] = url.pathname.match(mapCentreRegex);
mapCentre = `#map=${zoom}/${lat}/${lon}`; if (frontend == 'osm') mapCentre = `#map=${zoom}/${lat}/${lon}`;
if (frontend == 'facil') mapCentre = `#${zoom}/${lat}/${lon}`
console.log("lat", lat) // vertical
console.log("lon", lon) // horizontal
console.log("zoom", zoom)
} else if (url.search.includes("center=")) { } else if (url.search.includes("center=")) {
const [lat, lon] = url.searchParams.get("center").split(","); const [lat, lon] = url.searchParams.get("center").split(",");
mapCentre = `#map=${url.searchParams.get("zoom") || "17"}/${lat}/${lon}`; const zoom = url.searchParams.get("zoom") || "17";
// Set default zoom if mapCentre not present if (frontend == 'osm') mapCentre = `#map=${zoom}/${lat}/${lon}`;
} else if (frontend == 'facil') mapCentre = `#${zoom}/${lat}/${lon}`
params = "&zoom=17";
console.log("lat", lat)
console.log("lon", lon)
console.log("zoom", zoom)
}
// Set map layer // Set map layer
params = `${params}&layers=${layers[url.searchParams.get("layer")] || layers["none"]}`; let layer = osmLayers[url.searchParams.get("layer")] || osmLayers["none"];
params = `${params}&layers=${layer}`;
console.log("layer", layer);
// Handle Google Maps Embed API // Handle Google Maps Embed API
if (url.pathname.split("/").includes("embed")) { if (url.pathname.includes("/embed")) {
let query = ""; let query = "";
if (url.searchParams.has("q")) query = url.searchParams.get("q"); if (url.searchParams.has("q")) query = url.searchParams.get("q");
else if (url.searchParams.has("query")) query = url.searchParams.has("query"); else if (url.searchParams.has("query")) query = url.searchParams.has("query");
else if (url.searchParams.has("pb")) { else if (url.searchParams.has("pb"))
try { try {
query = url.searchParams.get("pb").split(/!2s(.*?)!/)[1]; query = url.searchParams.get("pb").split(/!2s(.*?)!/)[1];
} catch (error) { } catch (error) {
console.error(error); // Unable to find map marker in URL. console.error(error); // Unable to find map marker in URL.
} }
}
console.log("query", query)
let marker, bbox; let marker, bbox;
addressToLatLng(query, (coords, boundingbox) => { addressToLatLng(query, (coords, boundingbox) => {
marker = coords; marker = coords;
bbox = boundingbox; bbox = boundingbox;
console.log("marker", marker)
console.log("bbox", bbox)
}); });
redirect = `${link}/export/embed.html?bbox=${bbox}&layer=mapnik&marker=${marker}`; if (frontend == 'osm') redirect = `${randomInstance}/export/embed.html?bbox=${bbox}&layer=mapnik&marker=${marker}`;
if (frontend == 'facil') redirect = redirect = `${randomInstance}/${mapCentre}/Mpnk`
// Handle Google Maps Directions // Handle Google Maps Directions
} else if (url.pathname.split("/").includes("dir")) { } else if (url.pathname.includes("/dir")) {
const travelMode = travelModes[url.searchParams.get("travelmode")] || travelModes["driving"]; const travelMode = travelModes[url.searchParams.get("travelmode")] || travelModes["driving"];
let origin; let origin;
addressToLatLng(url.searchParams.get("origin"), (coords) => origin = coords); console.log(url.searchParams.get("origin"))
addressToLatLng(url.searchParams.get("origin"), coords => origin = coords);
let destination; let destination;
addressToLatLng(url.searchParams.get("destination"), (coords) => destination = coords); addressToLatLng(url.searchParams.get("destination"), coords => destination = coords);
redirect = `${link}/directions?engine=${travelMode}&route=${origin}%3B${destination}${mapCentre}${params}`;
if (frontend == 'osm') redirect = `${randomInstance}/directions?engine=${travelMode}&route=${origin}%3B${destination}`;
// ${mapCentre}${params}
if (frontend == 'facil') {
mapCentre = mapCentre ?? '#1/0/0';
redirect = redirect = `${randomInstance}/${mapCentre}/Mpnk`
}
console.log("travelMode", travelMode)
console.log("origin", origin)
console.log("destination", destination)
// Get marker from data attribute // Get marker from data attribute
} else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) { } else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) {
const [mlat, mlon] = url.pathname.match(dataLatLngRegex); const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
redirect = `${link}/?mlat=${mlat.replace("!3d", "")}&mlon=${mlon.replace("!4d", "")}${mapCentre}${params}`;
if (frontend == 'osm') redirect = `${randomInstance}/?mlat=${mlat.replace("!3d", "")}&mlon=${mlon.replace("!4d", "")}${mapCentre}${params}`;
if (frontend == 'facil') redirect = redirect = `${randomInstance}/${mapCentre}/Mpnk`
console.log("mlat", mlat)
console.log("mlon", mlon)
// Get marker from ll param // Get marker from ll param
} else if (url.searchParams.has("ll")) { } else if (url.searchParams.has("ll")) {
const [mlat, mlon] = url.searchParams.get("ll").split(","); const [mlat, mlon] = url.searchParams.get("ll").split(",");
redirect = `${link}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`; redirect = `${randomInstance}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`;
console.log("mlat", mlat)
console.log("mlon", mlon)
// Get marker from viewpoint param. // Get marker from viewpoint param.
} else if (url.searchParams.has("viewpoint")) { } else if (url.searchParams.has("viewpoint")) {
const [mlat, mlon] = url.searchParams.get("viewpoint").split(","); const [mlat, mlon] = url.searchParams.get("viewpoint").split(",");
redirect = `${link}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`; redirect = `${randomInstance}/?mlat=${mlat}&mlon=${mlon}${mapCentre}${params}`;
console.log("mlat", mlat)
console.log("mlon", mlon)
// Use query as search if present. // Use query as search if present.
} else { } else {
console.log("normal life")
let query; let query;
if (url.searchParams.has("q")) query = url.searchParams.get("q"); if (url.searchParams.has("q")) query = url.searchParams.get("q");
else if (url.searchParams.has("query")) query = url.searchParams.get("query"); else if (url.searchParams.has("query")) query = url.searchParams.get("query");
else if (url.pathname.match(placeRegex)) query = url.pathname.match(placeRegex)[1]; else if (url.pathname.match(placeRegex)) query = url.pathname.match(placeRegex)[1];
redirect = `${link}/${query ? "search?query=" + query : ""}${mapCentre || "#"}${params}`; if (frontend == 'osm') {
query = query ? "/search?query=" + query : "";
mapCentre = mapCentre ?? '#';
redirect = `${randomInstance}${query}${mapCentre}${params}`;
}
if (frontend == 'facil') {
query = query ? `/${query}` : "";
mapCentre = mapCentre ?? '#1/0/0';
redirect = `${randomInstance}/${mapCentre}/Mpnk${query}`
}
console.log("query", query)
} }
return redirect; return redirect;
} }
async function init() { async function init() {
return new Promise((resolve) => { return new Promise(resolve => {
browser.storage.local.get( browser.storage.local.get(
[
"disableMaps", "disableMaps",
(result) => { "mapsFrontend"
disable = result.disableMaps ?? false ],
r => {
disable = r.disableMaps ?? false
frontend = r.mapsFrontend ?? 'osm'
resolve(); resolve();
} }
); );
@ -141,7 +227,10 @@ async function init() {
export default { export default {
getDisable, getDisable,
setDisable, setDisable,
getFrontend,
setFrontend,
redirect, redirect,
isMaps,
init, init,
}; };

View File

@ -40,6 +40,7 @@ let BYPASSTABs = [];
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
details => { details => {
// console.log("url", details.url);
const url = new URL(details.url); const url = new URL(details.url);
let initiator; let initiator;
if (details.originUrl) if (details.originUrl)
@ -57,7 +58,7 @@ browser.webRequest.onBeforeRequest.addListener(
if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator); if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator);
if (mapsHelper.isMaps(url, initiator)) newUrl = mapsHelper.redirect(url); if (!newUrl) newUrl = mapsHelper.redirect(url, initiator);
if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator); if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator);

View File

@ -89,10 +89,20 @@
</section> </section>
<section class="option-block"> <section class="option-block">
<div class="some-block option-block"> <div class="some-block option-block">
<h4>Enable</h4> <h4>Enable</h4>
<input id="disable-osm" type="checkbox" checked /> <input id="disable-osm" type="checkbox" checked />
</div> </div>
<div class="some-block option-block">
<h4>Frontend</h4>
<select id="maps-frontend">
<option value="osm">OpenStreetMap</option>
<option value="facil">Facil Map</option>
</select>
</div>
</section> </section>
<script type="module" src="../init.js"></script> <script type="module" src="../init.js"></script>
<script type="module" src="./maps.js"></script> <script type="module" src="./maps.js"></script>

View File

@ -5,6 +5,16 @@ disableMapsElement.addEventListener("change",
(event) => mapsHelper.setDisable(!event.target.checked) (event) => mapsHelper.setDisable(!event.target.checked)
); );
let mapsFrontendElement = document.getElementById("maps-frontend");
mapsFrontendElement.addEventListener("change",
event => {
let frontend = event.target.options[mapsFrontendElement.selectedIndex].value;
mapsHelper.setFrontend(frontend);
}
);
mapsHelper.init().then(() => { mapsHelper.init().then(() => {
disableMapsElement.checked = !mapsHelper.getDisable(); disableMapsElement.checked = !mapsHelper.getDisable();
let frontend = mapsHelper.getFrontend();
mapsFrontendElement.value = frontend;
}) })

View File

@ -112,7 +112,7 @@
</div> </div>
<div class="some-block option-block"> <div class="some-block option-block">
<h4>Bypass Read on Reddit</h4> <h4>Bypass "Open in Reddit"</h4>
<input id="bypass-watch-on-reddit" type="checkbox" checked /> <input id="bypass-watch-on-reddit" type="checkbox" checked />
</div> </div>

View File

@ -103,7 +103,7 @@
</div> </div>
<div class="some-block option-block"> <div class="some-block option-block">
<h4>Bypass Watch on Twitter</h4> <h4>Bypass "Open in Twitter"</h4>
<input id="bypass-watch-on-twitter" type="checkbox" checked /> <input id="bypass-watch-on-twitter" type="checkbox" checked />
</div> </div>