Javascript: GetElementsByContent

I had the inclination last week to knock up a quick Javascript function to effectively give you GetElementsByContent. So, you can call a function which returns an array of nodes which match a string (or RegExp) which you pass in, which can optionally be filtered by tag name.

Now, I make no claims that this is “a first”, as it probably isn’t, I wanted to write it, so I did.  If I’d have googled it, I’d have probably found something really quickly that did exactly what I wanted and would probably be far superior to what I have written.  I wanted to write it for fun really, but I think it’s actually quite useful, so here it is –

var gbc=function(contents,tag){
  var n=[];var s=tag||'*';
  var b=document.getElementsByTagName(s);
  var r=new RegExp(contents,'ig');
  for(var i=0;i<b.length;i++){
    for(var t=0;t<b[i].childNodes.length;t++){
      if(b[i].childNodes[t].nodeType==3&&r.test(b[i].childNodes[t].textContent)){
        n.push(b[i]);
        break;
      }
    }
  }
  return n;
}

Because I’m lazy I’ve called it ‘gbc’ (Get By Content), but obviously you can name it whatever you like.

You use it like –

var f = gbc('forums');

This would return all nodes in the current document which contain the text ‘forums’.

You could do –

var f = gbc('forums','a');

This would return all nodes matching the text ‘forums’ (within the text content, not attributes), but limit the search to just ‘a’ tags.

Because the first parameter (the text to search) is actually treated as a regex, you can actually pass it a string to treat as a regex. So –

var f = gbc('abc\\d+');

This would return all nodes which contain the text ‘abc’ followed by one or more numbers. Note the double slash, this is due to the way Javascript interprets the RegExp when using the ‘new’ style syntax, you need to escape any slashes.

That’s about it really, let me know if you use it or have any feedback.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Advertisements

Switched Theme

Unfortunately, I’ve had to move away from my old theme, which is quite sad as Dennis featured so prominently on the old one. I’ll probably have to go for the CSS upgrade and get him back in here that way.

I needed to switch in order to get away from fixed width, which was ruining any code I pasted. As I intend to start posting more code, the old them had to go.

Right, now I need to right some new posts..

Google sync on windows mobile 5 (wm5)

Google has (finally) launched a product (service) allowing you to sync your contacts and calendar from windows mobile device with gmail equivalents.

Google Sync for Windows Mobile

If however, like me, you’re running an old window mobile 5 device, you may find yourself stumped while following the instructions.  Specifically, it says –

8. Leave the Domain field empty.

Hmm, not so easy.. it’s a compulsary field!

The answer is to use m.google.com/sync for the domain 🙂

This google forum post pointed me to the answer.

Oh, one more thing, you may want to take a backup of your phone contacts/calendar first.  That’s easy grab PIM backup from DotFred, it works great.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Hosted wordpress social bookmarks bookmarklet

As I use Google Chrome as my default browser nowadays, I don’t currently have access to greasemonkey (userscripts are coming in 2.0 though :)) and therefore I can’t use my hosted wordpress social bookmarks greasemonkey script. For this reason (and for others who don’t use firefox*), I’ve knocked up a bookmarklet version of the script.

Given that it’s really diffcult to format code correctly in here, I’ll just point you to it as a file.  Just grab the file and and copy the contents as the location of a bookmark (preferably one on you bookmarks bar so you have easy access to it).  Then, whenever you’re on the add or edit post pages in your wordpress admin, you can click the link to add in the nifty “Social Bookmarks” section as if you were using the greasemonkey version.

Neat.

*Apart from those using IE as it seems to truncate all bookmarks longer than a predetermined max… what a PITA.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Social bookmarks greasemonkey script updated

I’ve just updated my hosted wordpress social bookmarks script again.

This update just makes it work with the wordpress admin update.  Sorry for the delay on updating this.

If you’re using this greasemonkey script, you may want to grab the update.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

BBCode for Google Picasaweb images

If, like me, you post links to your Picasaweb images on forums, you’ll probably have scratched your head in the past and wondered why Google doesn’t include a ‘BBCode’ option in it’s ‘Link to this photo’ section on the right hand side.

So, I thought I’d knock up a quick bookmarklet to achieve this, basically if you copy the code below as the location for a bookmark in your bookmark bar, you can click it to swap out the standard ‘Embed image’ code for BBCode for insertion to forums.

Here you go –

javascript:void(function(){
  var s=document.getElementById('lhid_snippet');
  if(s){
    s.value='[url='+document.location.href+'][img]'+/<img src="([^"]+)"\/>/i.exec(s.value)[1]+'[/img][/url]'};
}())

(you’ll need to get it back one line, it wont format correctly here as one line)
Enjoy 🙂

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

Social bookmarks greasemonkey script updated

Just a quick post to say I’ve updated the hosted wordpress social bookmarks script I wrote back along.  If you’re using this greasemonkey script, you may want to grab the update.

I’ve simply updated the links to all have rel=”nofollow” which they should be really (IMO).

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live