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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// This script shows, if found, the kanji and kana for an article// For configuration, please see the documentationfunctionsetup(){// If we're not reading an article, do nothingif(!(mw.config.get('wgAction')==='view'&&mw.config.get('wgIsArticle')&&!___location.search.split('oldid=')[1]&&mw.config.get("wgPageName")!=="Main_Page")){return;}// Assuming that if there's no wikidata, there're no 1:1 interlanguage links,// and we don't want cases where a page links to a subsection of a jawiki// articleif(wikidataId===null){return;}// Placeholder so other elements don't push it down later$('#firstHeading').append("<div id='kanjiInfo' lang='ja'></div>");// Get the Japanese label from wikidata// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities$.ajax({url:"https://www.wikidata.org/w/api.php",data:{action:"wbgetentities",ids:wikidataId,props:"labels",languages:"ja",format:"json",origin:"*"},success:parseJaLabel});}functionparseJaLabel(response){varwikidataInfo=response.entities[wikidataId];varjaLabel;if(!jQuery.isEmptyObject(wikidataInfo.labels.ja)){jaLabel=wikidataInfo.labels.ja.value;}if(jaLabel){displayKanji(jaLabel);}else{return;}// If the japanese title is not just only kana, get the readingif(!kanaOnlyRe.test(jaLabel)){requestKana();}}functiondisplayKanji(kanji){$('#kanjiInfo').append("<ruby>"+kanji+"</ruby>");// Add some classes so users can choose to not display for example// katakana-only kanji in their CSSif(latinOnlyRe.test(kanji)){$("#kanjiInfo").addClass("kanjiInfo-latin-only");$("#kanjiInfo").css("display","none");}elseif(hiraganaOnlyRe.test(kanji)){$("#kanjiInfo").addClass("kanjiInfo-hiragana-only");}elseif(katakanaOnlyRe.test(kanji)){$("#kanjiInfo").addClass("kanjiInfo-katakana-only");}}functionrequestKana(){console.log("showKanji-dev.js: requesting kana");// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaims// We have to wholesale get all the claims instead of just one because the// kana might be present as a qualifier to another claim$.ajax({url:"https://www.wikidata.org/w/api.php",data:{action:"wbgetclaims",entity:wikidataId,format:"json",origin:"*"},success:parseKanaClaim});}functionparseKanaClaim(response){varkana;if(response.claims.P1814){// name in kanakana=response.claims.P1814[0].mainsnak.datavalue.value;}elseif(response.claims.P1476&&response.claims.P1476[0].qualifiers&&response.claims.P1476[0].qualifiers.P1814){// title w/ name in kanakana=response.claims.P1476[0].qualifiers.P1814[0].datavalue.value;}elseif(response.claims.P1705&&response.claims.P1705[0].qualifiers&&response.claims.P1705[0].qualifiers.P1814){// native label w/ name in kanakana=response.claims.P1705[0].qualifiers.P1814[0].datavalue.value;}else{scrapeKana();return;}displayKana(kana);$("#kanjiInfo rt").addClass("kanjiInfo-wikidata");}functionscrapeKana(){console.log("showKanji-dev.js: scraping kana");varjaLabel;if($(".interwiki-ja").length){jaLabel=getInterlanguageJa();}else{return;}// Get jawiki article's lead wikitext// API docs: https://www.mediawiki.org/wiki/API:Revisions$.ajax({url:"https://ja.wikipedia.org/w/api.php",data:{action:"query",prop:"revisions",format:"json",titles:jaLabel,redirects:"true",rvprop:"content",rvsection:"0",rvslots:"main",origin:"*"},success:parseJaLead});}functiongetInterlanguageJa(){varjaLabel=$(".interwiki-ja .interlanguage-link-target").attr("href");jaLabel=jaLabel.split("\/wiki\/")[1];jaLabel=decodeURIComponent(jaLabel);jaLabel=jaLabel.replace(/(.*)#.*/,"$1");// rm anchorsreturnjaLabel;}functionparseJaLead(response){varresponsePart=response.query.pages;// Have to split parsing into two parts since jawiki pageid is unknownvarpageId=Object.keys(responsePart)[0];varleadText=responsePart[pageId].revisions[0].slots.main["*"];// Only first sentencevarintroSearch=leadText.match(/'''.*?。/);varwikitext;if(introSearch){wikitext=introSearch[0];}else{return;}wikitext=wikitext.replace(/\{\{.*?\}\}/g,"{{}}");// Remove templatesvarkana;varkanji=$("#kanjiInfo").text();varkanjiEscaped=mw.RegExp.escape(kanji);kanjiEscaped=kanjiEscaped.replace(/\B/g," ?");// Account for spaces// Add kanji to regex to make sure we're not getting the reading of some// other termvarleadKanaRe=newRegExp(kanjiEscaped+".*?"+leadRe);console.log("showKanji-dev.js: lead: "+wikitext);console.log("showKanji-dev.js: kanji: "+kanji);console.log("showKanji-dev.js: regex: "+leadKanaRe);varkanaSearch=wikitext.match(leadKanaRe);if(kanaSearch&&kanaSearch.length==2){kana=kanaSearch[1];}else{return;}// Rm trailing characterskana=kana.replace(/[・、]$/,"");displayKana(kana);$("#kanjiInfo rt").addClass("kanjiInfo-jawiki");}functiondisplayKana(kana){$("#kanjiInfo ruby").append("<rt>"+kana+"</rt>");}varwikidataId=mw.config.get('wgWikibaseItemId');// RegexesvarlatinOnlyRe=/^[A-Za-z0-9\-?!,:;@#$%&+=*'" ]+$/;// Stolen from https://www.wikidata.org/wiki/Property:P1814#P1793varextrasRe="0-9「」・、 \\-!〜×";varkanaOnlyRe=newRegExp("^[ぁ-ゔァ-ヴー"+extrasRe+"]+$");varhiraganaOnlyRe=newRegExp("^[ぁ-ゔーA-Z"+extrasRe+"]+$");varkatakanaOnlyRe=newRegExp("^[ァ-ヴーA-Z"+extrasRe+"]+$");varleadRe="'''.*?(([ぁ-ゔァ-ヴー"+extrasRe+"]+)";// brittle$(setup);