Difference between revisions of "Click2dial"

From FreeSWITCH Wiki
Jump to: navigation, search
(Created page with 'I think I'm 85% complete with a server side click2dial solution for Firefox, on multiple client platforms. https://developer.mozilla.org/en/Web-based_protocol_handlers register...')
 
Line 1: Line 1:
I think I'm 85% complete with a server side click2dial solution for Firefox, on multiple client platforms.
+
save this off as click2dial.user.js:
  
https://developer.mozilla.org/en/Web-based_protocol_handlers
+
<pre>
  
register sip:// as a protocol handler, using the URL: htpp://yourFreeSWITCHserver.com/dial?data=fromextension@authkey@toextension
+
// ==UserScript==
 +
// @name          FS Click2Dial
 +
// @author        Nik Martin
 +
// @namespace    http://www.servercorps.com/gmscripts
 +
// @description  A FreeSWITCH Click 2 Dial script using the xml_rpc api
 +
// @include      http://*
 +
// ==/UserScript==
  
then, on the server, add a handler for the dial app.
+
 
 +
 
 +
(function () {
 +
var t = new Date().getTime();
 +
 
 +
 
 +
var my_user = "1000@my.sip.domain"
 +
var my_context = "my.dpcontext"
 +
var http_auth = "freeswitch:works"
 +
var my_fs_address = "my.fs.address"
 +
 
 +
//this is the test to see if the number looks like a phone number or not
 +
const trackRegex = /(\+\d\d?)?[\-\s\/\.]?[\(]?(\d){2,4}[\)]?[\-\s\/\.]?\d\d\d[\-\s\/\.]?(\d){3,5}\b/ig;
 +
function trackUrl(t) {
 +
return "http://" + http_auth + "@" + my_fs_address + ":8080/webapi/originate?user/" + my_user + " &transfer(" + String(t).replace(/[\(\)\- \.]/g, "") + " XML " + my_context + ")";
 +
}
 +
 
 +
    // 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", "object", "nobr", "pre", "p", "q", "samp", "small", "span", "strike",
 +
        "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"
 +
        ];
 +
   
 +
    var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ")" +
 +
//" and contains(translate(., 'HTTP', 'http'), 'http') + " +
 +
"]";
 +
 
 +
    var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
 +
 
 +
    //var t0 = new Date().getTime();
 +
    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", trackUrl(match[0]));
 +
                a.setAttribute("target", "_NEW");
 +
                a.appendChild(document.createTextNode(match[0]));
 +
                span.appendChild(a);
 +
 
 +
                lastLastIndex = trackRegex.lastIndex;
 +
            }
 +
 
 +
            span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
 +
            span.normalize();
 +
        }
 +
    }
 +
  var t1 = new Date().getTime();
 +
  alert("Click2Dial linkify took " + ((t1 - t0) / 1000) + " seconds");
 +
 
 +
})();
 +
 
 +
</pre>

Revision as of 07:07, 2 February 2011

save this off as click2dial.user.js:


// ==UserScript==
// @name          FS Click2Dial
// @author        Nik Martin
// @namespace     http://www.servercorps.com/gmscripts
// @description   A FreeSWITCH Click 2 Dial script using the xml_rpc api
// @include       http://*
// ==/UserScript==



(function () {
	 var t = new Date().getTime();


var my_user = "1000@my.sip.domain"
var my_context = "my.dpcontext"
var http_auth = "freeswitch:works"
var my_fs_address = "my.fs.address" 

//this is the test to see if the number looks like a phone number or not
	const trackRegex = /(\+\d\d?)?[\-\s\/\.]?[\(]?(\d){2,4}[\)]?[\-\s\/\.]?\d\d\d[\-\s\/\.]?(\d){3,5}\b/ig;
function trackUrl(t) {
		return "http://" + http_auth + "@" + my_fs_address + ":8080/webapi/originate?user/" + my_user + " &transfer(" + String(t).replace(/[\(\)\- \.]/g, "") + " XML " + my_context + ")";
	}

    // 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", "object", "nobr", "pre", "p", "q", "samp", "small", "span", "strike", 
        "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"
        ];
    
    var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ")" +
				//" and contains(translate(., 'HTTP', 'http'), 'http') + " + 
				"]";

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

    //var t0 = new Date().getTime();
    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", trackUrl(match[0]));
                a.setAttribute("target", "_NEW");
                a.appendChild(document.createTextNode(match[0]));
                span.appendChild(a);

                lastLastIndex = trackRegex.lastIndex;
            }

            span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
            span.normalize();
        }
    }
   var t1 = new Date().getTime();
   alert("Click2Dial linkify took " + ((t1 - t0) / 1000) + " seconds");

})();