User:Habst/mapLocations.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Habst/mapLocations. |
// max = 60 -> 250
window.level = '';
window.qids ??= {};
window.locs ??= {};
window.delay = 1000;
window.features = [];
pre=''
window.colors = {Platinum:'blue',Gold:'gold',Silver:'silver',Elite:'silver',Bronze:'bronze',Challenger:'black',Label:'black'};
get = async (url) => {
console.log(url);
await new Promise(res => setTimeout(res, window.delay));
return await (await fetch(url)).json();
}
(async () => {
for (const tr of document.querySelector('.wikitable').querySelectorAll('tbody tr')) {
const tds = tr.querySelectorAll('td');
if (tds.length === 1) {
level = tds[0].innerText.split(' ')[0];
continue;
}
const date = new Date(tds[0].querySelector('span').getAttribute('data-sort-value').replace(/^0+/, '').replace(/-0+$/, ''));
let loctitle = decodeURIComponent(tds[2].querySelector('a').href.split('/').at(-1));
loctitle = {Athina:'Athens',Maia:'Maia, Portugal'}[loctitle] ?? loctitle;
window.qids[loctitle] ??= await get(`https://en.wikipedia.org/w/api.php?${new URLSearchParams({action:'query',prop:'pageprops',ppprop:'wikibase_item',format:'json',titles:loctitle,redirects:''})}`);
const pid = Object.keys(qids[loctitle].query.pages)[0];
const qid = qids[loctitle].query.pages[pid].pageprops?.wikibase_item;
window.locs[qid] ??= await get(pre+`https://www.wikidata.org/w/api.php?${new URLSearchParams({action:'wbgetclaims',format:'json',entity:qid,property:'P625'})}`);
const { latitude, longitude } = locs[qid].claims.P625[0].mainsnak.datavalue.value;
console.log(loctitle, qid, latitude, longitude);
const coord = `{{coord|${latitude}|${longitude}}}`;
const dt = date.toLocaleString('sv').split('T')[0];
const matchFt = features.find(ft => ft['mark-coord'] === coord);
if (matchFt) {
matchFt['mark-title'] += `, [[${tds[1].innerText}]]`;
matchFt['mark-description'] += `, ${level} (${dt})`;
if (colors[level] !== matchFt['shape-color']) matchFt['shape-color'] = 'red';
} else {
features.push({
'mark-coord': coord,
'mark-title': `[[${tds[1].innerText}]]`,
'mark-description': `${level} (${dt})`,
shape: 'n-circle',
'shape-color': `${colors[level]}`
});
}
};
const out = `
{{OSM Location map
| zoom = 0
| float = right
| width = 300
| height = 250
| caption =
| title = 2024 World Athletics Label Road Races
| mark = Invisible.png
${features.map((ft, i) => Object.entries(ft).map(([k,v]) => `| ${k}${i+1} = ${v}`).join('\n')).join('\n')}
}}
`;
console.log(out);
return out;
})();