跳转到内容

User:A2569875/JSONCAT.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
(function($, mw) {
	var _addText="<p>前往[[Special:blankpage/JSONCAT]]</p>";
	if(mw.config.get('wgCanonicalNamespace').toLowerCase()==="special" && mw.config.get('wgCanonicalSpecialPageName').toLowerCase()==="blankpage" &&
	mw.config.get('wgPageName').replace(/special:[^\/]+\//i,'')==="JSONCAT"){
		$('#firstHeading').html("JSON分類");
		var temp_module_name = "AddText/Temp/Module/Data.lua";
		var module_call = {
			wikitext:"#invoke:",//分開來,避免被分到[[:Category:有脚本错误的页面]]
			pagename:"Module:"
		};
		var params = {
			action: "expandtemplates",
			format: "json",
			prop: "wikitext",
			contentmodel: "wikitext",
			templatesandboxtitle: module_call.pagename + temp_module_name,
			templatesandboxtext: 
				"return {checkJSON =function(frame)\n"+
					"local page_name = frame.args[1] or frame.args['1'] or ''\n"+
					"local page_data = (mw.text.trim(page_name)==''and mw.title.getCurrentTitle() or mw.title.new(page_name))\n"+
					"if mw.ustring.lower(page_data.contentModel)=='json' then\n"+
						"local wikitext = require('Module:Special wikitext').getJSONwikitext(page_data:getContent())\n"+
						"local cats = require('Module:Delcat').find_category(wikitext)\n"+
						"return '\"'.. page_data.fullText ..'\":[\"'..table.concat( cats, '\",\"')..'\"],'\n"+
					"end\n"+
					"return ''\n"+
				"end}",
			templatesandboxcontentmodel: "Scribunto",
			templatesandboxcontentformat: "text/plain"
		};
		new mw.Api().get({
			action: "query",
			format: "json",
			prop: "",
			list: "search",
			utf8: 1,
			srsearch: "insource:/\\\"_addText\\\"/i",
			srnamespace: "*",
			srlimit: 5000,
			srinfo: "",
			srprop: ""
		}).done(function(data){
			var result = data.query.search || [];
			var cat_getter = Object.assign({}, params);
			var cat_getter_json='';
			for(var i in result){
				if (Object.hasOwnProperty.call(result, i)) {
					var title = result[i].title || '';
					if(title.toString().trim()!==''){
						if(cat_getter_json.trim()!=='')cat_getter_json+='\n';
						cat_getter_json += "{{" + module_call.wikitext + temp_module_name + "|checkJSON|"+title + "}}";
					}
				}
			}
			cat_getter.text=cat_getter_json;
			if(cat_getter_json.trim()!=='')new mw.Api().get(cat_getter).done(function(catdata){
				var parsed_wiki = (catdata.expandtemplates.wikitext || '').toString().trim();
				var cat_json = {};
				var cat_data = {};
				try{eval("cat_json={"+parsed_wiki+"};");}catch{}
				for(var pagename in cat_json){
					if (Object.hasOwnProperty.call(cat_json, pagename)) {
						var cats = cat_json[pagename] || [];
						if(cats.length <= 1 && (cats[0] || '').toString().trim()===''){
							cat_data['']=cat_data['']||[];
							cat_data[''].push(pagename);
						}else{
							for(var j=0; j<cats.length; ++j){
								cat_data[cats[j]]=cat_data[cats[j]]||[];
								cat_data[cats[j]].push(pagename);
							}
						}
					}
				}
				//rendering
				var render_wikitext='';
				for(var cat_name in cat_data){
					if (Object.hasOwnProperty.call(cat_data, cat_name)) {
						var cat_pg_list = cat_data[cat_name] || [];
						if(cat_pg_list.length > 0){
							var display_cat_name = "未分類";
							if(cat_name.toString().trim()!=='')
								display_cat_name = '[[:' + cat_name + ']]';
							render_wikitext += '*' + display_cat_name + '\n';
							for(var k=0; k<cat_pg_list.length; ++k){
								render_wikitext += '**' + '[[:' + cat_pg_list[k] + ']]\n';
							}
						}
					}
				}
				if(render_wikitext.toString().trim() !== ''){
					var params = {
						action: 'parse',
						text: render_wikitext,
						contentmodel: "wikitext",
						prop: "text",
						format: 'json'
					};
					new mw.Api().get(params).done(function(final_data){
						var final_parsed_wiki = (final_data.parse.text['*'] || '').toString().trim();
						if(final_parsed_wiki !== ''){
							$('#mw-content-text').html(final_parsed_wiki);
						}
					});
				}
			});
		});
	}
})(jQuery, mw);