/ Published in: JavaScript
Expand |
Embed | Plain Text
function $$(xpath,root) { xpath = xpath .replace(/((^|\|)\s*)([^/|\s]+)/g,'$2.//$3') .replace(/\.([\w-]+)(?!([^\]]*]))/g, '[@class="$1" or @class$=" $1" or @class^="$1 " or @class~=" $1 "]') .replace(/#([\w-]+)/g, '[@id="$1"]') .replace(/\/\[/g,'/*['); str = '(@\\w+|"[^"]*"|\'[^\']*\')'; xpath = xpath .replace(new RegExp(str+'\\s*~=\\s*'+str,'g'), 'contains($1,$2)') .replace(new RegExp(str+'\\s*\\^=\\s*'+str,'g'), 'starts-with($1,$2)') .replace(new RegExp(str+'\\s*\\$=\\s*'+str,'g'), 'substring($1,string-length($1)-string-length($2)+1)=$2'); var got = document.evaluate(xpath, root||document, null, 5, null); var result=[]; while (next = got.iterateNext()) result.push(next); return result; } // Example usage: $$('#title')[0].innerHTML='Greased'; $$('a[@href $= "user.js"]').forEach(function (a) { a.innerHTML='check it out a script'; }); $$('a[@href ^= "http"]').forEach(function (a) { a.innerHTML += ' (external)'; });
You need to login to post a comment.
