tags:

views:

333

answers:

2

Hi,

I'm writing a bookmarklet, i.e. a bookmark that contains javascript instead of a URL, and I have some trouble. In fact, I cannot remember how I can get the content of the page as a string, so I can apply a regular expression to find what I want. Can you please help me on this?

Before anyone suggests it, I cannot use getElementBy(Id/Name/Tag), because the data I'm looking for is HTML-commented and inside markups, so I don't think that would work.

Thanks.

+3  A: 

You can access it through:

document.body.innerHTML
CMS
Damn innerHTML, how could I have forgotten this? Thx.
Antoine
+1  A: 

so I can apply a regular expression to find what I want

Do. Not. Use. Regex. To. Parse. HTML.

Especially when the browser has already parsed it for you! Come ON!

the data I'm looking for is HTML-commented

You can perfectly well grab comment content out of the DOM. eg.

<div id="mything"><!-- la la la I'm a big comment --></div>

alert(document.getElementById('mything').firstChild.data);

And if you need to search the DOM for comment elements:

// Get comment descendents
//
function dom_getComments(parent, recurse) {
    var results= [];
    for (var childi= 0; childi<parent.childNodes.length; childi++) {
        var child= parent.childNodes[childi];
        if (child.nodeType==8) // Node.COMMENT_NODE
            results.push(child);
        else if (recurse && child.nodeType==1) // Node.ELEMENT_NODE
            results= results.concat(dom_getComments(child));
    }
    return results;
}
bobince
Thanks for the info, but the code I'm writing is for a bookmarklet, a single line of code in a bookmark. And bookmarklets in IE are limited to 470 chars, so I aim to maximum simplicity and shortness.
Antoine