$ X () function not defined inside Chrome extension, script contents

$x("//a[contains(@href,'.jpg')]"); 

works as expected from the command line developer tool. But, when in the content-script extension, I get ' $x is not defined '.

Why is this not available in the script content or is there a special way to access it inside the content-script / Chrome extension?

I am using Chrome 22 on Debian.

+7
javascript google-chrome xpath google-chrome-extension content-script
source share
1 answer

$x() not part of the webpage runtime or script content. This is a tool that is part of the command line API for Chrome DevTools .

To use XPath in the content of the script, you need to do it in the usual way, the convenient DevTools shortcut is not available.

Your code will look like this:

 var jpgLinks = document.evaluate ( "//a[contains(@href,'.jpg')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); var numLinks = jpgLinks.snapshotLength; for (var J = 0; J < numLinks; ++J) { var thisLink = jpgLinks.snapshotItem (J); console.log ("Link ", J, " = ", thisLink); } 

is what $x() did for you, backstage.


While you're on it, go to the CSS selector . Then the same functionality:

 var jpgLinks = document.querySelectorAll ("a[href$='.jpg']"); var numLinks = jpgLinks.length; for (var J = 0; J < numLinks; ++J) { var thisLink = jpgLinks[J]; console.log ("Link ", J, " = ", thisLink); } 

- which is much nicer in my book.

+7
source share

All Articles