-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundpage.js
65 lines (62 loc) · 2.39 KB
/
backgroundpage.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
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "speak") {
chrome.tts.speak(request.toSay, { rate: 0.8, 'lang': 'th-TH', onEvent: function(event) {}}, function() {});
}
else if (request.method == "save_word") {
/*
request.word
request.definition
var xhttp = new XMLHttpRequest();
var method = 'POST';
xhttp.onload = function() {
//sendResponse(xhttp.responseText);
};
xhttp.onerror = function() {
// Do whatever you want on error. Don't forget to invoke the
// callback to clean up the communication port.
//sendResponse();
};
xhttp.open(method, 'http://someserver.com/', true);
if (method == 'POST') {
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhttp.send(request.data);
return true; // prevents the callback from being called too early on return
*/
}
else if (request.method == "getStorage") {
console.log("fetching data");
chrome.storage.sync.get(['status', 'lang', 'font_size'], function (data) {
//console.log("got data"+data);
//console.log("status="+data['status']);
chrome.tabs.query({}, function(tabs) { // {active: true, currentWindow: true}
for (var i=0; i<tabs.length; ++i) {
chrome.tabs.sendMessage(tabs[i].id, data);
}
});
});
console.log("done w data");
}
else if (request.action == "xhttp") {
var xhttp = new XMLHttpRequest();
var method = request.method ? request.method.toUpperCase() : 'GET';
xhttp.onload = function() {
sendResponse(xhttp.responseText);
};
xhttp.onerror = function() {
// Do whatever you want on error. Don't forget to invoke the
// callback to clean up the communication port.
sendResponse();
};
xhttp.open(method, request.url, true);
if (method == 'POST') {
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhttp.send(request.data);
return true; // prevents the callback from being called too early on return
}
else {
sendResponse({}); // snub them.
console.log("snubbing");
}
});