Skip to content

Commit

Permalink
import files
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Turmel committed Mar 28, 2018
1 parent 9cfc141 commit 4fc8bd4
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.xpi

web-ext-artifacts
126 changes: 126 additions & 0 deletions asterisk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
var selectedNumber = null;
var asteriskServer = null;
var asteriskPort = null;
var asteriskUsername = null;
var asteriskPassword = null;
var asteriskContext = null;
var asteriskProtocol = null;
var asteriskChannel = null;

var getting = browser.storage.local.get("asterisk");
getting.then(getOptions, onError);

function getOptions(result) {
//console.log(result);
asteriskServer = result.asterisk.server || "localhost";
asteriskPort = result.asterisk.port || "8088";
asteriskUsername = result.asterisk.username || "admin";
asteriskPassword = result.asterisk.password || "";
asteriskContext = result.asterisk.context || "default";
asteriskProtocol = result.asterisk.protocol || "";
asteriskChannel = result.asterisk.channel || "";
}

function onError(error) {
console.log(`Error: ${error}`);
}

/*
Called when the item has been created, or when creation failed due to an error.
We'll just log success/failure here.
*/
function onCreated() {
if (browser.runtime.lastError) {
console.log(`Error: ${browser.runtime.lastError}`);
} else {
//console.log("Item created successfully");
}
}

/*
Called when the item has been removed.
We'll just log success here.
*/
function onRemoved() {
//console.log("Item removed successfully");
}

function createMenuItem(number){
browser.menus.create({
id: "callnumber",
type: "normal",
title: "Call " + number,
contexts: ["selection"],
checked: true,
icons: {
"20": "icons/asterisk-20.png",
"32": "icons/asterisk-32.png"
}
}, onCreated);
}

function handleMessage(request, sender, sendResponse) {
//console.log("Message from the content script: " + request.selectedNumber);
selectedNumber = request.selectedNumber;
//sendResponse({response: "Response from background script"});
browser.menus.remove("callnumber");
createMenuItem(request.selectedNumber);
}

browser.runtime.onMessage.addListener(handleMessage);

browser.menus.onClicked.addListener((info, tab) => {
getting = browser.storage.local.get("asterisk");
getting.then(getOptions, onError);
switch (info.menuItemId) {
case "callnumber":
//console.log("Calling X number");
let xhr_login = new XMLHttpRequest();
let xhr = new XMLHttpRequest();
xhr_login.open("GET", "http://" + asteriskServer + ":" + asteriskPort + "/asterisk/mxml?action=Login&Username=" + asteriskUsername + "&Secret=" + asteriskPassword);
xhr_login.send("");
xhr_login.onreadystatechange = function() {
if (xhr_login.readyState == 4 && (xhr_login.status == 200 || xhr_login.status == 0)) {
console.log(xhr_login.responseText);
var response = xhr_login.responseXML;
var success = response.getElementsByTagName("generic")[0].getAttribute('response')
//console.log(success);
if (success == "Success") {
xhr.open("GET", "http://" + asteriskServer + ":" + asteriskPort + "/asterisk/mxml?action=originate&channel=" + asteriskProtocol + "/" + asteriskChannel + "&exten=" + selectedNumber + "&context=" + asteriskContext + "&CallerId=" + asteriskChannel + "&priority=1&codecs=alaw&timeout=5000");
xhr.send("");
}
else {
console.log("Bad authentification, check your credentials");
}
}
};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
console.log(xhr.responseText);
}
};
break;
}
});

/* browser.browserAction.onClicked.addListener((handleClick) => {
selection = window.getSelection().toString().trim();
console.log(selection);
function onGot(tabInfo) {
console.log(tabInfo[0].id);
browser.tabs.sendMessage(tabInfo[0].id, {action:"getSelection"})
.then(response => {
console.log(response.response);
});
}
function onError(error) {
console.log(`Error: ${error}`);
}
var gettingCurrent = browser.tabs.query({active: true});
gettingCurrent.then(onGot, onError);
}); */
29 changes: 29 additions & 0 deletions content-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

browser.runtime.onMessage.addListener(request => {
//console.log("Message from the background script:");
//console.log(request.action);
return Promise.resolve({response: window.getSelection().toString().trim()});
});

function hasNumbers(string)
{
return /\d/.test(string);
}

// Workaround without onBeforeShow for Firefox < 60
// https://bugzilla.mozilla.org/show_bug.cgi?id=1215376
document.addEventListener("selectionchange", function() {
// Check if the selected string has number in it
if (hasNumbers(window.getSelection().toString().trim())) {
var selectedNumber = window.getSelection().toString().trim().replace(/\+/g, "00").replace(/\D/g,'');
//console.log('Selected number is ' + selectedNumber);

var sending = browser.runtime.sendMessage({
selectedNumber: selectedNumber,
action: "selectionText"
});

//runtime.sendMessage();
}
});
Binary file added icons/asterisk-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/asterisk-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/asterisk-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{

"manifest_version": 2,
"name": "Asterisk Click2Dial",
"version": "0.2",

"description": "This is an easy to use extention, select any number on a web page, right click to open the context menu which you can use to call this number.",
"default_locale": "en",
"author": "Antoine Turmel",

"icons": {
"48": "icons/asterisk-48.png"
},

"permissions": [
"storage",
"menus",
"<all_urls>",
"activeTab",
"tabs"
],

"options_ui": {
"page" : "options.html"
},

"background": {
"scripts": ["asterisk.js"]
},

"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content-script.js"]
}]

}
44 changes: 44 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
</head>

<body>

<form>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Server</label></div>
<div class="browser-style"><input type="text" id="server"><label for=""></label></div>
</div>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Port</label></div>
<div class="browser-style"><input type="text" id="port"><label for=""></label></div>
</div>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Username</label></div>
<div class="browser-style"><input type="text" id="username"><label for=""></label></div>
</div>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Password</label></div>
<div class="browser-style"><input type="password" id="password"><label for=""></label></div>
</div>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Context</label></div>
<div class="browser-style"><input type="text" id="context"><label for=""></label></div>
</div>
<div class="panel-formElements-item browser-style">
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Protocol</label></div>
<div class="browser-style"><input type="text" id="protocol"><label for=""></label></div>
<div class="label"><label class="browser-style-label" for="" data-l10n-id="">Channel</label></div>
<div class="browser-style"><input type="text" id="channel"><label for=""></label></div>
</div>
<button type="submit">Save</button>
</form>

<script src="options.js"></script>

</body>

</html>
37 changes: 37 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function saveOptions(e) {
e.preventDefault();
var server = document.querySelector("#server").value;
var port = document.querySelector("#port").value;
var username = document.querySelector("#username").value;
var password = document.querySelector("#password").value;
var context = document.querySelector("#context").value;
var protocol = document.querySelector("#protocol").value;
var channel = document.querySelector("#channel").value;
browser.storage.local.set({
asterisk: {server: server, port: port, username:username, password:password, context:context,protocol:protocol,channel:channel}
});
}

function restoreOptions() {

function setCurrentChoice(result) {
//console.log(result);
document.querySelector("#server").value = result.asterisk.server || "localhost";
document.querySelector("#port").value = result.asterisk.port || "8088";
document.querySelector("#username").value = result.asterisk.username || "admin";
document.querySelector("#password").value = result.asterisk.password || "";
document.querySelector("#context").value = result.asterisk.context || "default";
document.querySelector("#protocol").value = result.asterisk.protocol || "";
document.querySelector("#channel").value = result.asterisk.channel || "";
}

function onError(error) {
//console.log(`Error: ${error}`);
}

var getting = browser.storage.local.get("asterisk");
getting.then(setCurrentChoice, onError);
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);

0 comments on commit 4fc8bd4

Please sign in to comment.