User:FlightTime/Fix quotes.js

// This is a custom fork of User:DemonDays64/Scripts/Dumb quotes.js

// NOTE: Uses a regex feature implemented fairly recently in some browsers (https://caniuse.com/?search=Lookbehind):

// Chrome: 10/2017 | Firefx: 6/2020 | Edge: 1/2020 | Safari: 3/2023 | Opera: 11/2017

// If running older browsers I believe it will work normally except that it will modify File links. The previous versions did not have this feature and used widely-supported Regex.

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", "#", "FIX 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(/(?

makeAndRunRegex(/(?

doEdit();

setEditSummary("Fixed quotes per MOS:CQ by script", true);

showDiff();

}

});

});