User:Opencooper/talkCount.js
Appearance
From Wikipedia, the free encyclopedia
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.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Opencooper/talkCount.
// Shows the number of discussions on the talk page // Namespace # reference: https://www.mediawiki.org/wiki/Manual:Namespace#Built-in_namespaces // License: CC0 functionsetup(){ // If we're not reading an article, do nothing if(!(mw.config.get('wgAction')==='view' &&mw.config.get('wgIsArticle') &&!location.search.split('oldid=')[1]// Old revision &&!mw.config.get("wgIsMainPage"))){ return; } // Check if we're on a talk page varnamespace=mw.config.get('wgNamespaceNumber'); if(namespace%2==1){// All talk namespaces have odd numbers return; } // Make sure there's a talk tab if(!$("#ca-talk").length){ return; } // Check if talk page is a redlink if($("#ca-talk.new").length!==0){ return; } vartalkUrl=$("#ca-talk a").attr("href"); vartalkTitle=talkUrl.split("/wiki/")[1]; talkTitle=decodeURI(talkTitle); // Mediawiki encodes these while decodeURI doesnt reverse it talkTitle=talkTitle.replace(/%26/,"&").replace(/%2B/,"+").replace(/%3D/,"=").replace(/%3F/,"?"); determineTalkType(talkTitle); } functiondetermineTalkType(talkTitle){ // Find out whether the talk page is a flow page or not // API docs: https://www.wikidata.org/w/api.php?action=help&modules=query%2Binfo $.ajax({ url:apiUrl, data:{ action:"query", format:"json", titles:talkTitle, prop:"info" }, success:function(queryResponse){ varpage=queryResponse.query.pages; varpageId=Object.keys(page)[0]; varcontentModel=page[pageId].contentmodel; if(contentModel=="flow-board"){ getFlowTopics(talkTitle); }else{ parseTalk(talkTitle); } } }); } functiongetFlowTopics(talkTitle){ // Get section info from Flow API // API docs: https://www.wikidata.org/w/api.php?action=help&modules=flow%2Bview-topiclist $.ajax({ url:apiUrl, data:{ action:"flow", submodule:"view-topiclist", vtltoconly:"true", vtllimit:"max", format:"json", page:talkTitle, }, success:parseFlow }); } functionparseFlow(flowResponse){ vartopicList=flowResponse.flow["view-topiclist"].result.topiclist; varroots=topicList.roots; varposts=[]; for(leti=0;i<roots.length;i++){ root=roots[i]; // Assuming roots have a 1:1 mapping to posts posts.push(topicList.posts[root][0]); } varrevisions=topicList.revisions; varsectionTitles=[]; for(leti=0;i<posts.length;i++){ varkey=posts[i]; vartitle=revisions[key].content.content; sectionTitles.push(title); } varsectionsCount=sectionTitles.length; displayCount(sectionsCount,sectionTitles); } functionparseTalk(talkTitle){ // Get parsed talk page section info // API docs: https://en.wikipedia.org/w/api.php?action=help&modules=parse $.ajax({ url:apiUrl, data:{ action:"parse", format:"json", page:talkTitle, redirects:"yes", prop:"sections" }, success:parseSections }); } functionparseSections(sectionsResponse){ varsectionsCount=sectionsResponse.parse.sections.length; varsectionTitles=[]; for(leti=0;i<sectionsCount;i++){ sectionHeader=sectionsResponse.parse.sections[i].line; sectionTitles.push(sectionHeader); } displayCount(sectionsCount,sectionTitles); } functiondisplayCount(sectionsCount,sectionTitles){ varformattedTitles=""; for(leti=0;i<sectionsCount;i++){ formattedTitles+=i+1+". "+sectionTitles[i]; if(i!=sectionsCount-1){ formattedTitles+="\n"; } } formattedTitles=formattedTitles.replace(/'/g,"'");// escape varcountHTML=" <small id='talkCount' title='"+formattedTitles+"'>(" +sectionsCount+")</small>"; $("#ca-talk a").append(countHTML); } varapiUrl=location.origin+"/w/api.php"; setup();