Xpath, In A Greasemonkey Script, Is Not Selecting Right Nodes On An Xhtml Page
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)
Solution 1:
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:var allLinks = document.querySelectorAll ("a[action-type='login']"); if (allLinks.length) { // PROCESS NODES AS DESIRED, HERE. }
Here's a complete Greasemonkey script, that handles the delayed content problem using jQuery and the waitForKeyElements() utility:
// ==UserScript==// @name _Weibo, hilite login links// @include http://s.weibo.com/weibo/*// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js// @require https://gist.github.com/raw/2625891/waitForKeyElements.js// @grant GM_addStyle// ==/UserScript==/*- The @grant directive is needed to work around a design change introduced in GM 1.0. It restores the sandbox. */functionprocessLoginLinks (jNode) { //***** YOUR CODE HERE ***** jNode.css ("background", "lime"); } waitForKeyElements ("a[action-type='login']", processLoginLinks);
Post a Comment for "Xpath, In A Greasemonkey Script, Is Not Selecting Right Nodes On An Xhtml Page"