finds
's within a given context of a web page
(function(){
var context = arguments[0] || document.body
//, nodes = context.childNodes, nLen = nodes.length
, ctxParagraphs = [], ctxConditions = false
, ELM_NODE = 1, TXT_NODE = 3;//, HTML_COMMENT_NODE = 8
function hasClass(ctx, cls) {
return ctx.className
&& new RegExp(
'(^|\\s)' + cls + '(\\s|$)'
)
.test(ctx.className);
}
function seek(ctx) {
if (ctxConditions) return;
var tryClass = arguments[1] ? arguments[1] : false
, isElement = ctx.nodeType == ELM_NODE
, isClassed = hasClass(ctx, tryClass);
//if (tryClass) console.log('seeking .'+tryClass);
ctxConditions = isElement && tryClass && isClassed;
/*
// is element node AND not attempting to match class:
(isElement && !tryClass)
|| // OR
// is element node AND attempting class match AND found match:
(isElement && tryClass && isClassed)
;*/
if ( ctxConditions ) {
// found a useful class-scoped context, use it and get out!
ctxParagraphs = ctx.getElementsByTagName('p');//console.log('context conditions met!');
return;
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
switch (ctx.childNodes[i].nodeName.toLowerCase()) {
case 'div':
seek(ctx.childNodes[i], tryClass);
break;
}
}
}
}
// seek known content #id markers...
// @todo
// seek known content .class markers...
try {
seek(context, 'birdie'); // miss
seek(context, 'fuckwad'); // miss
seek(context, 'gimmeh'); // miss
seek(context, 'post'); // hit! (wordpress)
}
catch(e) {;}
// if no narrower context was discovered, operate on all <p>'s:
if (ctxParagraphs.length == 0) {
//seek(context);
ctxParagraphs = context.getElementsByTagName('p');
}
var pLen = ctxParagraphs.length; //console.log(pLen); console.log(postParagraphs);
while(pLen--) {
// do shit!
ctxParagraphs[pLen].style.textDecoration = 'line-through';
}
}())
(function(){
var context = arguments[0] || document.body
, pNodes = [], txtNodes = []
, ELM_NODE = 1, TXT_NODE = 3//, HTML_COMMENT_NODE = 8
, whitespace = /^\s*$/;
// more useful!
function seekParaNodes(ctx) {
if (ctx.nodeType == ELM_NODE && ctx.nodeName == 'P') {
pNodes.push(ctx); //console.log(ctx.textContent);
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
seekParaNodes(ctx.childNodes[i]);
}
}
}
// less useful
function seekTextNodes(ctx) {
if (ctx.nodeType == TXT_NODE) {
if (whitespace.test(ctx.nodeValue)) {
txtNodes.push(ctx);//console.log(ctx.textContent);
}
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
seekTextNodes(ctx.childNodes[i]);
}
}
}
seekParaNodes(context);
console.log(pNodes);
}(/*document.getElementById('post-155442')*/))