Jump to content

User:DemonDays64/Scripts/Dumb quotes.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DemonDays64 (talk | contribs) at 19:27, 4 August 2023 (Implement negative lookbehind for File: without a closing ]] to avoid changing file links per Keith D request (see User talk:DemonDays64#Dumb Quotes; will test in-browser but in Regexr it seems good)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// See the documentation! Hope you like the script :-)
mw.loader.using('mediawiki.util', function () {

	$(document).ready(function () {
		var pageBeforeEdit;
		var modifiedPage;
		var previousSummary;

		//add a tab on the left
		var dumbQuotesLink = mw.util.addPortletLink("p-tb", "#", "Dumb quotes", "t-dumb-quotes", "Convert curly to straight quotes");
		$(dumbQuotesLink).click(function (event) {
			event.preventDefault();
			editPage();
		});

		function runRegex(regex, thingToRegex) {
			modifiedPage = thingToRegex.replace(regex.find, regex.replace);
		}

		function makeAndRunRegex(findRegex, replace) {
			var regexObject = {
				find: findRegex,
				replace: replace
			};
			runRegex(regexObject, modifiedPage);
		}

		function doEdit() {
			document.editform.wpTextbox1.value = modifiedPage;
		}

		function setEditSummary(summary, isMinor) {
			document.editform.wpMinoredit.checked = isMinor;
			previousSummary = document.editform.wpSummary.value;
			if (previousSummary !== "") {
				if (!previousSummary.includes(summary)) {
					document.editform.wpSummary.value = document.editform.wpSummary.value + " | " + summary;
				}
			}
			else {
				document.editform.wpSummary.value = summary;
			}
		}

		function showDiff() {
			if(typeof doaction !== 'undefined') doaction("diff");
		}

		function editPage() {
			pageBeforeEdit = document.editform.wpTextbox1.value;
			modifiedPage = pageBeforeEdit;

			makeAndRunRegex(/(?<!File:[^\]]*[^\]]*)(‘|’)/g, "'");
			makeAndRunRegex(/(?<!File:[^\]]*[^\]]*)(“|”)/g, '"');
			doEdit();
			setEditSummary("Replaced curly quotes with straight with [[User:DemonDays64/Scripts/Dumb quotes.js|script]] per [[MOS:CQ]].", true);
			showDiff();
		}
		
	});
});