From 51645c78dcb7d953bcc37725b5c66ec7dd5ef33e Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sat, 9 Sep 2023 18:32:38 +0300 Subject: [PATCH] Fixed OSM redirection not working https://github.com/libredirect/browser_extension/issues/812 --- src/assets/javascripts/services.js | 88 +++++++++++++++--------------- src/config.json | 3 + 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/assets/javascripts/services.js b/src/assets/javascripts/services.js index 2168193..c74e847 100644 --- a/src/assets/javascripts/services.js +++ b/src/assets/javascripts/services.js @@ -94,19 +94,19 @@ function redirect(url, type, initiator, forceRedirection) { } // Here is a (temperory) space for defining constants required in 2 or more switch cases. - const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/ const dataLatLngRegex = /!3d(-?[0-9]{1,}.[0-9]{1,})!4d(-?[0-9]{1,}.[0-9]{1,})/ const placeRegex = /\/place\/(.*)\// function convertMapCentre() { let [lat, lon, zoom] = [null, null, null] - if (url.pathname.match(mapCentreRegex)) { - // Set map centre if present - ;[lat, lon, zoom] = url.pathname.match(mapCentreRegex) + const reg = url.pathname.match(/@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/) + if (reg) { + [, lon, lat, zoom] = reg } else if (url.searchParams.has("center")) { - ;[lat, lon] = url.searchParams.get("center").split(",") + // Set map centre if present + [lat, lon] = url.searchParams.get("center").split(",") zoom = url.searchParams.get("zoom") ?? "17" } - return [zoom, lon, lat] + return { zoom, lon, lat } } if (!frontend) return @@ -219,25 +219,26 @@ function redirect(url, type, initiator, forceRedirection) { } function addressToLatLng(address) { - const xmlhttp = new XMLHttpRequest() - xmlhttp.send() - http.onreadystatechange = () => { - if (xmlhttp.status === 200) { - const json = JSON.parse(xmlhttp.responseText)[0] - if (json) { - return [`${json.lat},${json.lon}`, `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`] + const http = new XMLHttpRequest() + http.open("GET", `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(address)}&format=json&limit=1`, false) + http.send() + if (http.status == 200) { + const json = JSON.parse(http.responseText)[0] + if (json) { + return { + coordinate: `${json.lat},${json.lon}`, + boundingbox: `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}` } } - console.info("Error: Status is " + xmlhttp.status) + return {} } - xmlhttp.open("GET", `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`, false) } let mapCentre = "#" let prefs = {} const mapCentreData = convertMapCentre() - if (mapCentreData[0] && mapCentreData[1] && mapCentreData[2]) mapCentre = `#map=${mapCentreData[0]}/${mapCentreData[1]}/${mapCentreData[2]}` + if (mapCentreData.zoom && mapCentreData.lon && mapCentreData.lat) mapCentre = `#map=${mapCentreData.zoom}/${mapCentreData.lon}/${mapCentreData.lat}` if (url.searchParams.get("layer")) prefs.layers = osmLayers[url.searchParams.get("layer")] if (url.pathname.includes("/embed")) { @@ -250,55 +251,57 @@ function redirect(url, type, initiator, forceRedirection) { try { query = url.searchParams.get("pb").split(/!2s(.*?)!/)[1] } catch (error) { + // Unable to find map marker in URL. console.error(error) - } // Unable to find map marker in URL. + } - let [coords, boundingbox] = addressToLatLng(query) + let { coordinate, boundingbox } = addressToLatLng(query) prefs.bbox = boundingbox - prefs.marker = coords - prefs.layer = "mapnik" + prefs.marker = coordinate + prefs.layers = "mapnik" let prefsEncoded = new URLSearchParams(prefs).toString() return `${randomInstance}/export/embed.html?${prefsEncoded}` } else if (url.pathname.includes("/dir")) { // Handle Google Maps Directions - // https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling - - let travMod = url.searchParams.get("travelmode") - if (url.searchParams.has("travelmode")) prefs.engine = travelModes[travMod] - - let orgVal = url.searchParams.get("origin") - let destVal = url.searchParams.get("destination") - - let org = addressToLatLng(orgVal) - let dest = addressToLatLng(destVal) - prefs.route = `${org};${dest}` - - let prefsEncoded = new URLSearchParams(prefs).toString() + if (url.searchParams.has("travelmode")) { + prefs.engine = travelModes[url.searchParams.get("travelmode")] + } + const regex1 = /\/dir\/([^@/]+)\/([^@/]+)\/@-?\d[0-9.]*,-?\d[0-9.]*,\d{1,2}[.z]/.exec(url.pathname) + const regex2 = /\/dir\/([^@/]+)\//.exec(url.pathname) + if (regex1) { + // https://www.google.com/maps/dir/92+Rue+Moncey,+69003+Lyon,+France/M%C3%A9dip%C3%B4le+Lyon-Villeurbanne/@45.760254,4.8486298,13z?travelmode=bicycling + const origin = addressToLatLng(decodeURIComponent(regex1[1])).coordinate ?? '' + const destination = addressToLatLng(decodeURIComponent(regex1[2])).coordinate ?? '' + prefs.route = `${origin};${destination}` + } else if (regex2) { + // https://www.google.com/maps/dir/92+Rue+Moncey,+69003+Lyon,+France/@45.760254,4.8486298,13z?travelmode=bicycling + const origin = addressToLatLng(decodeURIComponent(regex2[1])).coordinate ?? '' + prefs.route = `${origin};` + } else { + // https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling + const origin = addressToLatLng(url.searchParams.get("origin")).coordinate ?? '' + const destination = addressToLatLng(url.searchParams.get("destination")).coordinate ?? '' + prefs.route = `${origin};${destination}` + } + const prefsEncoded = new URLSearchParams(prefs).toString() return `${randomInstance}/directions?${prefsEncoded}${mapCentre}` } else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) { // Get marker from data attribute // https://www.google.com/maps/place/41%C2%B001'58.2%22N+40%C2%B029'18.2%22E/@41.032833,40.4862063,17z/data=!3m1!4b1!4m6!3m5!1s0x0:0xf64286eaf72fc49d!7e2!8m2!3d41.0328329!4d40.4883948 let [, mlat, mlon] = url.pathname.match(dataLatLngRegex) - return `${randomInstance}/search?query=${mlat}%2C${mlon}` } else if (url.searchParams.has("ll")) { // Get marker from ll param // https://maps.google.com/?ll=38.882147,-76.99017 const [mlat, mlon] = url.searchParams.get("ll").split(",") - return `${randomInstance}/search?query=${mlat}%2C${mlon}` } else if (url.searchParams.has("viewpoint")) { // Get marker from viewpoint param. // https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832,2.295226&heading=-45&pitch=38&fov=80 - //console.log("viewpoint life") - const [mlat, mlon] = url.searchParams.get("viewpoint").split(",") - return `${randomInstance}/search?query=${mlat}%2C${mlon}` } else { // Use query as search if present. - //console.log("normal life") - let query if (url.searchParams.has("q")) query = url.searchParams.get("q") else if (url.searchParams.has("query")) query = url.searchParams.get("query") @@ -309,9 +312,6 @@ function redirect(url, type, initiator, forceRedirection) { } let prefsEncoded = new URLSearchParams(prefs).toString() - // console.log("mapCentre", mapCentre) - // console.log("prefs", prefs) - // console.log("prefsEncoded", prefsEncoded) return `${randomInstance}/${mapCentre}&${prefsEncoded}` } case "facil": { @@ -324,7 +324,7 @@ function redirect(url, type, initiator, forceRedirection) { } const mapCentreData = convertMapCentre() let mapCentre = "#" - if (mapCentreData[0] && mapCentreData[1] && mapCentreData[2]) mapCentre = `#${mapCentreData[0]}/${mapCentreData[1]}/${mapCentreData[2]}` + if (mapCentreData.zoom && mapCentreData.lon && mapCentreData.lat) mapCentre = `#${mapCentreData.zoom}/${mapCentreData.lon}/${mapCentreData.lat}` if (url.pathname.includes("/embed")) { // Handle Google Maps Embed API diff --git a/src/config.json b/src/config.json index 6f86411..aa14cae 100644 --- a/src/config.json +++ b/src/config.json @@ -489,12 +489,14 @@ "frontends": { "facil": { "name": "FacilMap", + "embeddable": true, "instanceList": true, "url": "https://github.com/FacilMap/facilmap" }, "osm": { "name": "OpenStreetMap", "instanceList": true, + "embeddable": true, "url": "https://www.openstreetmap.org/" } }, @@ -504,6 +506,7 @@ ], "name": "Maps", "options": { + "redirectType": "main_frame", "enabled": false, "frontend": "osm", "unsupportedUrls": "bypass"