-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
172 lines (139 loc) · 4.94 KB
/
Copy pathbootstrap.js
File metadata and controls
172 lines (139 loc) · 4.94 KB
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
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/AddonManager.jsm");
spellCheckEngines = {};
installedDictionaries = null;
windows = [];
blockers = {};
install = function () {};
uninstall = function () {};
startup = function () {
resetState();
var windowEnumerator = Services.wm.getEnumerator("navigator:browser");
while(windowEnumerator.hasMoreElements()){
register(windowEnumerator.getNext());
}
Services.ww.registerNotification(onWindow);
AddonManager.addAddonListener(addonListener);
};
shutdown = function () {
resetState();
AddonManager.removeAddonListener(addonListener);
Services.ww.unregisterNotification(onWindow);
};
resetState = function(){
resetDictionaries();
for(var i=0;i<windows.length;i++)
unregister(windows[i]);
windows = [];
for(var i=0;i<blockers.length;i++)
blockers.items[i].timer.cancel();
blockers = [];
};
resetDictionaries = function(){
installedDictionaries = null;
spellCheckEngines = {};
};
onWindow = function(subject, topic){
subject = subject.QueryInterface(Components.interfaces.nsIDOMWindow);
if (topic=="domwindowclosed")
unregister(subject);
if (topic=="domwindowopened")
register(subject);
};
refreshDictionaries = function() {
if (installedDictionaries==null){
installedDictionaries = [];
var spellCheckEngine = Components.classes["@mozilla.org/spellchecker/engine;1"].getService(Components.interfaces.mozISpellCheckingEngine);
spellCheckEngine.getDictionaryList(this.installedDictionaries, {});
installedDictionaries = installedDictionaries.value.toString().split(",");
}
};
addonListener = {//whenever an addon is enabled/disabled we refresh the set of dictionaries
onEnabled : function(addon) { resetDictionaries(); },
onDisalbed : function(addon) { resetDictionaries(); }
};
register = function(window){
window.addEventListener("keypress", onKeyPress, false);
windows.push(window);
};
unregister = function(window){
window.removeEventListener("keypress", onKeyPress, false);
windows=windows.filter(function(item){return item!=window;});
};
onKeyPress = function(e){
var target = e.originalTarget;
if (!target)
return;
if (!target.value)
return;
check(target);
};
registerBlocker = function(target){
blockers[target] = {
observe: function(subject, topic, data){
blocker = blockers[target];
delete(blockers[target]);
if (blocker.pending)
check(target);
},
pending: false,
timer: Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer),
};
blockers[target].timer.init(blockers[target], 500, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
};
check = function(target){
refreshDictionaries();
if (!(target instanceof Components.interfaces.nsIDOMNSEditableElement))
return;
target = target.QueryInterface(Components.interfaces.nsIDOMNSEditableElement);
var editor = target.editor;
if (!editor)
return;
var spellChecker = editor.getInlineSpellChecker(false);
if (!spellChecker)
return;//spell checking was never enabled
if (!spellChecker.enableRealTimeSpell)
return;//spell checking is disabled
if (blockers[target]){
blockers[target].pending = true;
return;
}
registerBlocker(target);
var text = target.value;
if (!text)
return;
// figure out the caret position so we only use the text in front of the caret
var cutoffPosition;
if (editor.selection.focusNode != editor.rootElement){
cutoffPosition = editor.selection.focusOffset;
// this is the caret position before the key press so it's potentially of by one
cutoffPosition = cutoffPosition + 1;
}else{
// if the caret is at the end of the text, the focusNode has a strange value and the focusOffset is 2 (not sure if this can only happen in such a case)
cutoffPosition = text.length;
}
if (cutoffPosition > text.length)
cutoffPosition = text.length;
text = text.substring(0,cutoffPosition).replace(/^\s\s*/, '').replace(/\s\s*$/, '').split(/[\s;:,.()\[\]¡!¿?]+/).slice(-10);//this is lame but \W will also throws out umlauts and all sorts of funny characters
var errors = [];
for(var i=0;i<installedDictionaries.length;i++){
var spellCheckEngine = Components.classes["@mozilla.org/spellchecker/engine;1"].getService(Components.interfaces.mozISpellCheckingEngine);
spellCheckEngine.dictionary = installedDictionaries[i];
errors[i] = countErrors(spellCheckEngine,text);
}
var best = 0;
for(var i=0;i<installedDictionaries.length;i++)
if (errors[i]<errors[best])
best=i;
if (installedDictionaries[best]==spellChecker.spellChecker.GetCurrentDictionary())
return;
spellChecker.spellChecker.SetCurrentDictionary(installedDictionaries[best]);
spellChecker.spellCheckRange(null);
};
countErrors = function(engine, tokens) {
var count=0;
for(var i=0;i<tokens.length;i++)
if(!engine.check(tokens[i]))
count++;
return count;
};