Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Features #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 99 additions & 14 deletions find_numbers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,99 @@
var anchors = document.getElementsByTagName("a");

for(var i = 0, len = anchors.length; i < len; i++) {
if(anchors[i].getAttribute("href").startsWith("tel:")) {
var tel = anchors[i].getAttribute("href").split(":").pop();
console.log("Grandstream CTC: Replaced " + tel);
anchors[i].setAttribute("href", "#");

anchors[i].onclick = function () {
console.log("Grandstream CTC: Calling " + tel);
chrome.runtime.sendMessage(tel);
}
}
}
chrome.storage.sync.get(['scrape','prefix'], function(result){
//console.log("Result: " + result.scrape);
var prefix = result.prefix;
if (result.scrape == 'yes') {
//Set regex patern for US phone numbers, will match (xxx)xxx-xxxx xxx-xxx-xxxx or xxx.xxx.xxxx
var regex = /((\(\d{3}\) ?)(?!([^<]*>)|(((?!<a).)*<\/a>))|(\d{3}-))?\d{3}-\d{4}(?!([^<]*>)|(((?!<a).)*<\/a>))|(\d{3}\.)?\d{3}\.\d{4}(?!([^<]*>)|(((?!<a).)*<\/a>))/g;
//store web page
var text = $("body").html();

var textNodes = $("body").find('*').contents().filter(function(){
if(regex.test(this.nodeValue)){
//create seperate string of Node when phone numbers detected to allow clean manipulation
strThisNode = this.nodeValue;
//Get parent node also to avoid duplicate nested links being created
strParent = this.parentNode;
//Check if parent node if already a tel link, if not, modify node
if(String(strParent).match(/tel/g) != "tel") {
//start by clearing the node so that we can rewrite it
this.nodeValue = "";
//create array of all phone numbers found in string in case more than 1 occurence
arrNumbers = strThisNode.match(regex);
//process string based on number of phone numbers found
for(var a = 0, aLen = arrNumbers.length; a < aLen; a++ ) {
//Create Array of text not including the number detected in string
arrNode = strThisNode.split(arrNumbers[a]);
//Create link element for phone number found removing special characters and appending 1 for US dialing
var anchor = document.createElement('a');
tel = arrNumbers[a];
tel = tel.replace(/[^0-9*]/g, '');
tellength = tel.length;
if(tellength == 10 ) {
if(prefix != null) {
tel = prefix + tel;
}
}
anchor.setAttribute('href', 'tel:'+tel);
//write the unmodifed phone number as link text
anchor.appendChild(document.createTextNode(arrNumbers[a]));
//get first text element as text node of text array in case there was text before number in original node
var t = document.createTextNode((arrNode[0]));
//Write text to end of existing node
this.parentNode.appendChild(t);
//write assembled achor to end of existing node
this.parentNode.appendChild(anchor);
//remove what was just written to existing node from string being used for processing to avoid duplication
strThisNode = strThisNode.replace(arrNode[0], '');
strThisNode = strThisNode.replace(arrNumbers[a], '');

}
// Write remaining string as text object to node.
var t = document.createTextNode(strThisNode);
this.parentNode.appendChild(t);
}
}
})

}


//Get and store all links on page to array
var anchors = document.getElementsByTagName("a");

for(var i = 0, len = anchors.length; i < len; i++) {
//check each link to see if it is a tel: link
if(anchors[i].getAttribute("href") != null) {
if(anchors[i].getAttribute("href").startsWith("tel:")) {
//if it is a tel: link, get destination and store as string
var tel = anchors[i].getAttribute("href").split(":").pop();
//overwrite link to self anchor
anchors[i].setAttribute("href", "#");
//remove everything except for numbers and * from tel: destination <Q5>
tel = tel.replace(/[^0-9*]/g, '');
//determine length of tel number string <Q5>
tellength = tel.length;
// this will work for US only - if 10 digit number add 1 prefix. Issue was occuring for numbers beginning with conflicting prefixes on platform. <Q5>
if(tellength == 10 ) {
if(prefix != null) {
tel = prefix + tel;
}
}
//Set class attribute of tel links to be the number in order to access when clicked <Q5>
anchors[i].setAttribute("class", tel);
}
}
}

//When any link is clicked send phonenumber stored as class to background <Q5>
$('a').click(function(){
//console.log("Mousedown");
//Get the class attribute of the link and store as toDial number. <Q5>
toDial = ($(this).attr('class'));
//Check to make sure that the toDial value is a valid number, or number beginning with *. If so send to background to send to phone. <Q5>
if (toDial.match(/^\*\d+$/) || toDial.match(/^-?\d+$/)) {
//console.log("class is number, dialing "+toDial);
chrome.runtime.sendMessage(toDial);
}
});

});
4 changes: 4 additions & 0 deletions jquery-2.2.0.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["find_numbers.js"]
"js": ["jquery-2.2.0.min.js","find_numbers.js"]
}
],

Expand All @@ -34,4 +34,4 @@
],

"options_page": "options.html"
}
}
17 changes: 14 additions & 3 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ <h3>Grandstream Click To Call Options</h3>
<body>

<div id="save_ip">
<p id="ip_title">Enter an IP address and click the button to save it.</p>
<p id="ip_title">Enter an IP address and click the button to save it.</p>
<p id="saved"></p>
<form onsubmit="return false">
<input title="Enter an IP address" id="ip_input" type="text" name="ip_input">
<p>IP Address:<input title="Enter an IP address" id="ip_input" type="text" name="ip_input"></p>
<p>Password: <input title="Enter user password" id="pw_input" type="text" name="pw_input"> </p>

<p>Protocol: <select title="Select Protocol" id="prot_input" type="select" name="prot_input">
<option name="http" value="http://">http://</option>
<option name="https" value="https://">https://</option>
</select></p>
<p>Scrape Page:<select title="scrape" id="scrape" type="select" name="scrape">
<option name="no" value="no">No</option>
<option name="yes" value="yes">Yes</option>
</select></p>
<p>Prefix: <input title="prefix" id="prefix" type="text" name="prefix" value=""> </p>
<button id="ip_submit" class="button" type="button">Save</button>
</form>
</div>
Expand All @@ -28,4 +39,4 @@ <h3>Grandstream Click To Call Options</h3>
<footer>
<p id="options_copyright" class="copyright">&copy; 2017 <a target="_blank" href="https://github.com/jabelone">Jaimyn Mayer</a></p>
</footer>
</html>
</html>
42 changes: 29 additions & 13 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
// Saves options to chrome.storage
// Saves values entered to chrome.storage
function save_options() {
var ip = document.getElementById('ip_input').value;
if (validateIP(ip)) {
var pw = document.getElementById('pw_input').value;
var prot = document.getElementById('prot_input').value;
var scrape = document.getElementById('scrape').value;
var prefix = document.getElementById('prefix').value;
if (validateIP(ip,prefix)) {
chrome.storage.sync.set({
ip: ip
ip: ip,
pw: pw,
prot: prot,
scrape: scrape,
prefix: prefix
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('saved');
status.textContent = 'IP saved.';
status.textContent = 'Data saved.';
chrome.runtime.sendMessage("Q5reload");
setTimeout(function() {
status.textContent = '';
}, 4000);
}, 2000);
});

}
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
// Pull values from chrome.storage if stored.
function restore_options() {
// Use default value color = 'red' and likesColor = true.
chrome.storage.sync.get({
ip: '192.168.0.10'
ip: '192.168.0.10',
pw: 'password',
prot: 'http://',
scrape: 'no',
prefix: '1'
}, function(items) {
document.getElementById('ip_input').value = items.ip;
document.getElementById('pw_input').value = items.pw;
document.getElementById('prot_input').value = items.prot;
document.getElementById('scrape').value = items.scrape;
document.getElementById('prefix').value = items.prefix;
});
}

function validateIP(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
function validateIP(ipaddress,prefix) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress) && /^[0-9]*$/g.test(prefix)) {
return (true)
}
alert("You have entered an invalid IP address!")
alert("Invalid entry, please correct.")
return (false)
}

Expand All @@ -40,4 +56,4 @@ document.addEventListener("DOMContentLoaded", function () {
document.getElementById("ip_submit").addEventListener("click", save_options, false);

restore_options();
});
});
Loading