-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
51 lines (46 loc) · 1.68 KB
/
background.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
// Called when the user clicks on the browser action.
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.message == "show") {
chrome.pageAction.show(sender.tab.id);
sendResponse({});
}
if (request.message == "convert") {
sendToService(request.html, false);
sendResponse({});
}
if (request.message == "convertAndSend") {
sendToService(request.html, true);
sendResponse({});
}
});
//Sends HippoService the selected text
function sendToService(html, shouldEmail) {
var params = "email[text]="+encodeURIComponent(html);
var xhr = new XMLHttpRequest();
// xhr.open('POST','http://hyppo.herokuapp.com/emails', true);
xhr.open('POST','http://localhost:3000/emails', true);
// xhr.open('POST','http://privacy.omadahealth.com:3000/emails', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
(shouldEmail) ? callSendEmail(xhr.responseText) : callDisplayImage(xhr.responseText);
}
if (xhr.readyState == 4 && xhr.status == 401) {
chrome.tabs.create( {url:''} )
}
}
xhr.send(params);
}
//Initiates the displayInEmail method in content_script.js
function callDisplayImage(text) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, {message: "displayImage", emailText: text}, function(response) {});
});
}
//Initiates the callSendEmail method in content_script.js
function callSendEmail(text) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, {message: "sendEmail", emailText: text}, function(response) {});
});
}