User:Quarl/auto summary.js
Appearance
(Redirected from User:Quarl/force summary.js)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Quarl/auto summary. |
// [[User:Quarl/auto_summary.js]] - automatically fill in edit summary based on diff; short cuts
// depends: wikipage.js, wikiedit.js, util.js, diff.js, shortcuts.js
// recommends: smartsubmit.js
// originally based on http://wiki.riteme.site/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Force_edit_summary
// quarl 2006-01-26 added shortcuts
// quarl 2006-01-30 auto diff
// TODO: add a checkbox for "auto" next to summary, to disable auto diff
// <pre><nowiki>
var autosummary = new Object();
// see also: [[Wikipedia:Edit_summary_legend]]
autosummary.shortcuts = Shortcuts({
'ed' : 'editing', // 'edit'
'cped,cpediting,cpyed,copyed,copyedit' : 'copy-editing',
'mn ' : 'minor',
'mnf ' : 'minor fixes',
'fmt' : 'formatting',
'mfmt ' : 'minor formatting',
'rv' : 'reverting',
'rvv' : 'reverting vandalism',
'gr' : 'grammar',
'sp' : 'spelling',
'rd ' : 'redirect',
'cmt' : 'commenting',
'cla' : 'clarifying',
'xl,xlink' : 'external link',
'sa' : 'see also',
'cap' : 'capitalization',
'catz' : 'categorizing',
'cl,cu' : 'cleaning up',
'newart,creat' : 'creating new article',
'dab,disamb,disam,disambig' : 'disambiguating',
'rddab' : 'replacing redirect with disambiguation page',
//'st' : 'see Talk page',
'style' : 'style',
'punc,punct,pnct' : 'punctuation',
'wfy,wkfy' : 'wikifying'
});
autosummary.prompt = 'Enter edit summary. ' + autosummary.shortcuts.msg();
// whether to query even when filled via auto diff
autosummary.query = true;
// whether to default edit summary to diff (not needed if
// autosummary.auto_diff is enabled)
autosummary.diff = true;
// whether to automatically prepend "(auto diff)" to edit summary while
// editing. Use integer number of seconds for update interval (in sec), or
// false to disable.
autosummary.auto_diff = 3;
// whether to automatically select text after the /* section */ and « diff »
autosummary.auto_select = true;
autosummary._load = function()
{
if (!WikiPage.editingP) return;
if (WikiPage.newSectionP) return;
if (autosummary.auto_diff) {
autosummary._autoDiffSetup();
}
if (autosummary.auto_select) {
hookEventObj(document.editform.wpSummary, 'focus', autosummary._focusSummaryEvent);
}
hookEventObj(document.editform.wpSave, 'click', autosummary._preSubmitEvent);
}
autosummary._autoDiffSetup = function() {
hookEventObj(document.editform.wpTextbox1, 'blur', autosummary._updateAutoDiff);
autosummary._autoDiffTimerUpdate();
}
autosummary._autoDiffTimerUpdate = function() {
autosummary._updateAutoDiff();
setTimeout(autosummary._autoDiffTimerUpdate, 1000*autosummary.auto_diff);
}
autosummary._textFilter = function(s) {
// ignore signature when diffing
return s.replace(/~~~~/g,'').replace(/<span class=[\'\"]user-sig.*?<\/span>/g, '');
}
autosummary._diffStrings = function(s1,s2) {
return diffSummary(autosummary._textFilter(s1), autosummary._textFilter(s2));
}
autosummary.diffTextbox = function() {
var editor = wikiPage.getEditor();
return autosummary._diffStrings(editor.wpTextbox1_orig, editor.wpTextbox1);
}
// update the edit summary with diff
autosummary._updateAutoDiff = function() {
var editor = wikiPage.getEditor();
editor.updateThis();
if (editor.wpTextbox1_prev == editor.wpTextbox1) {
// no change since last update
return;
}
editor.wpTextbox1_prev = editor.wpTextbox1;
var s = autosummary.diffTextbox();
if (s) {
s = "«" + s + "» ";
}
if (editor.wpSummary.match(/\«/)) {
editor.wpSummary = editor.wpSummary.replace(/«.*?» */, s);
} else if (s && !autosummary._pruneSection(editor.wpSummary)) {
editor.wpSummary = trimspaces(editor.wpSummary);
if (editor.wpSummary) editor.wpSummary += ' ';
editor.wpSummary += s;
}
editor.updateForm();
}
autosummary._preSubmitEvent = function(event)
{
if (autosummary.auto_diff) autosummary._updateAutoDiff();
var editor = wikiPage.getEditor();
editor.updateThis();
var r = autosummary._edit(editor);
editor.updateForm();
if (!r) {
event.preventDefault();
event.stopPropagation();
}
}
// auto focus
autosummary._focusSummaryEvent = function(event) {
var sumField = document.editform.wpSummary;
if (sumField.value.match(/^(?:\/\*.*?\*\/)?\s*(?:«(?:.*)»)? ?/)) {
var n = RegExp.lastMatch.length;
var m = sumField.value.length;
// apparently you can't setSelectionRange in an onFocus handler, but
// you can set a timer to do it 0 seconds from now.
setTimeout(function() { sumField.setSelectionRange(n, m) }, 0);
}
}
autosummary._pruneSection = function(s) {
return trimspaces(s.replace(/^\/\\*.*?\\*\//,''));
}
autosummary._edit = function(editor)
{
if (editor.wpTextbox1_orig == editor.wpTextbox1) {
// no change
return true;
}
var auto = false;
if (!editor.wpSummary.match(/REDIRECT/i) &&
editor.wpTextbox1.match(/^#REDIRECT/i))
{
// it's a redirect. Annotate with REDIRECT.
if (autosummary.auto_diff) {
// don't need auto diff
// editor.wpSummary = editor.wpSummary.replace(/^〈.*?〉 */, '');
}
editor.wpSummary += editor.wpTextbox1;
auto = true;
} else if (autosummary._pruneSection(editor.wpSummary)) {
// non-negligible summary exists; continue with submission
editor.wpSummary = autosummary.shortcuts.substWords(editor.wpSummary);
return true;
} else if (autosummary.diff) {
// if we get here then we're not using auto diff, or user manually
// removed it
var s = autosummary.diffTextbox();
if (s) {
editor.wpSummary += s;
auto = true;
}
}
if (!auto || autosummary.query) {
var r = prompt(autosummary.prompt, editor.wpSummary);
if(r == null) { return false; } // cancel
editor.wpSummary = autosummary.shortcuts.substWords(r);
}
return true;
}
$(autosummary._load);
// </nowiki></pre>