利用者:FlatLanguage/MarkSitelinks.js
表示
お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。
多くの Windows や Linux のブラウザ
- Ctrl を押しながら F5 を押す。
Mac における Safari
Mac における Chrome や Firefox
- ⌘ Cmd と ⇧ Shift を押しながら R を押す。
詳細についてはWikipedia:キャッシュを消すをご覧ください。
if (mw.config.get('wgCanonicalSpecialPageName') == 'Newpages') {
mw.hook("wikipage.content").add(function ($content) {
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.ForeignApi'], function ($content) {
var titles = [];
var pagedata = [];
var entitydata = {};
content = mw.util.$content || content;
if (!content[0]) return;
const rawAnchors = content[0].getElementsByClassName('mw-newpages-pagename');
const anchors = Array.prototype.slice.call(rawAnchors);
for (let a of anchors) {
let pagetitle = a.textContent;
if (!titles.includes(pagetitle)) {
titles.push(pagetitle);
}
}
const titlesArray = [...titles];
const titlesArrays = [];
while (titlesArray.length) {
titlesArrays.push(titlesArray.splice(0, 50));
}
var promises_getQID = [];
var promises_getwiki = [];
for (let titles of titlesArrays) {
promises_getQID.push(
new mw.Api().get({
action: 'query',
format: 'json',
formatversion: 2,
prop: 'pageprops',
titles: [...titles].join('|'),
}).then(function (obj) {pagedata.push(...obj.query.pages);})
);
}
Promise.all(promises_getQID).then((obj) => {
//console.log(JSON.stringify(pagedata));
let QID = [];
let i = 0;
for (let pagedatum of pagedata) {
if ('pageprops' in pagedatum) {
if ('wikibase_item' in pagedatum.pageprops) {
QID[i] = pagedatum.pageprops.wikibase_item;
i++;
}
}
}
const QIDArray = [...QID];
const QIDArrays = [];
while (QIDArray.length) {
QIDArrays.push(QIDArray.splice(0, 50));
}
for (let QIDs of QIDArrays) {
promises_getwiki.push(
new mw.ForeignApi('//www.wikidata.org/w/api.php').get({
action: 'wbgetentities',
format: 'json',
props: 'sitelinks',
ids: [...QIDs].join('|'),
}).then(function (obj) {Object.assign(entitydata, obj.entities);})
);
}
Promise.all(promises_getwiki).then((obj) => {
//console.log(JSON.stringify(entitydata));
for (let a of anchors) {
let pagetitle = a.textContent;
let pagedatum = pagedata.find( (pagedatum) => pagedatum.title == pagetitle );
if ('pageprops' in pagedatum) {
if ('wikibase_item' in pagedatum.pageprops) {
let text = count_and_search(entitydata[pagedatum.pageprops.wikibase_item].sitelinks);
span = document.createElement('span');
span.classList.add('msl-sitelinks');
span.textContent = text;
a.append(span);
}
}
}
});
});
function count_and_search (sitelinks) {
let sites = {enwiki: 'en', frwiki: 'fr', dewiki: 'de', eswiki: 'es', kowiki: 'ko', zhwiki: 'zh', specieswiki: 'species', commonswiki: 'c'};
var text = '【' + (Object.keys(sitelinks).length - 1) + 'wiki';
for (let wiki in sites) {
if (wiki in sitelinks) {
text += ', ' + sites[wiki] + ':' + sitelinks[wiki].title;
break;
}
}
text += '】';
return text;
}
});
});
}