0

我正在尝试编写一个小的 Greasemonkey 脚本来更改页面上出现的一些元素的值(通过 DOM 修改)。

我只是一个 Greasemonkey 用户,我没有 JavaScript 经验。我收到此错误:(the expression is not a legal expression.行:结果 =...)

我也想知道是否还有需要更正的错误。

这是我的脚本:

// ==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);
4

1 回答 1

1

它抱怨 的值path不是有效的 XPath 选择器。从我所见,您传递的值foo3意味着一个标签<foo3>- 可能不是您想要的。//*[@id='foo3']改为尝试,请参阅http://ejohn.org/blog/xpath-css-selectors/以获取更多 xpath 示例。

于 2013-04-19T10:02:33.740 回答