forked from jacob-l/SpellCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
73 lines (63 loc) · 2.63 KB
/
background.js
File metadata and controls
73 lines (63 loc) · 2.63 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
/*
* http://developer.chrome.com/extensions/background_pages.html - фоновая страница
*/
function getHighlightedText(text, data) {
var result = '',
start = 0,
noSuggestion = chrome.i18n.getMessage('noSuggestion'),
errorCls = 'error-spell-checker';
data.forEach(function(item) {
var suggestion = item.s.join(', ') || noSuggestion;
result += text.substr(start, item.pos - start);
result += '<span class="{0}" title="{1}">{2}</span>'.
replace('{0}', errorCls).
replace('{1}', suggestion).
replace('{2}', item.word);
start = item.pos + item.len;
});
return result + text.substr(start, text.length - start);
}
function contextMenuHandler(info, tab) {
var text = info.selectionText;
new SpellChecker().check(text).done(function(data) {
var htmlResult = getHighlightedText(text, data);
/*
* http://developer.chrome.com/extensions/tabs.html#method-sendMessage
*
* Отправляет сообщение во встроенный нашим
* расширением скрипт в указанной вкладке.
*
* См. contentScript.js
*/
chrome.tabs.sendMessage(tab.id, {
content: htmlResult
});
}).fail(function() {
console.log(arguments);
chrome.tabs.sendMessage(tab.id, {
content: chrome.i18n.getMessage('checkError')
});
});
};
chrome.contextMenus.onClicked.addListener(contextMenuHandler);
function installContextMenu() {
/*
* http://developer.chrome.com/extensions/contextMenus.html#method-create
*
* Добавляем элемент в контекстное меню.
*/
chrome.contextMenus.create({
title: chrome.i18n.getMessage('checkSpelling'),
//Элемент появится только тогда, когда есть выделенный текст на странице
contexts: ['selection'],
id: 'checkSpelling'
})
}
/*
* http://developer.chrome.com/extensions/runtime.html#event-onInstalled
*
* Это событие возникает, когда расширение впервые устанавливается,
* обновляется до новой версии или Google Chrome обновляется до новой версии.
*/
chrome.runtime.onInstalled.addListener(installContextMenu);
chrome.runtime.onStartup.addListener(installContextMenu);