小脚本上的JavaScript错误
我正在尝试编写一个小的Greasemonkey脚本,以更改页面上出现的一些元素的值(通过DOM修改) 我只是一个油嘴滑舌的用户,我没有JavaScript方面的经验。我收到以下错误:小脚本上的JavaScript错误,javascript,greasemonkey,Javascript,Greasemonkey,我正在尝试编写一个小的Greasemonkey脚本,以更改页面上出现的一些元素的值(通过DOM修改) 我只是一个油嘴滑舌的用户,我没有JavaScript方面的经验。我收到以下错误:表达式不是合法表达式。(行:result=…) 我还想知道是否还有其他错误需要纠正 这是我的剧本: // ==UserScript== // @name myscript // @namespace http://www.google.com // @include http://mysit
表达式不是合法表达式。
(行:result=…)
我还想知道是否还有其他错误需要纠正
这是我的剧本:
// ==UserScript==
// @name myscript
// @namespace http://www.google.com
// @include http://mysite/
// @version 1
// @grant GM_addStyle
// ==/UserScript==
function waitForKeyElements (selectorTxt, actionFunction) {
if (getElementByXPath(selectorTxt) == null) {
var timeControl = setInterval (function () {
waitForKeyElements (selectorTxt, actionFunction);
},
300
);
} else {
clearInterval (timeControl);
actionFunction();
}
}
var getElementByXPath = function (path) {
result = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue;
};
function myFunc (jNode) {
getElementById("foo1").setValue("foo2");
}
waitForKeyElements ("foo3", myFunc);
它抱怨
path
的值不是有效的XPath选择器。据我所见,您正在传递值foo3
,这意味着标记
——可能不是您想要的。请尝试/*[@id='foo3']
,查看更多xpath示例。哪一行引发此错误?抱歉,我忘了提及。让我来编辑。这是“result=…”一行,你在用哪个浏览器?我怀疑document.evaluate是否适用于IE和更老的浏览器。你是对的。我的实际xpath的形式是//h2[contains(,'myText')],但显然“contains”(这是有效的xpath)不起作用。。。我怎么能模仿这个呢?