User:Nux/WikilinQs.js
Appearance
< User:Nux
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:Nux/WikilinQs. |
// <nowiki>
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/**
* WikilinQs.
*
* Show WD items id (Q) for links.
* Show Q before links.
*
* Repository (issues, pull requests):
* https://github.com/Eccenux/WikilinQs
*
* Authors:
* Maciej Nux Jaros
*
* Deployed with love using Wikiploy: [[Wikipedia:Wikiploy]]
*/
var logTag = '[wdQs]';
/** API url */
var wdUrl = (title) => `/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&format=json&formatversion=2&titles=${title}`;
/**
* Main links.
*/
class WikidataLinks {
/**
* Read and show all WD ids beside wiki-links.
*/
async all() {
const links = document.querySelectorAll('.mw-parser-output a[href^="/wiki"]');
if (!links.length) {
return;
}
// CSS
this.addStyle();
// read Q from links and show Q beside them
const promki = [];
for (const link of links) {
promki.push(this.readAndShowQ(link));
}
console.log(logTag, 'scheduled %d requests', links.length);
// wait
await Promise.all(promki);
console.log(logTag, 'done');
}
/** @private Add CSS. */
addStyle() {
const styleId = 'wikilinqs-css';
if (document.getElementById(styleId)) {
console.warn(logTag, 'addStyle repeat');
return;
}
const css = /*css*/`
.wikilinqs {
display: inline-block;
vertical-align: super;
font-size: 80%;
padding-left: .2em;
color: mediumvioletred;
background: white;
}
`;
document.body.insertAdjacentHTML('beforeend', `<style id='${styleId}'>${css}</style>`);
}
/**
* Show WD item for the atricle link.
*
* @private
*
* @param {Element} link Article link element.
*/
async readAndShowQ(link) {
const wd = await this.readQ(link);
this.addQ(wd);
console.log(logTag, wd);
}
/** @private Show Q beside link. */
addQ(wd) {
wd.el.insertAdjacentHTML('afterend', `<div class="wikilinqs">${wd.q}</div>`);
return wd;
}
/** @private get WD id from API. */
async readQ(el) {
let title = el.href.replace(/.+\.org\/wiki\//, '');
let url = wdUrl(title);
const re = await fetch(url);
const data = await re.json();
//console.log(data);
const page = data.query.pages.pop();
return {
title: page.title,
q: page.pageprops.wikibase_item,
el: el,
};
}
}
// export
if (typeof module === 'object' && module.exports) {
module.exports.WikidataLinks = WikidataLinks;
module.exports.logTag = logTag;
}
},{}],2:[function(require,module,exports){
const { WikidataLinks, logTag } = require("./WikidataLinks");
// WD instance
const wdLinks = new WikidataLinks();
// run hook
mw.hook('userjs.wikilinqs.ready').fire(wdLinks);
// prep. tool
mw.loader.using(["mediawiki.util"]).then( function() {
var portletId = mw.config.get('skin') === 'timeless' ? 'p-pagemisc' : 'p-tb';
var linkLabel = '🇶 Show WD-Q';
var itemId = 'wikilinqs-tool';
var item = mw.util.addPortletLink(portletId, '#', linkLabel, itemId);
item.title = 'WikilinQs: Show WD ids beside wiki-links.'
$(item).on('click', (evt) => {
evt.preventDefault();
wdLinks.all();
});
});
},{"./WikidataLinks":1}]},{},[2]);
// </nowiki>