Skip to content

Commit

Permalink
Added caller ID selection (#1)
Browse files Browse the repository at this point in the history
* Added all .pems to .gitignore

* Started adding support for setting caller Id

* Ensure callerId value is being set correctly
  • Loading branch information
joshfarrant authored and adambowles committed Jan 14, 2020
1 parent 0b8b95e commit 468a43c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sc-chrome-plugin.pem
*.pem
sc-chrome-plugin.zip
.DS_Store
3 changes: 3 additions & 0 deletions sc-chrome-plugin/call.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
</div>
<div class="modal-body">
<input type="text" id="number" placeholder="Number" maxlenght="25">
<label for="callerIdPicker">Caller Id:</label>
<select id="callerIdPicker">
</select>
<label class="checkbox"><input id="dialerWithhold" type="checkbox"> <small>Withhold Caller ID</small></label>
<p class="muted"><small>Click the 'dial' button below to call this number from your extension. You can edit the number before dialing or click 'close' to go back.</small></p>
<div id="notification"></div>
Expand Down
21 changes: 20 additions & 1 deletion sc-chrome-plugin/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function dial() {

var call = $('#number').val();

chrome.extension.sendMessage({number: call, numberHold: withhold}, function(response) {
var selectedCallerId = $('#callerIdPicker').val();

chrome.extension.sendMessage({number: call, numberHold: withhold, callerId: selectedCallerId}, function(response) {
status = response[0];
message = response[1];

Expand Down Expand Up @@ -99,4 +101,21 @@ $(document).ready(function() {
$('#number').val(number);
}

try {
var outgoingCallerIds = localStorage[localStorage['loginUsername'] + '_outgoingCallerIds'];

JSON.parse(outgoingCallerIds).forEach((outgoingCallerId) => {
if (!outgoingCallerId.allowCalls) return;

var newItem = $("<option></option>").attr("value",outgoingCallerId.uri).text(outgoingCallerId.number);

if (outgoingCallerId.uri == localStorage[localStorage['loginUsername'] + '_prefMainExtensionDefaultCallerId']) {
newItem.attr("selected", "selected");
}
$('#callerIdPicker').append(newItem);
});

} catch (err) {
console.error('Error getting outgoingCallerIds: ', err);
}
});
11 changes: 6 additions & 5 deletions sc-chrome-plugin/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var notification_timeout = 5; // Minutes
// Add listener for messages passed from the content.js script
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.number != null) { sendResponse(tryCall(request.number, request.numberHold)); }
if (request.number != null) { sendResponse(tryCall(request.number, request.numberHold, request.callerId)); }
if (request.sms != null) { sendResponse(sendSMS(request.sms, request.message, request.from)); }
if (request.clickableEnabled != null) { sendResponse(clickableEnabled()); }
if (request.connectStream != null) { attempts = 0; setupNotifications(); }
Expand Down Expand Up @@ -391,7 +391,7 @@ function sendSMS(number, message, from) {

}

function tryCall(number, numberHold) {
function tryCall(number, numberHold, callerId) {

log('makeCall - Number: ' + number);
log('[SCCE] Call number. Number: ' + number);
Expand All @@ -401,15 +401,15 @@ function tryCall(number, numberHold) {
}

var realNumber = fixNumber(number);
return makeCall(realNumber);
return makeCall(realNumber, callerId);
}

function fixNumber(number) {

return number.replace(/(?:\+44|\(|\)|-|\s)/g, "");
}

function makeCall(number) {
function makeCall(number, callerId) {

var endpoint = localStorage[localStorage['loginUsername'] + '_prefMainExtension'];
var username = localStorage["loginUsername"];
Expand All @@ -422,7 +422,8 @@ function makeCall(number) {
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({ type: "call",
endpoint: endpoint,
to: number
to: number,
callerId: callerId,
}));

var status = xmlhttp.status;
Expand Down
41 changes: 30 additions & 11 deletions sc-chrome-plugin/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function showWelcome() {
function showDialer() {
hide();
showMenu();
getOutgoingNumbers();
numbersAutoComplete("#dialerNumber", true, true);
$('#blockDialer').fadeIn(400);
$("#menuDialer").addClass("active");
Expand Down Expand Up @@ -248,8 +249,13 @@ function saveSettings() {
if ( $('#extensions').val() != "notset" ) {
var extension = $('#extensions').val();

var shortNumber = localStorage[localStorage['loginUsername'] + '_' + $('#extensions').val()];

var selectedExtension = JSON.parse(localStorage[localStorage['loginUsername'] + '_localExtensions']).find(x => x.value === shortNumber);

localStorage[localStorage['loginUsername'] + '_prefMainExtension'] = extension;
localStorage[localStorage['loginUsername'] + '_prefMainExtensionShort'] = localStorage[localStorage['loginUsername'] + '_' + $('#extensions').val()]
localStorage[localStorage['loginUsername'] + '_prefMainExtensionShort'] = shortNumber;
localStorage[localStorage['loginUsername'] + '_prefMainExtensionDefaultCallerId'] = selectedExtension.defaultCallerId;

// Set up is done, so we set the _loginSetup value to done
localStorage[localStorage['loginUsername'] + "_loginSetup"] = 'done';
Expand Down Expand Up @@ -438,8 +444,9 @@ function dial() {
var withhold = 0;
}

var selectedCallerId = $('#callerIdPicker').val();
// We send the number to the background page (background.js) as we use this in some other places as well
chrome.extension.sendMessage({number: call, numberHold: withhold}, function(response) {
chrome.extension.sendMessage({number: call, numberHold: withhold, callerId: selectedCallerId}, function(response) {
// Status is the numeric code of the request
status = response[0];
// The mesage contains any validation errors etc
Expand Down Expand Up @@ -503,7 +510,7 @@ function getExtensions() {

lable = item['name'] + ' - ' + item['shortNumber'];
value = item['shortNumber'];
localExtensions.push({label: lable, value: value, category: "Extensions"});
localExtensions.push({label: lable, value: value, category: "Extensions", defaultCallerId: item['defaultCallerId']});

localStorage[localStorage['loginUsername'] + '_' + item['uri']] = item['shortNumber'];

Expand Down Expand Up @@ -824,15 +831,27 @@ function getOutgoingNumbers(type) {
if (xmlhttp.status === 200) {
obj = JSON.parse(xmlhttp.responseText);

for (var item in obj.items) {
if (obj.items[item].allowCalls == true) {
label = obj.items[item].number;
value = obj.items[item].number;
// Remove any existing options
$('#callerIdPicker').find('option').remove();

localStorage[localStorage['loginUsername'] + '_outgoingCallerIds'] = JSON.stringify(obj.items);

for (var i in obj.items) {
const itemObj = obj.items[i];
var label = itemObj.number;
var value = itemObj.number;

if (itemObj.allowCalls == true) {
allowCalls.push({label: label, value: value, category: "Numbers"});
}
if (obj.items[item].allowSms == true) {
label = obj.items[item].number;
value = obj.items[item].number;

var newItem = $("<option></option>").attr("value",itemObj['uri']).text(label);

if (itemObj.uri == localStorage[localStorage['loginUsername'] + '_prefMainExtensionDefaultCallerId']) {
newItem.attr("selected", "selected");
}
$('#callerIdPicker').append(newItem);

} else if (itemObj.allowSms == true) {
allowSMS.push({label: label, value: value, category: "SMS Enabled"});
}
}
Expand Down
6 changes: 6 additions & 0 deletions sc-chrome-plugin/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ <h3>Loading...</h3>
<label class="checkbox"><input id="dialerWithhold" type="checkbox"> <small>Withhold Caller ID</small></label>
<small id="dialerInfo" class="text-info"></small>

<br />
<br />
<small>Caller Id:</small>
<select id="callerIdPicker">
</select>

<div id="numberDialerButtons">
<button type="submit" id="dial" class="btn btn-info">Dial <i class="icon-chevron-right icon-white"></i></button>
<button id="recent" class="btn" type="button" alt="Recent Calls"><i class="icon-align-justify"></i></button>
Expand Down

0 comments on commit 468a43c

Please sign in to comment.