Device Device - 1 month ago 8
Javascript Question

Add XPath to chrome extension or into javascript

I'm making chrome extension and i need to have xpath in it, it dose not work even when i add jquery in to the manifest.

ERROR:

Uncaught ReferenceError: $x is not defined


MANIFEST:

"permissions": [
"activeTab",
"*://www.znamky.zsunesco.cz/*"
],
"content_scripts": [
{
"matches": ["http://znamky.zsunesco.cz/*"],
"js": ["jquery-1.11.3.min.js", "Script.js"]
}
]


SCRIPT:

$x("//div[@class='nazevprdiv' and ./a[text()='"+name+"']]/../../td/table/tbody/tr[@class='detznamka']/td");

Answer

Like adelphus said, it's only a variable usd by Firebug as an xpath shortcut. You can implement it with this code:

function $x(path){
 var xpath = document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
   var temp = [];
   for (var i = xpath.snapshotLength - 1; i >= 0; i--) {
     temp.push(xpath.snapshotItem(i));
   }
   return temp;
}