Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29741
| From | Martin Honnen <mahotrash@yahoo.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Searching the visual appearance of a Web page? |
| Date | 2016-03-01 11:39 +0100 |
| Organization | Liberty Development |
| Message-ID | <nb3rgk$ial$1@news.albasani.net> (permalink) |
| References | <wW01wWroDN1WFw8z@invalid.uk.co.demon.merlyn.invalid> |
Dr J R Stockton wrote:
> I have a reference to the body element of a local Web page, and can
> assume that body.onload() has finished. I also have a RegExp, which has
> been defined from the value of an input type=text element.
>
> I want to apply that RegExp to the whole displayed text, all at once or
> piecemeal, and get all of the matches. I have been using the match
> method on body.innerText, body.innerHTML, or body.textContent, which was
> good enough to do what I wanted, but not ideal.
>
> For example, the text up<br>on in the HTML source must be treated as the
> two words "up on" and not the one word "upon". And, if practical,
> "câm" should be treated as a three-letter word.
innerText should give you a plain string in which e.g. <br> has been
converted to a new line character and a character reference to its
character. The only drawback is that Firefox in its current version 44
does not support it, but according to
http://perfectionkills.com/the-poor-misunderstood-innerText/ in Firefox
45 we will see support. So doing the regular expression search on
body.innerText seems like the most promising approach. Or why did
"innerText" not give you the ideal result, unless you needed it with
Mozilla browsers?
> The immediate aim is to use something like /\b[A-Z]{4,}\b/gi to find all
> upper-case "word"s of four or more letters, in order to discover most of
> the acronyms without too many false positives or negatives, so that a
> list of them can be converted into, or used to check, a Glossary.
> How should it best be done, in outline?
As the article above suggests, an alternative would be to get the text
of a selection, the article suggests
function getSelectionString(el, win) {
win = win || window;
var doc = win.document, sel, range, prevRange, selString;
if (win.getSelection && doc.createRange) {
sel = win.getSelection();
if (sel.rangeCount) {
prevRange = sel.getRangeAt(0);
}
range = doc.createRange();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
selString = sel.toString();
sel.removeAllRanges();
prevRange && sel.addRange(prevRange);
}
else if (doc.body.createTextRange) {
range = doc.body.createTextRange();
range.moveToElementText(el);
range.select();
}
return selString;
}
as an implementation, I have tried that in
https://jsfiddle.net/vb87g1ye/1/, seems to do the "<br>" to new line
conversion in Mozilla, so perhaps you could check
if (typeof document.body.innerText != 'undefined')
to work with innerText where supported and use that selection approach
in Mozilla browsers to get the text.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Searching the visual appearance of a Web page? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-02-29 23:14 +0000
Re: Searching the visual appearance of a Web page? Martin Honnen <mahotrash@yahoo.de> - 2016-03-01 11:39 +0100
Re: Searching the visual appearance of a Web page? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-03-03 23:25 +0000
Re: Searching the visual appearance of a Web page? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-03-03 16:27 -0800
Re: Searching the visual appearance of a Web page? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-03-05 23:37 +0000
Re: Searching the visual appearance of a Web page? Bart Van der Donck <bart@nijlen.com> - 2016-03-02 00:49 -0800
csiph-web