User:Acagastya/linksHere.js

// const linksHereRunner = => { const title = document.getElementById('firstHeading').innerText.split(' ').join('%20'); const site = 'https://en.wikinews.org'; const query = `/w/api.php?action=query&format=json&prop=linkshere&titles=${title}&lhnamespace=0&lhlimit=max&origin=*`; const url = site + query; fetchRes(url); async function fetchRes(url) { const data = await fetch(url); const parsed = await data.json; const pages = parsed.query.pages; const id = Object.keys(pages); const res = pages[id].linkshere; try { const num = res.length; if (num) { const list = res.map(el => el.title); const listToStr = list.join('\n'); console.log('bar'); alert(         `${num} articles links here.  They are:\n\n${listToStr}`        ); }   } catch (e) { console.error(e); } } };

const linksHereInit = => { let t = document.getElementById('p-article-tools'); if (!t) return; let li = document.createElement('li'); let a = document.createElement('a'); a.setAttribute('href', 'javascript:linksHereObject.linksHereRunner'); a.setAttribute('id', 'links-here'); a.innerText = 'Links here'; li.setAttribute('id', 'ca-linkshere'); li.appendChild(a);

const tBody = [...t.children][1]; const ul = [...tBody.children][0]; ul.appendChild(li); };

const linksHereObject = { linksHereRunner, linksHereInit };

$.when(mw.loader.using(['mediawiki.util']), $.ready).done( linksHereObject.linksHereInit ); //