I'm working on a Greasemonkey script for weibo.com. I can't pick the elements using XPath on the XHTML page.
This code fails to grab the elements I want:
function resolver(prefix) {
return prefix === 'x' ? 'http://www.w3.org/1999/xhtml' : null;
}
var allLinks, thisLink;
allLinks = document.evaluate(
"//x:a[@href]",
document,
resolver,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);
Only the <a>
elements on the sidebar are picked and the rest are still there. Please refer to this, weibo.com, target page.
Is there anyway to pick all the elements with attribute action-type="login"
?
I used "//x:a[@action-type='login']"
, but It didn't work.
The problem is that the script is running before all of these nodes are added to the page. They are added by the page's AJAX later.
So, you could add a time-delay to your script. But:
If you just want to grab select elements, you almost never need to use XPath. Use
querySelectorAll()
or jQuery instead. Here's a basic example withquerySelectorAll
and no time delay:Here's a complete Greasemonkey script, that handles the delayed content problem using jQuery and the waitForKeyElements() utility: