Added custom instance to FacilMap #139

This commit is contained in:
ManeraKai 2022-04-13 21:35:09 +03:00
parent 7123f7fe27
commit efc52079a1
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
8 changed files with 140 additions and 54 deletions

View File

@ -47,6 +47,7 @@ async function updateInstances() {
instagramHelper.setRedirects(instances.bibliogram);
redditHelper.setTedditRedirects(instances.teddit);
redditHelper.setLibredditRedirects(instances.libreddit);
translateHelper.setSimplyTranslateRedirects(instances.simplyTranslate);
translateHelper.setLingvaRedirects(instances.lingva)
@ -128,7 +129,7 @@ function processDefaultCustomInstances(
for (let element of nameCheckListElement.getElementsByTagName('input')) {
if (element.className != 'toogle-all')
nameProtocolElement.getElementsByClassName(element.className)[0].addEventListener("change", event => {
nameProtocolElement.getElementsByClassName(element.className)[0].addEventListener("change", event => {
if (event.target.checked)
nameDefaultRedirects.push(element.className)
else {

View File

@ -116,9 +116,9 @@ function isImgur(url, initiator) {
if (url.pathname == "/") return false;
if (
initiator &&
([...redirects.rimgo.normal, ...rimgoNormalCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host))
([...redirects.rimgo.normal, ...rimgoNormalCustomRedirects].includes(initiator.origin) || targets.test(initiator.host))
) return false;
return targets.some(rx => rx.test(url.href));
return targets.test(url.href);
}
function redirect(url, type) {

View File

@ -4,6 +4,7 @@ window.browser = window.browser || window.chrome;
import commonHelper from './common.js'
const targets = /^https?:\/{2}(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/;
let redirects = {
'osm': {
"normal": [
@ -16,49 +17,7 @@ let redirects = {
]
}
};
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\/(.*)\//;
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.
};
const travelModesFacil = {
driving: "car",
walking: "pedestrian",
bicycling: "bicycle",
transit: "car", // not implemented on Facil, default to car.
};
const osmLayers = {
none: "S",
transit: "T",
traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C",
};
function addressToLatLng(address, callback) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
const json = JSON.parse(xmlhttp.responseText)[0];
if (json) callback(
`${json.lat},${json.lon}`,
`${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`,
);
} else
console.info("Error: Status is " + xmlhttp.status);
}
};
xmlhttp.open(
"GET",
`https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`,
false
);
xmlhttp.send();
}
const getRedirects = () => redirects;
let disable;
const getDisable = () => disable;
@ -76,7 +35,67 @@ function setFrontend(val) {
console.log("mapsFrontend: ", frontend)
};
let facilNormalRedirectsChecks;
const getFacilNormalRedirectsChecks = () => facilNormalRedirectsChecks;
function setFacilNormalRedirectsChecks(val) {
facilNormalRedirectsChecks = val;
browser.storage.local.set({ facilNormalRedirectsChecks })
console.log("facilNormalRedirectsChecks: ", val)
}
let facilNormalCustomRedirects = [];
const getFacilNormalCustomRedirects = () => facilNormalCustomRedirects;
function setFacilNormalCustomRedirects(val) {
facilNormalCustomRedirects = val;
browser.storage.local.set({ facilNormalCustomRedirects })
console.log("facilNormalCustomRedirects: ", val)
}
function redirect(url, initiator) {
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\/(.*)\//;
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.
};
const travelModesFacil = {
driving: "car",
walking: "pedestrian",
bicycling: "bicycle",
transit: "car", // not implemented on Facil, default to car.
};
const osmLayers = {
none: "S",
transit: "T",
traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C",
};
function addressToLatLng(address, callback) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
const json = JSON.parse(xmlhttp.responseText)[0];
if (json) callback(
`${json.lat},${json.lon}`,
`${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`,
);
} else
console.info("Error: Status is " + xmlhttp.status);
}
};
xmlhttp.open(
"GET",
`https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`,
false
);
xmlhttp.send();
}
if (disable) return;
if (initiator && initiator.host === "earth.google.com") return;
@ -84,7 +103,7 @@ function redirect(url, initiator) {
let randomInstance;
if (frontend == 'osm') randomInstance = commonHelper.getRandomInstance(redirects.osm.normal);
if (frontend == 'facil') randomInstance = commonHelper.getRandomInstance(redirects.facil.normal);
if (frontend == 'facil') randomInstance = commonHelper.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]);
let mapCentre = "#";
let prefs = {};
@ -200,11 +219,18 @@ async function init() {
browser.storage.local.get(
[
"disableMaps",
"mapsFrontend"
"mapsFrontend",
"facilNormalRedirectsChecks",
"facilNormalCustomRedirects",
],
r => {
disable = r.disableMaps ?? false
frontend = r.mapsFrontend ?? 'osm'
disable = r.disableMaps ?? false;
frontend = r.mapsFrontend ?? 'osm';
facilNormalRedirectsChecks = r.facilNormalRedirectsChecks ?? [...redirects.facil.normal];
facilNormalCustomRedirects = r.facilNormalCustomRedirects ?? [];
resolve();
}
);
@ -218,6 +244,13 @@ export default {
getFrontend,
setFrontend,
getRedirects,
getFacilNormalRedirectsChecks,
setFacilNormalRedirectsChecks,
getFacilNormalCustomRedirects,
setFacilNormalCustomRedirects,
redirect,
init,
};

View File

@ -417,7 +417,7 @@ function switchInstance(url) {
}
async function init() {
return new Promise((resolve) => {
return new Promise(resolve => {
fetch('/instances/data.json').then(response => response.text()).then(data => {
let dataJson = JSON.parse(data);
browser.storage.local.get(
@ -453,6 +453,7 @@ async function init() {
alwaysUsePreferred = r.alwaysUsePreferred ?? false;
redirects.teddit = dataJson.teddit;
redirects.libreddit = dataJson.libreddit;
if (r.redditRedirects) redirects = r.redditRedirects;
if (r.redditRedirects) redirects = r.redditRedirects;

View File

@ -144,10 +144,38 @@
</select>
</div>
<div id="facil">
<div class="normal">
<div class="some-block option-block">
<h4 data-localise="__MSG_defaultInstances__">Default Instances</h4>
</div>
<div class="checklist checklist"></div>
<hr>
<div class="some-block option-block">
<h4 data-localise="__MSG_customInstances__">Custom Instances</h4>
</div>
<form class="custom-instance-form">
<div class="some-block option-block">
<input class="custom-instance" placeholder="https://facilMap.com" type="url" />
<button type="submit" class="add add-instance">
<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px"
fill="currentColor">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</svg>
</button>
</div>
</form>
<div class="checklist custom-checklist"></div>
</div>
</div>
</div>
</section>
<script type="module" src="../init.js"></script>
<script type="module" src="./maps.js"></script>
<script type="module" src="../../../assets/javascripts/localise.js"></script>
</body>
</html>
</html>

View File

@ -1,4 +1,5 @@
import mapsHelper from "../../../assets/javascripts/helpers/maps.js";
import commonHelper from "../../../assets/javascripts/helpers/common.js";
let disableMapsElement = document.getElementById("disable-osm");
disableMapsElement.addEventListener("change",
@ -10,11 +11,35 @@ mapsFrontendElement.addEventListener("change",
event => {
let frontend = event.target.options[mapsFrontendElement.selectedIndex].value;
mapsHelper.setFrontend(frontend);
changeFrontendsSettings(frontend);
}
);
let facilDivElement = document.getElementById("facil")
function changeFrontendsSettings(frontend) {
if (frontend == 'facil') {
facilDivElement.style.display = 'block';
}
else if (frontend == 'osm') {
facilDivElement.style.display = 'none';
}
}
mapsHelper.init().then(() => {
console.log(mapsHelper.getFacilNormalRedirectsChecks())
disableMapsElement.checked = !mapsHelper.getDisable();
let frontend = mapsHelper.getFrontend();
mapsFrontendElement.value = frontend;
changeFrontendsSettings(frontend);
commonHelper.processDefaultCustomInstances(
'facil',
'normal',
mapsHelper,
document,
mapsHelper.getFacilNormalRedirectsChecks,
mapsHelper.setFacilNormalRedirectsChecks,
mapsHelper.getFacilNormalCustomRedirects,
mapsHelper.setFacilNormalCustomRedirects
)
})

View File

@ -81,7 +81,6 @@ redditHelper.init().then(() => {
protocolElement.value = protocol;
changeProtocolSettings(protocol);
commonHelper.processDefaultCustomInstances(
'libreddit',
'normal',

View File

@ -78,7 +78,6 @@ function init() {
let protocol = twitterHelper.getProtocol();
protocolElement.value = protocol;
changeProtocolSettings(protocol);
console.log('init');
theme.value = twitterHelper.getTheme();
infiniteScroll.checked = twitterHelper.getInfiniteScroll();
stickyProfile.checked = twitterHelper.getStickyProfile();