wuser — 2012-01-04T03:52:17-05:00 — #1
I wonder how can I find a specific text in the source of a web page.
"color = document.body.style.background"
but I need to find text that is not part of the DOM Objects.
To be more specific, when I check news on a specific website like this: http://www.bbc.co.uk/news/technology-16382648
I need to make a script (a bookmarklet or a greasemonkey script) to extract the date, and therefore I have to search for the text "<span class="date">"
In other words: how can I make a script or a Firefox addon to "grep" for a certain text on a web site?
I am using Firefox browser, so for me it's enough a solution that only works in that browser.
wuser — 2012-02-09T17:12:32-05:00 — #2
I found the solution, in case anyone else needs something similar:
var x = document.body.innerHTML;
x = x.match(/class=.date.*/);
x = x.replace(/class=.date../, "");
x = x.replace(/..span/, "");
aussiejohn — 2012-02-09T17:40:34-05:00 — #3
So long as you're using a modern browser you could use document.getElementsByClassName(); to get the date element. If you would need to narrow it down to only a specific area (to save having to look through a whole document) you could use something like
var el = document.getElementById("main-content");
var articleDate = el.getElementsByClassName("date").innerHTML; //  is there to return the first element only.