User:Krenakarore/common.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.

/**
 * 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]]
 */
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	/**********
	** Register scripts
	*********/
	pathoschild.TemplateScript.add([
		{ name: 'fix dashes', script: dashes },
		{ name: 'cleanup', script: cleanup },
		{ name: 'both', script: both },
		{ name: 'ref', script: ref },
		{ name: 'author', script: author },
		{ name: 'welcome & test', script: weliptest },
		{ name: '{{similar}}', script: similar },
		{ name: 'DNBset', script: DNBset }
	]);


	/**********
	** Define scripts
	*********/
	function dashes(editor) {
		editor.replace(/--/g, '—');
	}
	
	function author(editor) {
		// creates an author: link from selected text
		editor.replaceSelection(function(name) {
			return '[[Author:'+name+'|'+name+']]';
		});
	}
	
	function cleanup(editor) {
		editor
			// remove trailing whitespace at the end of each line
			.replace(/ \n/g, '\n')
	
			// remove trailing whitespace at the end of input
			.replace(/\s+$/g, '')
	
			// convert double-hyphen to mdash
			.replace(/--/g, '—')
	
			// remove spacing around mdash, but only if it has spaces on both sides
			// (we don't want to remove the trailing space from "...as follows:— "
			.replace(/ +— +/g, '—')
	
			// join words that are hyphenated across a line break
			.replace(/-\n/g, '');
	
		// stuff to do only if the page doesn't contain a <poem> tag:
		if (editor.get().indexOf("<poem>") === -1) {
			editor
				// remove single line breaks; preserve multiple.
				.replace(/([^>\n])\n([^<\n])/g, '$1 $2')
	
				// collapse sequences of spaces into a single space
				.replace(/  +/g, ' ');
		}
	
		editor
			// remove unwanted spaces around punctuation marks
			.replace(/ ([;:\?!,])/g, '$1')
	
			//OCR fixes
			// convert i9 to 19, etc.
			.replace(/[il]([0-9])/g, '1$1')
	
			// "the", "them", "their", etcetera
			.replace(/tlie/g, 'the');
	}
	
	function both(editor) {
		editor.forField('#wpHeaderTextbox')
			.append('\n{{running header|left=|center=|right=}}');
	}
	
	function ref(editor) {
		editor.forField('#wpFooterTextbox')
			.replace('<references/>','{{float center|{{smallrefs}}}}\n');
	}
	
	function weliptest(editor) {
		editor.forField('#wpHeaderTextbox')
			.append('{{subst:' + 'welcomeip}}-- ~~' + '~~\n{{test}}');
		editor.appendEditSummary('welcome & test');
	}
	
	function similar(editor) {
		editor
			.replace(/\{\{header/g, '{{similar|}}\n{{header')
			.appendEditSummary('{{similar}}');
	}
	
	function DNBset(editor) {
		editor.replace(/\{\{header\n \| title      \=.+\n \| author     \= \n \| translator \= \n \| section    \= \n \| previous   \= \n \| next       \= \n \| notes      \= \n\}\}\n/, '{{subst:DNBset\n |article= \n |previous= \n |next= \n |volume = \n |contributor = \n |wikipedia = \n |extra_notes= \n |from= \n |to= \n |section= \n}}');
	}
});
// </nowiki>

self.proofreadpage_disable_wheelzoom = false;
popupFixDabs=true;
popupAdminLinks=true;

if (mw.toolbar) {
	 $('#wpTextbox1').wikiEditor('addToToolbar', {
		section: 'main',
		group: 'format',
		tools: {
			'custom-rule': {
				label: 'rule',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/4/41/Button_hr_halfwidth.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{rule}}',
						post: '',
						sampleText: ''
					}
				}
			},
			'custom-Larger': {
				label: 'Larger',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/2/28/Bouton_noir.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{larger|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Sm': {
				label: 'Sm ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/83/Bouton_noir_mini.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{smaller|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Centre text': {
				label: 'Centre text',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/5/5f/Button_center.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{center|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Author link': {
				label: 'Author link',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/83/Button_biocitas.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '[[Author:',
						post: '|]]',
						sampleText: ''
					}
				}
			},
			'custom-Reference': {
				label: 'Reference',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/c/c4/Button_ref.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<ref>',
						post: '</ref>',
						sampleText: ''
					}
				}
			},
			'custom-sc': {
				label: 'sc ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/a/a2/SmallcapsButton.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{sc|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-f/sc': {
				label: 'f/sc ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/4/4e/Button_romain.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{sc|{{'+'subst:ucfirst:{{'+'subst:lc:',
						post: '}}}}}}',
						sampleText: ''
					}
				}
			},
			'custom-hws ': {
				label: 'hws ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/4/41/Button_hr_halfwidth.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hws|',
						post: '|}}',
						sampleText: ''
					}
				}
			},
			'custom-hwe': {
				label: 'hwe ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/4/41/Button_hr_halfwidth.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{hwe|',
						post: '|}}',
						sampleText: ''
					}
				}
			},
			'custom-S/n beg': {
				label: 'S/n beg ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{Sidenotes begin',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-S/n end': {
				label: 'S/n end ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{Sidenotes end',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-Force s/n': {
				label: 'Force s/n',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/8d/Button_image_annotation.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '{{Force sidenote|left|',
						post: '}}',
						sampleText: ''
					}
				}
			},
			'custom-section begin': {
				label: 'section begin',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/thumb/7/71/Progress-0625.svg/20px-Progress-0625.svg.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<section begin=',
						post: ' />',
						sampleText: ''
					}
				}
			},
			'custom-br': {
				label: ' br ',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/en/1/13/Button_enter.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<br />\n',
						post: '',
						sampleText: ''
					}
				}
			},
			'custom-section end': {
				label: 'section end',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/thumb/8/83/Progress-0750.svg/20px-Progress-0750.svg.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<section end=',
						post: ' />',
						sampleText: ''
					}
				}
			},
			'custom-—': {
				label: ' — ',
				type: 'button',
				icon: '//bits.wikimedia.org/skins-1.5/common/images/button_hr.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '—',
						post: '',
						sampleText: ''
					}
				}
			},
			'custom-Poem': {
				label: 'Poem',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/8f/Button_poeme.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: '<poem>',
						post: '</poem>',
						sampleText: ''
					}
				}
			}
		}
	});
}

// Auto create page when requesting a non existent Page:*, inefficient since there is
// two request per page, see setupNextLink() which avoid it in most case.
if (mw.config.get('wgCanonicalNamespace') == 'Page') {
	$(function autoCreatePage() {
		if (mw.config.get('wgArticleId') === 0 && mw.config.get('wgAction') === 'view') {
			url = document.URL.slice((mw.config.get('wgServer') + '/wiki/').length);
			url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + url + '&action=edit';
			window.location = url;
		}
	});
}

// when acting on a Page:* setup the next link to edit mode if the next page doesn't exist.
if (mw.config.get('wgCanonicalNamespace') == 'Page') {
	$(function setupNextLink() {
		ca_next = document.getElementById('ca-next');
		if (!ca_next) return;
		href = ca_next.firstChild;
		if (!href) return;
		
		var r = new RegExp("page(\\d+)");
		page = Number(r.exec(proofreadPageViewURL)[1]) + 1;
		next_page = mw.config.get('wgPageName').replace(/\d+$/g, page);
		
		var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath')
			+ "/api.php?format=xml&action=query&prop=info&titles="
			+ encodeURIComponent(next_page);
		var request = sajax_init_object();
		request.open('GET', url, true);
		request.onreadystatechange = function () {
		if (request.readyState == 4) {
			var xml = request.responseXML ;
			if (xml === null) return ;
			var page = xml.getElementsByTagName( "page" )[0];
			if (page.getAttribute("missing") == "")
				href.href = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + encodeURIComponent(next_page) + '&action=edit';
			}
		};
		request.send(null);
	});
}

/* [[Category:Bots/Script files to update]] */