2

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.

2 Answers 2

9

You can access it through:

document.body.innerHTML 
Sign up to request clarification or add additional context in comments.

1 Comment

Damn innerHTML, how could I have forgotten this? Thx.
1

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; } 

2 Comments

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 You can always create a script tag that loads an external javascript file of arbitrary length.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.