User:Horcrux/WikidataShortcut.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.
// This script allow to open the corresponding item on Wikidata in a new tab by pressing shift+alt+1 (since shift+alt+ctrl+g does not work anymore)
function openWikidataPage() {
//let wikidataUrl = `https://www.wikidata.org/wiki/Special:ItemByTitle/${ mw.config.get('wgContentLanguage') }/${ mw.config.get('wgPageName') }`;
let wikidataUrl;
if (RLCONF['wgWikibaseItemId'] != undefined) {
wikidataUrl = "https://www.wikidata.org/wiki/" + RLCONF['wgWikibaseItemId'];
}
else if (window.confirm("No Wikidata item found. Do you want to create it?")) {
let canonicalUrl = document.querySelector("link[rel='canonical']").getAttribute("href");
let domain2langProjSuffix = {
"wikibooks" : "wikibooks",
"wikinews" : "wikinews",
"wikipedia" : "wiki",
"wikiquote" : "wikiquote",
"wikisource" : "wikisource",
"wikiversity" : "wikiversity",
"wiktionary" : "wiktionary"
}
let domain2noLangProj = {
"commons.wikimedia" : "commonswiki",
"meta.wikimedia" : "metawiki",
"species.wikimedia" : "specieswiki",
"wikisource" : "sourceswiki",
"www.wikidata" : "wikidatawiki",
"www.mediawiki" : "mediawikiwiki",
}
let match = canonicalUrl.match(/(?<=^|\/)((?:([^./]+)\.)?([^./]+))\.org/);
if (match) {
let [fulldomain, subdomain, domain] = match.slice(1);
let sitecode = subdomain && domain in domain2langProjSuffix
? subdomain + domain2langProjSuffix[domain]
: fulldomain in domain2noLangProj
? domain2noLangProj[fulldomain]
: undefined;
if (sitecode != undefined) {
let pagename = mw.config.get('wgPageName').replaceAll("_", " ");
wikidataUrl = encodeURI(`https://www.wikidata.org/wiki/Special:NewItem?site=${ sitecode }&page=${ pagename }&label=${ pagename }`);
}
}
if (!wikidataUrl) window.alert("Unexpected domain: " + canonicalUrl.replace(/(?<=\.org\/).*/, ""));
}
if (wikidataUrl) window.open(wikidataUrl, '_blank').focus();
}
if (mw.config.get('wgSiteName') != 'Wikidata') {
document.onkeydown = function(event) {
var x = event.charCode || event.keyCode;
if (x == 49 && event.shiftKey && event.altKey) openWikidataPage();
};
}