-
Notifications
You must be signed in to change notification settings - Fork 3
/
terkait_communication.js
60 lines (53 loc) · 1.85 KB
/
terkait_communication.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
/**
* This file holds all methods to communicate with the background.html page.
*/
if (!window.terkait) {
window.terkait = {};
}
window.terkait.communication = {};
jQuery.extend(window.terkait.communication, {
sendReq : function (req, opts) {
opts = (opts)? opts : {};
if (req === "getOptions") {
chrome.extension.sendRequest({action: req}, function (theOptions) {
window.terkait.settings = theOptions;
if (typeof window.terkait.vie === "function")
window.terkait.vie = window.terkait.vie();
window.terkait.updateSettings();
});
} else if (req === "openUrl") {
chrome.extension.sendRequest({action: req, options: opts});
}
}
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.action === "createOrDestroy") {
var res = window.terkait.createOrDestroy();
sendResponse(res);
} else if (request.action === "recommend") {
var res = window.terkait.recommend();
sendResponse(res);
} else if (request.action === "analyzeSelection") {
window.terkait.create();
var rng = window.terkait.util.getRangeObject();
if (rng) {
rng.expand("sentence"); // expands to sentence boundaries
var text = rng.toString();
var res = window.terkait.recommend(text);
sendResponse(res);
} else {
sendResponse({
error : "No Range could be found!"
});
}
} else if (request.action === "annotateSelectionAs") {
var res = window.terkait.annotate(request["args"]["id"], sendResponse);
sendResponse(res);
} else {
sendResponse({
error : "unknown request!" + request
});
}
});
window.terkait.communication.sendReq("getOptions");
setInterval(function () {window.terkait.communication.sendReq("getOptions");}, 10000);