Googleの検索順位を調べるXPath - Ci.nsIZIGOROu - Mozilla 拡張機能勉強会
var host = "pathtraq.com";
document.evaluate('count(//div[contains(concat(" ",@class," ")," g ") and not(@style)][h2/a[contains(@href,"' + host + '")]][1]/following-sibling::div[1]/preceding-sibling::div[not(@style)])', document, null, XPathResult.NUMBER_TYPE, null).numberValue;
/* ->
0: out of 100 or just 100
otherwise: rank
*/
見つからなかったときcount(NULL)+1だと1になってしまうので、1つ進めて前を数えるみたいなことをしてる。
そのせいで、100位ちょうどを発見できなくなってしまった。微妙。
count()にこだわらなければ、$xを使うこともできる。
var host = "pathtraq.com";
$x('//div[contains(concat(" ",@class," ")," g ") and not(@style)][h2/a[contains(@href,"' + host + '")]][1]/following-sibling::div[1]/preceding-sibling::div[not(@style)]').length;
/* ->
0: out of 100 or just 100
otherwise: rank
*/
やっぱり100位ちょうどは発見できない。微妙。