Bug 1143844 - Check document for readerable content to determine whether or not to show reader button. r=Gijs

--HG--
extra : rebase_source : 7a849952243aa5ff8d4711a1f0308747558faa2f
This commit is contained in:
Margaret Leibovic 2015-03-18 13:42:52 -07:00
Родитель 9d06161cb9
Коммит c0ede94dd6
1 изменённых файлов: 35 добавлений и 2 удалений

Просмотреть файл

@ -63,14 +63,47 @@ this.ReaderMode = {
/**
* Decides whether or not a document is reader-able without parsing the whole thing.
* XXX: In the future, this should be smarter (bug 1143844).
*
* @param doc A document to parse.
* @return boolean Whether or not we should show the reader mode button.
*/
isProbablyReaderable: function(doc) {
let uri = Services.io.newURI(doc.documentURI, null, null);
return this._shouldCheckUri(uri);
if (!this._shouldCheckUri(uri)) {
return false;
}
let REGEXPS = {
unlikelyCandidates: /combx|comment|community|disqus|extra|foot|header|menu|remark|rss|shoutbox|sidebar|sponsor|ad-break|agegate|pagination|pager|popup|tweet|twitter/i,
okMaybeItsACandidate: /and|article|body|column|main|shadow/i,
};
let nodes = doc.getElementsByTagName("p");
if (nodes.length < 5) {
return false;
}
let possibleParagraphs = 0;
for (let i = 0; i < nodes.length; i++) {
let node = nodes[i];
let matchString = node.className + " " + node.id;
if (REGEXPS.unlikelyCandidates.test(matchString) &&
!REGEXPS.okMaybeItsACandidate.test(matchString)) {
continue;
}
if (node.textContent.trim().length < 200) {
continue;
}
possibleParagraphs++;
if (possibleParagraphs >= 5) {
return true;
}
}
return false;
},
/**