From 49f861ab7125dba440370b795817148ef913f2b1 Mon Sep 17 00:00:00 2001 From: gsandruck <79586458+gsandruck@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:33:33 -0500 Subject: [PATCH 1/8] Update find numebrs Changed a lot on this. Added scrape feature to find numbers that are not links, added formatting of phone numbers to remove special characters and only send numbers. Added custom prefix function to allow setting prefix in options. Changed how phone numbers are stored. They are now assigned to the link created as it's class attribute so that there can be more than one on a page and it will not always send the last one stored in tel variable. --- find_numbers.js | 113 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/find_numbers.js b/find_numbers.js index 1654bc7..c0bd2bf 100644 --- a/find_numbers.js +++ b/find_numbers.js @@ -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); - } - } -} \ No newline at end of file +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}\) ?)(?!([^<]*>)|(((?!))|(\d{3}-))?\d{3}-\d{4}(?!([^<]*>)|(((?!))|(\d{3}\.)?\d{3}\.\d{4}(?!([^<]*>)|(((?!))/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 + tel = tel.replace(/[^0-9*]/g, ''); + //determine length of tel number string + 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. + 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 + anchors[i].setAttribute("class", tel); + } + } + } + + //When any link is clicked send phonenumber stored as class to background + $('a').click(function(){ + //console.log("Mousedown"); + //Get the class attribute of the link and store as toDial number. + 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. + if (toDial.match(/^\*\d+$/) || toDial.match(/^-?\d+$/)) { + //console.log("class is number, dialing "+toDial); + chrome.runtime.sendMessage(toDial); + } + }); + +}); From 93f97df1415ce0130dfd8007df83c6b446564c88 Mon Sep 17 00:00:00 2001 From: gsandruck <79586458+gsandruck@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:36:42 -0500 Subject: [PATCH 2/8] Add jquery for click In order to sense clicks, added jquery in content scripts --- manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manifest.json b/manifest.json index 0655fde..3b94f10 100644 --- a/manifest.json +++ b/manifest.json @@ -13,7 +13,7 @@ "content_scripts": [ { "matches": [""], - "js": ["find_numbers.js"] + "js": ["jquery-2.2.0.min.js","find_numbers.js"] } ], @@ -34,4 +34,4 @@ ], "options_page": "options.html" -} \ No newline at end of file +} From 8c8c7ff9f27d2f1ea906b749616ffe4e0dda287d Mon Sep 17 00:00:00 2001 From: gsandruck <79586458+gsandruck@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:42:09 -0500 Subject: [PATCH 3/8] Added options. In addition to IP address, can change password, protocol, whether to scrape for numbers, and prefix. May want to check formatting. --- options.html | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/options.html b/options.html index 3b28729..c760a30 100644 --- a/options.html +++ b/options.html @@ -16,10 +16,20 @@ Grandstream Click To Call Options - Enter an IP address and click the button to save it. + Enter an IP address and click the button to save it. - + IP Address: + + Protocol: + http:// + https:// + + Scrape Page: + No + Yes + + Prefix: Save @@ -28,4 +38,4 @@ Grandstream Click To Call Options - \ No newline at end of file +
Enter an IP address and click the button to save it.
IP Address:
Protocol: + http:// + https:// +
Scrape Page: + No + Yes +
Prefix: