User:SuperHamster/view-it.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
function viewIt() {
var VI_NAMESPACE = mw.config.get('wgNamespaceNumber');
if (VI_NAMESPACE === 0 || VI_NAMESPACE > 99) {
// Translations for button label and description copied over from https://meta.wikimedia.org/wiki/View_it!_Tool/localization.json
var VI_TRANSLATIONS = {
"bn": {
"label": "দেখুন",
"description": "এই নিবন্ধের সাথে সম্পর্কিত চিত্রগুলি দেখুন"
},
"cs": {
"label": "Zobrazení",
"description": "Zobrazit média související s tímto tématem"
},
"en": {
"label": "View",
"description": "View images related to this article"
},
"eo": {
"label": "Rigardu",
"description": "Rigardu bildojn rilatajn al ĉi tiu artikolo"
},
"es": {
"label": "Ver",
"description": "Ver imágenes relacionadas a este artículo"
},
"fi": {
"label": "Kuvia",
"description": "Katso tähän artikkeliin liittyviä kuvia"
},
"gl": {
"label": "Ver",
"description": "Ver imaxes relacionadas con este artigo"
},
"ha": {
"label": "Duba",
"description": "Duba hotuna masu alaƙa da wannan labarin"
},
"hu": {
"label": "Képek",
"description": "Ehhez a szócikkhez kapcsolódó képek mutatása"
},
"it": {
"label": "Guarda",
"description": "Guarda immagini collegate a questa pagina"
},
"ja": {
"label": "見てみる",
"description": "この記事に関連のある画像を見てみる"
},
"nl": {
"label": "Afbeeldingen",
"description": "Bekijk gerelateerde afbeeldingen bij dit artikel"
},
"pa": {
"label": "ਝਾਤ ਮਾਰੋ",
"description": "ਮਤਾਲਕ ਤਸਵੀਰਾਂ ਨੂੰ ਵੇਖੋ"
},
"pnb": {
"label": "جھات مارو",
"description": "متعلق تصویراں نوں ویکھو"
},
"pt": {
"label": "Visualizar",
"description": "Veja imagens relacionadas a este artigo"
},
"ro": {
"label": "Vizualizare",
"description": "Vizualizați imagini legate de acest articol"
},
"sv": {
"label": "Visa",
"description": "Se bilder relaterade till den här artikeln"
},
"tl": {
"label": "Tingnan",
"description": "Tingnan ang mga larawang may kinalaman sa artikulong ito"
},
"uk": {
"label": "Див.",
"description": "Див. зображення, які пов'язані з цією статтею"
},
"vi": {
"label": "Xem",
"description": "Xem hình ảnh có liên quan đến bài viết"
},
"tw": {
"label": "Hwɛ",
"description": "Hwɛ mfonini ahorow a ɛfa asɛm yi ho"
},
"zh": {
"label": "查看",
"description": "查看与本条目相关的图像"
},
"dag": {
"label": "Lihima",
"description": "Lihimi anfooni shɛŋa din jɛndi atikle ŋɔ"
},
"gpe": {
"label": "Pop",
"description": "Make you pop images wey dey relate plus dis article"
},
"fat": {
"label": "Hwɛ",
"description": "Hwɛ mfonyin ahorow a ɔfa asɛmpɔw yi ho"
},
"ee": {
"label": "Kpɔ",
"descriptionn": "Kpɔ nɔnɔmetata siwo do ƒome kple nyati sia"
}
};
var VI_DISABLEDLINKCSS = '.skin-timeless #pt-viewit>span,.skin-vector-2022 #pt-viewit>span{color:#abc3ed;display:block;padding:18px 0 7px;margin-bottom:-1px}.skin-vector-legacy #pt-viewit>span{background-position:right bottom;color:#abc3ed;box-sizing:border-box;display:block;float:left;height:3.07692308em;position:relative;padding-top:1.25em;padding-left:8px;padding-right:8px;font-size:.8125em;background-image:linear-gradient(to bottom,rgba(167,215,249,0) 0,#c4c4cc 100%);background-repeat:no-repeat;background-size:1px 100%}';
var VI_URL = 'https://view-it.toolforge.org/';
var viewItLabel = '';
var viewItDescription = '';
var lang = mw.config.get('wgContentLanguage');
if (lang in VI_TRANSLATIONS) {
viewItLabel = VI_TRANSLATIONS[lang]['label'];
viewItDescription = VI_TRANSLATIONS[lang]['description'];
} else {
viewItLabel = VI_TRANSLATIONS['en']['label'];
viewItDescription = VI_TRANSLATIONS['en']['description'];
}
var qNumber = '';
if (mw.config.get('wgSiteName') === 'Wikidata') {
qNumber = mw.config.get('wbEntityId');
} else {
qNumber = mw.config.get('wgWikibaseItemId');
}
if (qNumber) {
mw.util.addPortletLink('p-views', VI_URL + '?q=' + qNumber, viewItLabel, 'pt-viewit', viewItDescription, '', document.querySelector('#p-views>div>ul>li:nth-child(2)'));
document.querySelector('#pt-viewit > a').setAttribute('target', '_blank');
mw.loader.using('mediawiki.ForeignApi').then(function() {
var api = new mw.ForeignApi('https://commons.wikimedia.org/w/api.php');
api.get({
action: 'query',
format: 'json',
cirrusDumpResult: 'true',
generator: 'search',
gsrsearch: 'filetype:bitmap|drawing -fileres:0 custommatch:depicts_or_linked_from=' + qNumber,
gsrlimit: '0',
gsrinfo: 'totalhits',
gsrnamespace: '6',
wbetterms: 'label'
}).done(function(data) {
var hits = data['__main__']['result']['hits']['total']['value'];
if (hits > 99) {
hits = '99+'
}
document.querySelector('#pt-viewit > a').innerHTML = viewItLabel + ' (' + hits + ')';
if (hits === 0) {
document.head.insertAdjacentHTML("beforeend", '<style>' + VI_DISABLEDLINKCSS + '</style>');
document.querySelector('#pt-viewit').innerHTML = "<span>" + viewItLabel + " (0)</span>";
}
});
});
} else {
console.log('[View-it!] No corresponding Wikidata item found for article ' + mw.config.get('wgPageName') + '.');
}
}
}
viewIt();