forked from dweuthen/crx-c2d-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
157 lines (134 loc) · 4.88 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
function getQuerystring(key, default_) {
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
function normalizePhoneNumber(number) {
// Strip "(0)" from number. We pretend pretend this only occurs when
// number has also an international prefix.
number = number.replace(/\(0\)/g,"");
// Strip some special characters that do not have a real meaning.
number = number.replace(/(\s|\,|\.|\(|\)|\-)/g,"");
// Replace the plus sign with the configured international prefix
number = number.replace("+",localStorage["prefixInternational"]);
// Prepend long distance prefix
number = localStorage["prefixLongDistance"] + number;
return number;
}
function buildUrl(url, number) {
var options = ["phoneUsername","phonePassword","phoneAddress"];
var len = options.length;
for(var i = 0; i < len; i++) {
if (localStorage[options[i]]) {
url = url.replace("$" + options[i], localStorage[options[i]]);
}
}
if (number) {
url = url.replace("$number", number);
}
return url;
}
function setStatus(message, timeout) {
var status = document.getElementById("status");
status.innerHTML = message;
setTimeout(function() {
status.innerHTML = "";
}, timeout);
}
function resizeWindow(width, height) {
document.getElementById("status").style.width = width + 10;
document.getElementById("number").style.width = width;
window.resizeTo(width + 40, height + 130);
}
function doHangup() {
var req = new XMLHttpRequest();
var urlHangup = buildUrl(localStorage["urlHangup"]);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status == 200) {
setStatus("Hanging up.", 3000);
} else {
setStatus("An error occured while hanging up :<br/>" + req.responseText, 5000);
}
}
};
req.open("GET", urlHangup, true);
req.send(null);
}
function doCall() {
var req = new XMLHttpRequest();
var number = normalizePhoneNumber(document.getElementById("number").value);
var urlDial = buildUrl(localStorage["urlDial"],number);
req.onreadystatechange = function() {
if (req.readyState === 4){
if (req.status == 200) {
setStatus("Dialing " + number + "...", 3000);
} else {
setStatus("An error occured while dialing " + number + ":<br/>" + req.responseText, 5000);
}
}
};
req.open("GET", urlDial, true);
req.send(null);
}
function doRefreshDisplay() {
var urlDisplay = buildUrl(localStorage["urlDisplay"]);
var displayRefresh = localStorage["displayRefresh"];
if (urlDisplay) {
var xhr = new XMLHttpRequest()
// Initial request of display
xhr.open('GET', urlDisplay, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
var img = document.getElementById("phonedisplay")
img.onload = function(e) {
resizeWindow(img.width, img.height);
window.URL.revokeObjectURL(img.src); // Clean up after yourself.
};
img.src = window.URL.createObjectURL(xhr.response);
} else {
setStatus("An error occured while retrieving display :<br/>" + req.responseText, 5000);
}
};
xhr.send();
// Refresh display in given interval
setInterval(function () {
xhr.open("GET",urlDisplay, true);
xhr.send();
}, displayRefresh);
} else {
document.getElementById("display").innerHTML = 'No display URL configured.';
}
}
// Send credential if authentication is required
chrome.webRequest.onAuthRequired.addListener(
function() { return {authCredentials: {username: localStorage["phoneUsername"], password: localStorage["phonePassword"]}} },
{urls: ["<all_urls>"]},
['blocking']
);
// Bind event listener for hangup and dial button and enter key
document.querySelector('#hangup').addEventListener('click', doHangup);
document.querySelector('#dial').addEventListener('click', doCall);
document.querySelector('#number').addEventListener('keypress', function(event) {
if (event.keyCode === 13) {
doCall();
event.preventDefault();
}
}, false);
// If there is a query string parameter "number" try to normalize that number
try {
var number = unescape(getQuerystring('number'));
if (number) {
document.getElementById("number").value = normalizePhoneNumber(number);
}
} catch(err) {
alert(err)
}
// Show display
doRefreshDisplay();