JavaScript generate an result array from XPATH

This is a function in JavaScript that use an XPATH string to generate an Array from the results.This is very useful if you have to manipulate or use some items on a page and don’t have the id(like bots).

That is the function:

You could use it in the Terminal:

And we get all hrefs on this page in the Terminal:

Use it to store it in an Array and then write it on the page:

and we got this result:

XPATH

The XPATH(XML Path Language) is a query language from the W3C, it is used to select nodes in an XML file.

Full Path

In this example I start with “/” that say we start on root and then i navigate from element to element.

 

Anywhere

In this example I use the “//” to search in the full document to find all a nodes.(you could use the “//” behind a “/” like “/html/body/div//a” to find all elements in html body div.)

 

Attributes

you could use the “@” to find all elements wit the attribute class that is the string “secondarary”.

 

As example you could select all hrefs where the Attributes contains a string:

Or select all hrefs where the text from the URL contains a string:

 

Array

If you select all Elements you get an Array, in the example above I select the firs element from my result Array.You should avoid this, better is to navigate over the class or ID because if the Page changes a bit the XPATH don’t work any more.

 

That are some basics, if you need something special take a look into the Network or on my page.As example Web crawling in PHP or Perl.

Submit iframe from main window

I had the problem to submit my form in a iframe with a button on the main page, I used XPATH to get my iframe and then select it and submit the form like this:

Grease monkey open url with JavaScript

I use this JavaScript and XPATH with Greasemonkey to open the urls on Piratebay or other pages, if you open a torrent in a new tab the script search for urls containign “imagecurl.org”, “imgcrl.org” or “pixoverflow” then wait 10 seconds and open the url.This saves time, just open all what’s interesting in a new tab then just look at the images :

Perl crawl example

This is a little example how to crawl a web page with Perl, in this example i crawl my own page with XPATH to get all titles.

This is our result, this are the matches from the XPATH:

To see this in PHP click: PHP crawl example.

PHP crawl example

This is a little example how to crawl a web page with PHP, in this example I crawl my own page with XPATH to get all titles.

This is our result, this are the matches from the XPATH:

To see this in Perl click: Perl crawl example.

Javascript XPATH and Sleep

If you need to select an element via JavaScript and XPATH you could use this code.In line 4 I make a sleep about 10 Seconds and then open the link in the same window.