User:DemonDays64/Scripts/Dumb quotes.js: Difference between revisions

Content deleted Content added
Revert to https://en.wikipedia.org/w/index.php?title=User:DemonDays64/Scripts/Dumb_quotes.js&oldid=950073978
Add note about regex
 
(42 intermediate revisions by 2 users not shown)
Line 1:
// See the documentation! Hope you like the script :-)
// Make sure the utilities module is loaded (will only load if not already)
// 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 = originalPage;
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();
setDumbQuoteseditPage();
});
 
function setEditSummaryrunRegex(regex, thingToRegex) {
modifiedPage = modifiedPagethingToRegex.replace(singleQuoteRegexregex.find, singleQuoteRegexregex.replace);
document.editform.wpMinoredit.checked = true;
document.editform.wpSummary.value = 'Replaced smart quotes with dumb. Problem? [[User talk:DemonDays64|Tell me]].';
doaction('diff');
}
function setDumbQuotes() {
var singleQuoteRegex = {
find: /(‘|’)/g,
replace: "'"
}
 
function makeAndRunRegex(findRegex, replace) {
var doubleQuoteRegex = {
var singleQuoteRegexregexObject = {
find: /(“|”)/g,
replacefind: '"'findRegex,
replace: "'"replace
};
runRegex(regexObject, modifiedPage);
}
 
function setDumbQuotesdoEdit() {
document.editform.wpTextbox1.value = modifiedPage;
}
 
function setEditSummary(summary, isMinor) {
document.editform.wpMinoredit.checked = trueisMinor;
var originalPagepreviousSummary = document.editform.wpTextbox1wpSummary.value;
if (previousSummary !== "") {
if (!previousSummary.includes(summary)) {
document.editform.wpSummary.value = document.editform.wpSummary.value + " | " + summary;
}
}
else {
document.editform.diff()wpSummary.value = summary;
}
}
 
function showDiff() {
var originalPage = document.editform.wpTextbox1.value;
if(typeof doaction !== 'undefined') doaction("diff");
var modifiedPage = originalPage;
}
 
function editPage() {
modifiedPage = modifiedPage.replace(singleQuoteRegex.find, singleQuoteRegex.replace);
pageBeforeEdit = document.editform.wpTextbox1.value;
modifiedPage = modifiedPage.replace(doubleQuoteRegex.find, doubleQuoteRegex.replace);
modifiedPage = pageBeforeEdit;
document.editform.wpTextbox1.value = modifiedPage;
 
setEditSummary();
makeAndRunRegex(/(?<!File:[^\]]*[^\]]*)(‘|’)/g, "'");
document.editform.diff();
makeAndRunRegex(/(?<!File:[^\]]*[^\]]*)(“|”)/g, '"');
doEdit();
setEditSummary("Replaced curly quotes with straight with [[User:DemonDays64/Scripts/Dumb quotes.js|script]] per [[MOS:CQ]].", true);
showDiff();
}
});
});