views:

64

answers:

2

I want to add h323:number style links to HighRise contact numbers, so that users can click a link to dial the IP phone...

The html I'm looking at is:

<table>
  <tbody>
    <tr>
      <th>Phone</th>
      <td>+44 (0)1123 1231312<span>Work</span></td>
    </tr>
    <tr>
      <th></th>
    <td>+44 (0)777 2342342<span>Other</span></td>
    </tr>
  </tbody>
</table>

and basically I want to pull out the number which is in a td and which starts with +44, strip out the spaces and stick in a link inside the span that has an href like

h323:4411231231312

i.e. is stripping out the 0 in brackets.

Any help would be greatfully received to any of the following.

(1) How do I match the td containing +\d\d numbers? (2) How do I use selectors to exclude the span when I get the number from the td (3) What regex should I use to cleanup the number for the href?

+2  A: 

This should be close to what you need.

$('tbody td').each(function() {
    // match a sequence of digits, parentheses and spaces
    var matches = $(this).text().match(/[ \d()]+/);

    if (matches) {
        // remove the spaces and stuff between parentheses
        var href = 'h323:' + matches[0].replace(/\s|\(.*?\)/g, '');
        var link = $('<a/>').attr('href', href);

        $('span', this).append(link);
    }
});

One word of caution though, if a span's content starts with a digit it will be included in the match; is this a possibility that needs to be accounted for?

Alex Barrett
Thanks for this. I've added an answer containing the final GreaseMonkey script...
Dycey
A: 

Here's the final GreaseMonkey script - might be useful for someone...

// ==UserScript==
// @name          HighRise Dialler
// @namespace     
// @description   Adds a CALL link to HighRise Contacts.
// @include       https://*.highrisehq.com/*
// @require       http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

(function(){

GM_xmlhttpRequest({
   method: "GET",
   url: "http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js",
   onload: run
});

function run(details) {

   if (details.status != 200) {
       GM_log("no jQuery found!");
       return;
   }

   eval(details.responseText);
   var $ = jQuery;

   //do something useful here....

   $('table td').each(function() {
       var matches = $(this).text().match(/^\+*?[\d\(\) ]+/);

       if (matches) {
         var href = 'h323:' + matches[0].replace(/\+44|\+|\s|\(|\)/g, '');
         var link = $(' <a>CALL<a/>').attr('href', href);
         $(this).find('span').append(link);
       }
   });

}

})();
Dycey