Phone Number Identifier

By JoeSimmons Last update Sep 18, 2008 — Installed 230 times.

Request for additional number format and instead of bold make a link

in
Subscribe to Request for additional number format and instead of bold make a link 2 posts, 1 voice



Plasbot Scriptwright
FirefoxWindows

If you could get this to work I think it would be very useful. Sometimes I'll be looking over call logs from my home voip phone or AT&T, and I don't recognize a number so I'll google it or if it's an 800 number I check whocalled.us. If your script could just turn all recognized phone numbers on the page to a link querying either google or whocalled it would be SO helpful!
I tried modifying the script myself but I couldn't figure out the crazy regex and I don't know javascript to begin with :/
On the regex: my voip uses this format: 1222333444 . A 1 with the full number all bunched together. When I installed your script it does not seem to recognize this format.
Thanks!

 
Plasbot Scriptwright
FirefoxWindows

Woohoo! I did it! Can't believe it but this works on my VoicePulse call log:

// ==UserScript==
// @name           Phone Number Identifier
// @namespace      http://userscripts.org/users/23652
// @description    Makes phone numbers bold and italic
// @include        *
// ==/UserScript==

(function () {
 	const trackRegex =  /((\d{3})\s?\-\s?(\d{4}))|(\(\d{3}\)\s?\d{3}\s?\-\s?\d{4})|(\d{3}\s\d{4})|(\d{11})/g;

    // tags we will scan looking for un-hyperlinked urls
    var allowedParents = [
        "abbr", "acronym", "address", "applet", "b", "bdo", "big", "blockquote", "body", 
        "caption", "center", "cite", "code", "dd", "del", "div", "dfn", "dt", "em", 
        "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6", "i", "iframe",
        "ins", "kdb", "li", "nobr", "object", "pre", "p", "q", "samp", "small", "span", "strike", 
        "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"
        ];
    
    var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ")" + "]";

    var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

    for (var cand = null, i = 0; (cand = candidates.snapshotItem(i)); i++) {
        if (trackRegex.test(cand.nodeValue)) {
            var span = document.createElement("span");
            var source = cand.nodeValue;
            
            cand.parentNode.replaceChild(span, cand);

            trackRegex.lastIndex = 0;
            for (var match = null, lastLastIndex = 0; (match = trackRegex.exec(source)); ) {
                span.appendChild(document.createTextNode(source.substring(lastLastIndex, match.index)));
                
                var a = document.createElement('a');
				a.setAttribute('href', 'http://www.google.com/search?q=' + source.slice(1) );
                var b = document.createElement("b");
                b.appendChild(document.createTextNode(match[0]));
                a.appendChild(b);
                span.appendChild(a);

                lastLastIndex = trackRegex.lastIndex;
            }

            span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
            span.normalize();
        }
    }

})();

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel