User:BethNaught/testing.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/**
 * This script facilitates testing of functions I create.
 * It is not useful unless you want to debug my scripts.
 */
 
//  Imports:
mw.loader.load('//en.wikisource.org/w/index.php?title=User:BethNaught/retrievePagenum.js&action=raw&ctype=text/javascript');

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:Pathoschild/templatescript.js]]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	
	pathoschild.TemplateScript.add([
		
		// to test retrievePagenum.js; Page: NS only
		{ category: 'Test',
		  name: 'retrievePagenum',
		  script: function(editor) {
		  	alert(retrievePagenum());
		  },
		  forNamespaces: 'page'
		},
		// Put header material on the first line, separate parts with pipes,
		// and click this to transfer to a RunningHeader. Could be customised
		// per work by editing exactly how the header is changed.
		// Currently: Generic 
		{ category: 'BN dev',
		  name: 'HeaderHelp',
		  script: function(editor) {
            var firstLine, parts, numParts, numberRegex, newHeader;
		  	firstLine = /^.*\n/.exec(editor.get())[0];
		  	parts = firstLine.split(/ +/).map(x => x.trim());
		  	numParts = parts.length;
		  	numberRegex = /[0-9]+/;
		  	if (numberRegex.test(parts[0])) {
		  		newHeader = '{{RunningHeader|' + parts[0] + '|' + parts.slice(1, numParts).join(' ') + '|}}';
		  	} else if (numberRegex.test(parts[numParts - 1])) {
		  		newHeader = '{{RunningHeader||' + parts.slice(0, numParts - 1).join(' ') + '|' + parts[numParts - 1] + '}}';
		  	} else {
		  		alert('Failed to parse first line as a header, aborting');
		  		return;
		  	}
		  	editor.forField('#wpHeaderTextbox').set(newHeader);
		  	editor.replace(/^.*\n/, '');
		  },
		  forNamespaces: 'page'
		}
	]);
});