This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
popup.js
197 lines (173 loc) · 7.62 KB
/
popup.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
function Handset(ip, protocol, password) {
var self = this;
this.ip = ip;
this.protocol = protocol;
this.password = password;
this.api_status = "/cgi-bin/api-get_phone_status";
this.api_start_call = "/cgi-bin/api-make_call";
this.api_phone_operation = "/cgi-bin/api-phone_operation";
this.api_sys_operation = "/cgi-bin/api-sys_operation";
this.status = "unknown";
this.update_delay = 2000;
this.updateStatus = function (status, callback) {
//console.log("status: " + status);
if (document.getElementById("phone_details_title")) document.getElementById("phone_details_title").remove();
document.getElementById("phone_details_ip").innerHTML = "<strong>IP Address: </strong>" + self.ip;
if (status === "available") self.status = "Ready to Dial";
if (status === "busy") self.status = "Busy";
document.getElementById("phone_details_status").innerHTML = "<strong>Status: </strong>" + self.status;
if(callback) callback(); //check before calling it.
};
this.getStatus = function (callback) {
//Call the handset"s api
var callUrl = this.protocol + this.ip + this.api_status + "?passcode=" + this.password;
//console.log(callUrl);
var x = new XMLHttpRequest(); //Make a new http request
x.open("GET", callUrl); // Make it a get request
x.responseType = "json"; // The handset API responds with JSON - var"s parse it
x.onload = function () {
// Parse and process the response from the handset
var response = x.response;
//console.log("response:\n" + JSON.stringify(response, null, 4)); // Output the response - for debugging
if (!response || !response.response) {
console.log("No response from Handset! Make sure the IP is correct...");
return;
}
if (typeof(response.response) !== "string") {
console.log("Unexpected response from the Handset\"s API!");
return;
}
//console.log(result); // Output the response - for debugging
if(callback) callback(response.body); //check before calling it.
};
x.send(); // Send API call
};
this.startCall = function (number, account, callback) {
var opt = {
type: "basic",
title: "Dialing Number",
message: number,
iconUrl: "assets/icons/icon.png",
priority: 2,
buttons: [{title:"Dismiss"}, {title:"End Call"}]
};
chrome.notifications.create("", opt);
//Call the handset"s api
var callUrl = this.protocol + this.ip + this.api_start_call + "?phonenumber=" + number +
"&account=" + account + "&password=" + this.password;
var x = new XMLHttpRequest(); //Make a new http request
x.open("GET", callUrl); // Make it a get request
x.responseType = "json"; // The handset API responds with JSON - var"s parse it
x.onload = function () {
// Parse and process the response from the handset
var response = x.response;
//console.log("response:\n" + JSON.stringify(response, null, 4)); // Output the response - for debugging
if (!response || !response.response) {
console.log("No response from Handset! Make sure the IP is correct...");
return;
}
if (typeof(response.response) !== "string") {
console.log("Unexpected response from the Handset\"s API!")
return;
}
if (callback) callback(response); //check before calling it.
};
x.send(); // Send API call
};
this.startCallTyped = function () {
var number_to_dial = document.getElementById("type_to_dial_number").value;
self.startCall(number_to_dial, "0");
};
this.sendOperation = function (operation, sys, callback) {
var callUrl;
if (sys) callUrl = this.protocol + this.ip + this.api_sys_operation + "?request=" + operation + "&passcode=" + this.password;
else callUrl = this.protocol + this.ip + this.api_phone_operation + "?cmd=" + operation + "&passcode=" + this.password;
var x = new XMLHttpRequest(); //Make a new http request
x.open("GET", callUrl); // Make it a get request
x.responseType = "json"; // The handset API responds with JSON - var"s parse it
x.onload = function () {
// Parse and process the response from the handset
var response = x.response;
//console.log("response:\n" + JSON.stringify(response, null, 4)); // Output the response - for debugging
if (!response || !response.response) {
console.log("No response from Handset! Make sure the IP is correct...");
return;
}
if (typeof(response.response) !== "string") {
console.log("Unexpected response from the Handset\'s API!")
return;
}
if (callback) callback(response); //check before calling it.
};
x.send(); // Send API call
};
this.notify = function (title, message) {
var opt = {
type: "basic",
title: title,
message: message,
iconUrl: "assets/icons/icon.png",
priority: 2,
buttons: [{title:"Dismiss"}]
};
chrome.notifications.create("", opt);
};
this.acceptCall = function () {
self.sendOperation("acceptcall");
self.notify("Accepted Call", "");
};
this.endCall = function () {
self.sendOperation("endcall");
self.notify("Ended Call", "");
};
this.holdCall = function () {
self.sendOperation("holdcall");
self.notify("Toggling Call Hold", "");
};
this.rejectCall = function () {
self.sendOperation("rejectcall");
self.notify("Rejecting Call", "");
};
this.reboot = function () {
self.sendOperation("REBOOT", 1);
self.notify("Rebooting Handset", "");
};
this.getIP = function () {
chrome.storage.sync.get({
ip: '192.168.0.10'
}, function(items) {
self.ip = items.ip;
console.log("Updated IP to: " + items.ip);
return items.ip;
});
}
}
var phone = new Handset("10.0.2.30", "http://", "admin");
phone.getIP();
console.log("IP: " + phone.protocol + phone.ip + " password: " + phone.password);
document.addEventListener("DOMContentLoaded", function () {
// Attach listeners to all the buttons
document.getElementById("type_to_dial_submit").addEventListener("click", phone.startCallTyped, false);
document.getElementById("phone_actions_acceptcall").addEventListener("click", phone.acceptCall, false);
document.getElementById("phone_actions_endcall").addEventListener("click", phone.endCall, false);
document.getElementById("phone_actions_holdcall").addEventListener("click", phone.holdCall, false);
document.getElementById("phone_actions_reboot").addEventListener("click", phone.reboot, false);
// Get initial status then do it again every x seconds
phone.getStatus(phone.updateStatus);
window.setInterval(function () {
phone.getStatus(phone.updateStatus)
}, phone.update_delay);
});
chrome.runtime.onMessage.addListener(function (message) {
console.log("number received: " + message);
phone.startCall(message, "0");
});
chrome.notifications.onButtonClicked.addListener(function(notificationId, buttonIndex) {
if (buttonIndex === 1) {
chrome.notifications.clear(notificationId);
phone.endCall();
}
else {
chrome.notifications.clear(notificationId);
}
});