XPath脂猴 [英] xpath greasemonkey

查看:110
本文介绍了XPath脂猴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在油脂猴子脚本中修改gmail.使用xpather,我可以获取我要隐藏的部分的xpath表达式.但是使用下面的代码片段我找不到匹配项.永远不会调用警报功能.谁能指出我做错了什么?

I am trying to modify gmail in a greasemonkey script. Using xpather i can get the xpath expression for the part i am trying to hide. But using following snippet i can not get a match. alert function is never called. Can anyone point me to what am i doing wrong?

var allLinks, thisLink;
allLinks = document.evaluate(
     "//html/body/div[1]/div/div/div[1]/div/div[3]/div[1]/div[2]/div[2]/div[1]/div[1]",
     document,
     null,
     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
     null);
for (var i = 0; i < allLinks.snapshotLength; i++) {
  thisLink = allLinks.snapshotItem(i);
  alert("found");
}

@Alan Storm

@Alan Storm

我安装了firebug,并使用控制台对脚本进行了测试,但该脚本仍然有效,但是我仍然无法在油脂猴子下正常工作. Greasemonkey仍然没有给我警报框.我还尝试添加一个加载事件监听器,该监听器也没有帮助.

I installed firebug tested the script using console it works but still i can not get it to work under greasemonkey. Greasemonkey still not giving me an alert box. I also tried adding a on load event listener that did not helped either.

推荐答案

首先是一般的调试技巧,然后是您遇到的问题的解决之道.

First, a general debugging tip, then a stab at your problem.

请记住,Greasemonkey脚本只是JavaScript,并且您仍然可以使用所有可用的Javascript工具来调试问题.打开gmail,启动 Firebug ,然后尝试直接在命令行上运行javascript代码(单击向上的圆圈控制台行右侧的箭头以显示较大的键入区域.

Remember that Greasemonkey scripts are just javascript, and you can still use all the Javascript tools avaiable to you to debug your problem. Open up gmail, fire up Firebug, and try running your javascript code directly on the command line (click the upward circle arrow to the right of the console line for a bigger typing area).

当我对您的JavaScript代码段进行上述操作时,allLinks.snapshotLength的计算结果为0,表明您的xpath表达式不匹配任何内容.这很奇怪,因为当我使用 XPath Checker 时,它与标识.

When I did the above with your javascript snippet, allLinks.snapshotLength was evaluating to 0, indicating that your xpath expression didn't match anything. This is odd, because when I used XPath Checker, it matched the logo.

深入研究,看起来gmail的主要文档是许多iframe,而iframe包含实际的应用程序元素.具体来说,有一个ID为"canvas_frame"的框架,其中包含应用程序接口的实际DOM.当我尝试

Digging in a bit deeper, it looks like gmail's main document is a number of iframes, and the iframes contain the actual application elements. Specifically, there's a frame with an ID of "canvas_frame" which contains the actual DOM for the application interface. When I try

canvas = window.frames[document.getElementById('canvas_frame').name].document;
allLinks = canvas.evaluate(
     "//html/body/div[1]/div/div/div[1]/div/div[3]/div[1]/div[2]/div[2]/div[1]/div[1]",
     canvas,
     null,
     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
     null);

我得到的响应长度为1,可能更适合您的需求.

I get a response with a length of 1, which may suit your needs better.

最后,这不是必需的,但是您的xPath表达式看起来有些脆弱.如果gmail甚至略微更改了文档结构(例如,使用wrapper div),您的程序就会中断.考虑这样的事情.

Finally, this isn't required, but your xPath expression looks a little fragile. If gmail changes the document structure even slightly (say, with a wrapper div), your program will break. Consider something like this instead.

<!-- 
all divs on the page that contains an h1 element 
that contains the text "Gmail Logo" 
-->
//div[contains(./h1,"Gmail Logo")]

这篇关于XPath脂猴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆