Jump to content

User:Guarapiranga/scriptsPanel.js

From Wikipedia, the free encyclopedia
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.
const params = {
	action: 'parse',
	page: 'Special:MyPage/common.js',
	format: 'text/plain'
};

const api = new mw.Api();
var scripts;

api.get(params).done(data => {
	scripts = data.parse.match(/^\s*\/\/\s*importScript\([\'\"](.+)\.js[\'\"]\)/g);
});

(function() {
	var $html;
	if(mw.loader.getState('mobile.init.styles') !== 'ready') {
		$html = $("<nav class='mw-portlet vector-menu vector-menu-portal portal' role='navigation' id='p-scripts' aria-labelledby='p-scripts-label'></nav>");
		$html.append("<label id='p-scripts-label' class='vector-menu-heading'><span class='vector-menu-heading-label'>Scripts</span></label>");
		$html.append("<div class='vector-menu-content'><ul class='vector-menu-content-list'></ul></div>");
	} else {
		$html = $("<div class='generated-sidebar portlet' id='p-scripts' role='navigation'></div>");
		$html.append("<h3>Scripts</h3>");
		$html.append("<div class='pBody'><ul></ul></div>");
	}
 
	var loadScript = function(e) {
		mw.loader.load(e.data.src);
		$(e.target).parent().remove();
	};
 
 	if(scripts) {
		$.each(scripts, function(script) {
		  var scriptName = script.replace(/ /g,"_");
		  $html.find("ul").append("<li><a href='javascript:' id='t-script-"+scriptName+"'>"+script+"</a></li>");
		  $(mw.loader.getState('mobile.init.styles') !== 'ready' ? "#mw-panel" : "#column-one").append($html);
		  $("#t-script-"+scriptName).click({
		  	script: script.replace(/_/g," "),
		  	src: "//en.wikipedia.org/w/index.php?action=raw&ctype=text/javascript&title="+scriptName+".js"
		  }, loadScript);
		});
 	} /* else {
 		$html.find("ul").append("<li><a href='//en.wikipedia.org/wiki/User:MusikAnimal/scriptManager'>No scripts registered!</a></li>");
 		$(mw.loader.getState('mobile.init.styles') !== 'ready' ? "#mw-panel" : "#column-one").append($html);
 	} */
}());