User:A930913/vada/plugin/imgcap.js

if(typeof window!="undefined"){ //inside Vada DOM (function{		var a930913_imgcap={			about:{				name:"A930913's Image Captioner",				description:"Given a file, searches for all uses' captions and adds to file.",				namespace:"a930913_imgcap",				userscript:"User:A930913/vada/plugin/imgcap.js",			}		}		window.vada.loadPlugin(a930913_imgcap);	}); } else { //inside Vada process vada.plugins.a930913_imgcap={}; vada.plugins.a930913_imgcap.page=function(params){ var p=new vada.Page(params.page); var text=p.text; if(/\{\{now commons(\||\}\})/i.exec(text)!==null){ vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(params.page)),1); vada.dom.queue.redraw; return vada.dom.queue.next; }		var xhr=new XMLHttpRequest; xhr.open("get","//tools.wmflabs.org/cluestuff/cgi-bin/vada/imgcaps.py?img="+encodeURIComponent(params.page.split(":")[1]),false); xhr.send; var data=""; try { data=JSON.parse(xhr.responseText); } catch (e) { vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(params.page)),1); vada.dom.queue.redraw; return vada.dom.queue.next; }		if(Object.keys(data).length===0){ vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(params.page)),1); vada.dom.queue.redraw; return vada.dom.queue.next; }		var section='\n\n== Captioned As ==\n\n{| class="wikitable" |-\n! Page !! Caption \n'; for(var key in data)section+="|-\n| "+key+" || "+data[key]+"\n"; section+="|}\n\n\n"; if(/\n==\s?captioned as\s?==\n/i.exec(text)===null){ if(/\n==\s?licen(s|c)(e|ing):?\s?==\n/i.exec(text)===null){ //no captioned as header and no licence header: append text=text.trim+section; } else { //no captioned as header, but licence header: insert above licence var sectionstart=text.search(/\n==\s?licen(s|c)(e|ing):?\s?==\n/i.exec(text)[0]); text=text.substr(0,sectionstart).trim+section+text.substr(sectionstart); }			vada.dom.summarybox.setText("/* Captioned As */ new section"); } else { //captioned as header exists: replace var needle=/\n==\s?captioned as\s?==\n/i.exec(text)[0]; var sectionstart=text.search(needle); var sectionend=text.substr(sectionstart+needle.length).search("\n==")+sectionstart+needle.length; if(sectionend==sectionstart+needle.length-1){ //captioned as header at end text=text.substr(0,sectionstart).trim+section; } else { //captioned as header in middle text=text.substr(0,sectionstart).trim+section+text.substr(sectionend); }			vada.dom.summarybox.setText("/* Captioned As */ update"); }		vada.dom.minorbox.uncheck; vada.dom.load(p,text); }	vada.plugins.a930913_imgcap.queueclick=function(params){ vada.plugins.a930913_imgcap.page({page:params.title}); vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(params.title)),1); vada.dom.queue.redraw; }	vada.plugins.a930913_imgcap.saveclick=function(params){ vada.dom.queue.next; }	vada.plugins.a930913_imgcap.start=function{ vada.events.trigger("plugins.stop",{}); vada.events.addListener("plugins.stop",vada.plugins.a930913_imgcap.stop); vada.dom.menu.new([vada.dom.menu.buttons.nextq,vada.dom.menu.buttons.save]); vada.dom.queue.builders.show; vada.events.addListener("pagename",vada.plugins.a930913_imgcap.page); vada.events.addListener("queue.page.selected",vada.plugins.a930913_imgcap.queueclick); vada.events.addListener("save.click",vada.plugins.a930913_imgcap.saveclick); vada.plugins.a930913_imgcap.running=true; }	vada.plugins.a930913_imgcap.stop=function{ vada.plugins.a930913_imgcap.running=false; vada.dom.queue.builders.hide; vada.events.removeListener("pagename",vada.plugins.a930913_imgcap.page); vada.events.removeListener("queue.page.selected",vada.plugins.a930913_imgcap.queueclick); vada.events.removeListener("save.click",vada.plugins.a930913_imgcap.saveclick); }	vada.plugins.a930913_imgcap.running=false; vada.plugins.a930913_imgcap.button=(function(pl){		var button=new vada.dom.menu.Button("a930913_imgcap_start");		button.text="A930913's Image Captioner";		button.click=pl.start;		vada.dom.menu.buttons.vada.children.push(button);		vada.dom.menu.redraw;		return button;	})(vada.plugins.a930913_imgcap); }