This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SUAC.user.js
413 lines (350 loc) · 17.4 KB
/
SUAC.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// ==UserScript==
// @name Super User Automatic Corrector
// @author TomWij on GitHub
// @version 1.0
// @description By TomWij on GitHub. Using Jakub Hampl & Nathan Osman's framework to create a Super User specific Automatic Corrector.
// @include http://superuser.com/*
// @include http://askubuntu.com/*
// @include http://stackoverflow.com/*
// @include http://meta.superuser.com/*
// @include http://meta.askubuntu.com/*
// @include http://meta.stackoverflow.com/*
// ==/UserScript==
function EmbedCodeOnPage(javascript_code) {
var code_element = document.createElement('script');
code_element.type = 'text/javascript';
code_element.textContent = javascript_code;
document.getElementsByTagName('head')[0].appendChild(code_element);
}
function EmbedFunctionOnPage(function_name, function_contents) {
EmbedCodeOnPage(function_contents.toString().replace(/function ?/, 'function ' + function_name));
}
function EmbedFunctionOnPageAndExecute(function_contents) {
EmbedCodeOnPage("(" + function_contents.toString() + ")()");
}
EmbedFunctionOnPage('LoadDependentScript', function(script_filename, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = script_filename;
script.onload = callback;
document.getElementsByTagName('head')[0].appendChild(script);
});
EmbedFunctionOnPage('AddToolbarButton', function(toolbar, icon, tooltip, callback) {
var left = toolbar.find('li:not(.wmd-help-button):last').css('left');
if(left !== null)
left = parseInt(left.replace(/\D/g, '')) + 50;
else
left = 400;
var button = $('<li class="wmd-button" style="left: ' + left + 'px; background-image: url(' + icon + '); background-repeat: no-repeat; background-position: center center;" title="' + tooltip + '"></li>');
button.click(callback);
toolbar.append(button);
});
EmbedFunctionOnPage('CorrectTitle', function(title) {
return title.replace(/^([a-z])/g, function(match) { return match.toUpperCase(); }).replace(/(?:issue|problem)/gi, '');
});
EmbedFunctionOnPage('CorrectBody', function(originalBody) {
var corrections = {
CorrectCommonMisspellings : function(body) {
// This first batch of replacements only applies to tokens inside of word boundaries!
var replacements = {
'u':'you',
'ur':'your',
'i( |\')?':'I$1',
'i m':'I\'m',
'teh':'the',
'c(o|u)(s|z)e?':'because',
'b4':'before',
'w[au]t':'what',
'alot':'a lot',
'(I|(?:any|some)(?:one|body)) got':'$1 have',
'whos':'who\'s',
'thier':'their',
'cud':'could',
'pl[sz]':'please',
'(can|doesn|don|won|hasn|haven|isn|didn)t':'$1\'t',
'(?:ubunto|ubunut|ubunutu|ubunu|ubntu|ubutnu|uuntu|unbuntu|ubunt|ubutu)':'Ubuntu',
'ty':'Thank you',
'n1':'Nice one',
'any1':'anyone',
'shuting':'shutting',
'puting':'putting',
'instaled':'installed',
'zune':'Zune',
'whoo?ping':'',
'just few':'just a few',
'I (never|bought|searched)':'I have $1',
'is your done':'is that you are done',
'therfore':'therefore',
'unrestrictive':'nonrestrictive',
'm((icro[s$]oft)|s)':'Microsoft',
'win(dow[s$])?[ ]*(XP|vista|7)':'Windows $2',
'pl[ /-]?sql':'PL/SQL',
't[ /-]?sql':'T-SQL'
};
for (var wrong_word in replacements)
body = body.replace(new RegExp('\\b' + wrong_word + '\\b', 'gi'), replacements[wrong_word]);
// This second batch of replacements can apply anywhere in the text
var variableReplacements = {
'\\bwindow$':'Windows',
'\\b(a)n(?= +(?![aeiou]|HTML|user|hour))':'$1',
'\\b(a)(?= +[aeiou](?!ser))':'$1n',
'^[ ]*((?:any|some)(?:one|body)) have':function (_,first) { return 'Does ' + first.toLowerCase() + ' have'; }
};
for (var wrong_word in variableReplacements)
body = body.replace(new RegExp(wrong_word, 'gim'), variableReplacements[wrong_word]);
// These names will be properly capitalized and excessive (or missing) whitespace inside these terms will be replaced
var trademarks = [
"2D", "3D", "AMD", "Android", "AppleScript", "ASUS", "ATI", "BIOS", "Bluetooth", "Chrome", "Chromium", "CMOS", "CPU", "DirectX", "DivX", "DVD", "Eclipse", "Edubuntu", "Eee PC", "FileZilla", "Firefox", "FireWire", "GarageBand", "GHz", "Gmail", "GNOME", "Google", "HDD", "iBookstore", "iCal", "iChat", "IDE", "iLife", "Intel", "iMac", "iMovie", "iOS", "IP", "iPad", "iPhone", "iPhoto", "iPod", "ISP", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "KDE", "Kubuntu", "Lenovo", "Linux", "Lubuntu", "LXDE", "MacBook", "MacPorts", "MHz", "mIRC", "MobileMe", "Mozilla", "MySQL", "Notepad", "Nvidia", "OpenGL", "Oracle", "OS X", "PayPal", "POSIX", "PowerBook", "PowerPoint", "QuickTime", "RAM", "SATA", "Spotify", "SSD", "Stack Overflow", "Super User", "TCP", "TextEdit", "TextMate", "ThinkPad", "Ubuntu", "UDP", "Unity", "UNIX", "USB", "Vista", "VPN", "VMware", "WebKit", "Wi-Fi", "Windows", "Windows Phone", "WordPress", "Xcode", "Xfce", "XMLHttpRequest", "Xserve", "Xubuntu"
];
// Replace trademarks
for (var trademark in trademarks)
body = body.replace(new RegExp('\\b' + trademarks[trademark].replace(" ","\s*").replace("-","\s*") + '\\b', 'gi'), trademarks[trademark]);
var endings = {
'essisary':'ecessary',
'harachters':'haracters',
'abey':'aybe',
'efull':'eful',
'rafic':'raphic',
'eparatable':'eparable','eperatable':'eparable','eperable':'eparable',
'ylise':'ylize',
'tivly':'tively',
'mmount':'mount',
'rmenent':'rmanent'
};
for (var wrong_word in endings)
body = body.replace(new RegExp(wrong_word + '\\b', 'gi'), endings[wrong_word]);
return body;
},
GetRidOfSmileys : function (body) {
body = body
.replace(/:-\)/gi, '').replace(/:\)/gi, '').replace(/:-\(/gi, '').replace(/(?![A-Za-z0-9]):\(/gi, '')
.replace(/;-\)/gi, '').replace(/;\)/gi, '').replace(/:-D/gi, '').replace(/(?![A-Za-z0-9]):D/gi, '')
.replace(/:-O/gi, '').replace(/(?![A-Za-z0-9]):O/gi, '').replace(/:-S/gi, '').replace(/(?![A-Za-z0-9]):S/gi, '')
;return body;
},
CorrectFileSizes : function(body) {
body = body
// Only apply to full lower-case. Otherwise mb is converted to Mb.
.replace(/([0-9]) ?(k|m|g|t)(i)?b\b/g, function(match,$1,$2,$3,$4) { return $1 + ' ' + $2.toUpperCase() + (($3)?$3:'') + 'B'; } )
// When already upper-case, just make sure the spacing is correct.
.replace(/([0-9])(K|M|G|T)(i)?B\b/g, function(match,$1,$2,$3,$4) { return $1 + ' ' + $2 + (($3)?$3:'') + 'B'; } )
;return body;
},
CorrectRepeatedPunctuation : function(body) {
body = body
// Triple or more commas are likely an intended elipsis
.replace(/,,,+/gi, function(match) { return match.replace(/,/g, '.'); })
// Correct multiple commas into one
.replace(/,+/gi, ',')
// More than three dots should be only three, an elipsis
.replace(/\.\.\.+/gi, '@@@²@@@')
// Correct multiple dots into one
.replace(/\.+/gi, '.')
// Correct full stops interspersed by spaces
.replace(/[.]([ ]+[.]+)+/gi, '.')
// Correct full stop followed by other punctuation
.replace(/\.\?/gi, '?')
.replace(/\.:/gi, ':')
// Reduce multiple question mark
.replace(/\?+/gi, '?')
// Reduce multiple exclamation mark
.replace(/!+/gi, '!')
;return body;
},
ProperSpacesAroundPunctuationMarks : function (body) {
body = body
// Fix (insert) space after punctuation mark; remove spaces before punctuation mark
.replace(/((?:(?:https?|ftp):\/\/|www\.)[a-zA-Z0-9\/.%_#~-]+)?([ ]*[.:!?,]+(?!\/)[ ]*?(?:(?=[^ ])|([ ]{2,}$)))/gim, function (orig,look,match,softLineBreak) { return look?orig:match.trim()[0] + (softLineBreak?' ':' '); })
// Handle special cases
.replace(/www\.[ ]/gi, 'www.')
.replace(/:[ ]\/\//gi, '://')
.replace(/![ ]\[/gi, '![')
// Fix erroneous spaces inserted by function in front of `\`, `/`, `)`, `*` and end of line.
.replace(/([.:!?,])[ ]([\\\/)*]|$)/gim, '$1$2')
// Add a space before `(` if after letter, number or punctuation.
.replace(/(\w|[.:!?,])([(])/gi, '$1 $2')
// XXX Breaks: "Oh. So, that was cool." into "Oh.so, that was cool." which is unacceptable. We will want to remove any words from here that could be used after a "." and then uncomment it.
//.replace(/\.[ ]+(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|as|asia|at|aw|ax|az|ba|bb|be|bf|bg|bh|bi|biz|bj|bm|bo|br|bs|bt|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|es|eu|fi|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gov|gp|gq|gr|gs|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|im|in|info|int|io|iq|ir|is|it|je|jo|jobs|jp|kg|ki|km|kn|kr|ky|kz|la|lc|li|lk|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mn|mo|mobi|mp|mq|mr|ms|mu|museum|mv|mw|mx|my|na|name|nc|ne|net|nf|nl|no|nr|nu|org|pa|pe|pf|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|so|sr|st|su|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|travel|tt|tv|tw|ua|ug|us|uz|va|vc|vg|vi|vn|vu|wf|ws|yt|xxx)\b/gi, function(match) { return '.' + match.toLowerCase(); })
//.replace(/\.It'/gm, '. It\'')
;return body;
},
RemoveUnnecessarySentences : function(body) {
body = body
.replace(/(?:, |many )?(?:thank|k?thn?x(?:bye)?)(?:s|(?: |-)you)?(?: (?:so|very) much)?(?:\s?(?:,|-)(?:[\w\s]+)| :-?\)| a lot| and regards| for(?: any| the| your)? (?:help|ideas)| in advance)?[.|!]?/i, '')
.replace(/Thanks for help(?:ing)?(?: me)?[.:!, ]*/gi, '')
.replace(/^[A-Za-z ]*hope this helps[A-Za-z ?!.]*$/gim, '')
.replace(/I have this (?:problem|issue)(?: too)?[.:!, ]*/gi, '')
.replace(/What [cw]ould be the (?:problem|issue)[.:!?, ]*/gim, '')
.replace(/I am stumped[.:!, ]*/gi, '')
.replace(/(?:Please|(?!(?:may|can|will|click(?: on| on the)?) )Help(?! (?:files?|document(?:ation|s?)))\b|Thanks?)[.?!,]*/gi, '')
.replace(/^Wow[.:!?, ]*/gi, '')
.replace(/^ *[b|B]ut */gim, '')
;return body;
},
CorrectLists : function(body) {
body = body
.replace(/^( *[0-9]+\))/gim, function(match) { return match.replace(')', '.'); })
.replace(/^[A-Za-z]\)/img, function(match) { return " " + ( match.substr(0,1).toUpperCase().charCodeAt(0) - 64 ) + "."; })
.replace(/^ *-(\w)/gim, function(match,letter) { return "- " + letter; })
.replace(/:[\r\n ]*[\r\n][\r\n ]*-/g, function(match) { return ":\n\n-"; })
;return body;
},
CorrectFirstLetters : function(body) {
body = body
// Capitalize the first letter of each new sentence.
.replace(/[a-z]/i, function (match) { return match.toUpperCase(); })
.replace(/(\r?\n[ ]*\r?\n)([a-z])/gm, function(match,prefix,letter) { return prefix + letter.toUpperCase(); })
.replace(/\b([A-Za-z]+)((?:\.|\?|\!)[ ])([a-z])/gi, function(_, word, one, two) { return word + one + two.toUpperCase(); })
.replace(/((?:\. [ \r\n]*)|(?:^[ ]*- [ \r\n]*))([a-z])/gm, function(match,prefix,letter) { return prefix + letter.toUpperCase(); })
// Handle some special cases
.replace(/Http:\/\//gi, 'http://')
.replace(/Ftp:\/\//gi, 'ftp://')
.replace(/Www\./gi, 'www.')
// i.e.
.replace(/(\s|^|[(])i\.[ ]e\.[ ](\S)([' ]*)/gim, function(match, pre, postFirstChar, postSecondChar) { return pre + 'i.e. ' + ((postFirstChar.toLowerCase()=='i' && postSecondChar) ? postFirstChar.toUpperCase() : postFirstChar.toLowerCase()) + postSecondChar; })
// e.g.
.replace(/(\s|^|[(])e\.[ ]g\.[ ](\S)([' ]*)/gim, function(match, pre, postFirstChar, postSecondChar) { return pre + 'e.g. ' + ((postFirstChar.toLowerCase()=='i' && postSecondChar) ? postFirstChar.toUpperCase() : postFirstChar.toLowerCase()) + postSecondChar; })
// P.S.
.replace(/(\s|^|[(])p\.[ ]s\.[ ](\S)([' ]*)/gim, function(match, pre, postFirstChar, postSecondChar) { return pre + 'P.S. ' + ((postFirstChar.toLowerCase()=='i' && postSecondChar) ? postFirstChar.toUpperCase() : postFirstChar.toLowerCase()) + postSecondChar; })
;return body;
},
FixEnumerations : function(body) {
body = body
// XXX Breaks for things like "A and B did X, then A and B did Y, then A and B did Z." and "A, B did X, then A, B did Y, then A and B did Z."
//.replace(/ and (?=[^,.!?\n]*? and )/gi, function(match) { return ','; })
;return body;
},
ApplyFixedWidth : function(body) {
body = body
// Hostnames as fixed-width.
.replace(/(?:[\s^])(\w+(\.\w+){2,})\b(?!\/)/gi, function(match,url) { return match.replace( url, '`' + url + '`' ); })
;return body;
},
AddKeyboardMarkup : function(body) {
body = body
.replace(/\bf[1-9][0-2]?\b/gi, function(match) { return "<kbd>" + match + "</kbd>"; })
;return body;
},
CorrectScriptMistakes : function(body) {
body = body
// CorrectRepeatedPunctuation
.replace(/@@@²@@@/gi, '...')
// Replace punctutation marks at the end that don't make any sense.
.replace(/[,;][ \r\n]*$/, '.')
;return body;
},
};
var MARKER = "###µ";
// Find code blocks and URLs and replace them by markers.
var retain = originalBody.match(/(^[ ]{4}(.|([\r\n][ ]{4}))*)|`[^`]*`|<pre>[^<]*<\/pre>|[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gim) || [];
correctedBody = originalBody;
for (var i = 0; i < retain.length; ++i)
correctedBody = correctedBody.replace(retain[i], MARKER + i);
// Find extra stuff that can be turned into code blocks.
correctedBody = correctedBody
.replace(/\b .exe\b/gi, function(_) { retain.push(' `.exe`'); return MARKER + (retain.length - 1); })
.replace(/\babout:config\b/gi, function(_) { retain.push('`about:config`'); return MARKER + (window.retain.length - 1); });
// Run cleanup process.
for (var correction in corrections)
correctedBody = corrections[correction](correctedBody);
// Place code blocks and URLs back in.
correctedBody = correctedBody.replace(new RegExp(MARKER + '([0-9]+)', 'gi'), function(_,num) { return retain[num]; });
return correctedBody;
});
EmbedFunctionOnPage('diffString', function(o, n) {
o = o.replace(/\s+$/, '');
n = n.replace(/\s+$/, '');
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
var str = "";
var oSpace = o.match(/\s+/g) || []; oSpace.push("\n");
var nSpace = n.match(/\s+/g) || []; nSpace.push("\n");
if (out.n.length == 0) {
for (var i = 0; i < out.o.length; i++)
str += '<span class="diff-delete">' + out.o[i] + oSpace[i].replace(/\n/g, ' ') + "</span>";
} else {
if (out.n[0].text == null)
for (n = 0; n < out.o.length && out.o[n].text == null; n++)
str += '<span class="diff-delete">' + out.o[n] + oSpace[n].replace(/\n/g, ' ') + "</span>";
for (var i = 0; i < out.n.length; i++) {
if (out.n[i].text == null) {
str += '<span class="diff-add">' + out.n[i] + nSpace[i] + "</span>";
} else {
var pre = "";
for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++)
pre += '<span class="diff-delete">' + out.o[n] + oSpace[n].replace(/\n/g, ' ') + "</span>";
str += " " + out.n[i].text + nSpace[i] + pre;
}
}
}
return str.replace(/\b <\/span> \b/gi, '</span> ');
});
EmbedFunctionOnPage('diff', function(o, n) {
var ns = new Object();
var os = new Object();
for (var i = 0; i < n.length; i++) {
ns[n[i]] = ns[n[i]] || { rows: new Array(), o: null };
ns[n[i]].rows.push(i);
}
for (var i = 0; i < o.length; i++) {
os[o[i]] = os[o[i]] || { rows: new Array(), n: null };
os[o[i]].rows.push(i);
}
for (var i in ns)
if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
n[ns[i].rows[0]] = { text: n[ns[i].rows[0]], row: os[i].rows[0] };
o[os[i].rows[0]] = { text: o[os[i].rows[0]], row: ns[i].rows[0] };
}
for (var i = 0; i < n.length - 1; i++)
if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null && n[i + 1] == o[n[i].row + 1]) {
n[i + 1] = { text: n[i + 1], row: n[i].row + 1 };
o[n[i].row + 1] = { text: o[n[i].row + 1], row: i + 1 };
}
for (var i = n.length - 1; i > 0; i--)
if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null && n[i - 1] == o[n[i].row - 1]) {
n[i - 1] = { text: n[i - 1], row: n[i].row - 1 };
o[n[i].row - 1] = { text: o[n[i].row - 1], row: i - 1 };
}
return { o: o, n: n };
});
EmbedFunctionOnPageAndExecute(function() {
$(document).bind('DOMNodeInserted', function(event) {
var toolbar = $(event.target);
if (toolbar.attr('id') === undefined || toolbar.attr('id').indexOf('wmd-button-row') !== 0)
return;
window.setTimeout(function() {
AddToolbarButton(toolbar, 'http://i.stack.imgur.com/wWIIc.png', 'Stack Exchange Post Editor',
function() {
var title = toolbar.parents('.post-editor').find('#title');
if (title.length) {
var new_title = CorrectTitle(title.attr('value'));
title.attr('value', new_title);
$('#question-header .question-hyperlink').text(new_title)
}
var editor = toolbar.parents('.wmd-container').find('.wmd-input');
var original = editor.val();
var corrected = CorrectBody(original);
editor.val(corrected);
toolbar.parents('.post-editor').find('.wmd-preview').html(diffString(original, corrected).replace(/\n/g, '<br />'));
});
}, 100);
window.setTimeout(function() {
AddToolbarButton(toolbar, 'http://i.stack.imgur.com/wWIIc.png', 'Keyboard Markup',
function() {
var editor = toolbar.parents('.wmd-container').find('.wmd-input');
var selectionStart = editor[ 0 ].selectionStart;
var selectionEnd = editor[ 0 ].selectionEnd;
if( ' ' == editor.val().substring( selectionEnd - 1, selectionEnd ) ) {
--selectionEnd;
}
editor.val(
editor.val().substring( 0, selectionStart )
+ "<kbd>"
+ editor.val().substring( selectionStart, selectionEnd )
+ "</kbd>"
+ editor.val().substring( selectionEnd )
);
});
}, 100);
});
});