-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackground.js
87 lines (76 loc) · 2.79 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
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
/*
* Copyright (c) 2018 Robin Mulloy http://robin.mulloy.ca All rights reserved.
* Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
*/
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
if(request.action == 'create_menu') {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
"title" : "Dial " + request.selection,
"contexts" : ["selection"],
"id" : "contextSelection",
"onclick" : onClickHandler
});
});
sendResponse({'result':'created','message':request.action, 'selection':request.selection});
}
else if(request.action == 'delete_menu') {
chrome.contextMenus.removeAll();
sendResponse({'result':'removed','message':request.action});
}
else {
// uh, not sure what this is??
sendResponse({'notsure':request});
}
return true;
});
var settings = [
'username',
'password',
'domain',
'dest_cid_name',
'dest_cid_number',
'src',
'auto_answer',
'rec',
'ringback'
];
function onClickHandler(info, tab) {
var phoneNum = info.selectionText.replace(/[^+\d]+/g,'');
var url = '';
chrome.storage.local.get(settings, function(items) {
if( !items.username || !items.password ) {
alert('Options have not been defined.');
}
else {
//console.log("Calling: " + phoneNum);
url = items.domain + "/app/click_to_call/click_to_call.php?src_cid_name=WebDialer" +
'&src_cid_number=' + phoneNum +
'&dest_cid_name=' + items.dest_cid_name +
'&dest_cid_number=' + items.dest_cid_number +
'&src=' + items.src +
'&dest=' + phoneNum +
'&auto_answer=' + items.auto_answer +
'&rec=' + items.rec +
'&ringback=' + items.ringback;
var login = 'username=' + items.username + '&password=' + items.password;
// alert(url);
//alert(login);
// do a background post
var http = new XMLHttpRequest();
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4) {
if(http.status == 200) {
// alert(http.responseText);
}
else {
alert('ERROR: PBX click_2_call; HTTP status: '+http.status);
}
}
}
http.send(login);
}
});
};