Greasemonkey+jQuery 无法提交表单,或访问 DOM!

发布于 2024-11-04 17:43:23 字数 1549 浏览 3 评论 0原文

页面上有一个链接,该链接调用带有两个参数的名为 submitForm 的方法;该方法使用参数设置表单值,然后提交表单。

我使用 GreaseMonkey (GM) 脚本(如下)和 jQuery 来解析和访问链接的 href 属性中的这些参数,然后我想自动提交表单。我尝试了三种方法(已注释掉),但似乎都不起作用,所以我认为我错过了有关 GM 工作原理的一些信息。

$(document).ready(function() {
    var regex = /\'([0-9]+)\',\'([0-9]+\'/g;
    var link = $('td.dataContend:first a');
    var match = regex.exec($(link).attr('href'));
    if (match != null) {
        $('input[name="field1"]').val(match[1]);
        $('input[name="field2"]').val(match[2]);

        try {
            // 1. The next line says "document.submitForm is not a function"
            //document.submitForm(match[1], match[2]);
            // 2. The next line says "document.billViewForm is undefined"
            //document.billViewForm.submit();
            // 3. The next line throws no error but the page does not change
            //$('form[name="billViewForm"]').trigger('submit');
        }
        catch (err) {
            alert(err);
        }
    } else {
        alert('no match');
    }
});

我已经确认 try/catch 块之前的所有内容都可以正常工作;正则表达式正确解析值,并且 jQuery 更改表单字段值。

方法 1. 和 2. 失败并出现错误,看起来我由于某种原因无法从 GM 访问文档。如果我使用 FireBug 控制台并输入 1. 或 2. 中的任何一个,页面就会完美提交。这真的让我很害怕,因为我之前能够毫无问题地访问该文档。

方法 3. 不会引发错误,但页面不会刷新表单提交的结果。我也尝试过使用 .submit() 方法,但没有成功。如果我在 FireBug 控制台中输入 3.,我会收到有关 $('form... 未定义的错误。

似乎通过使用 jQuery 我无法访问正常的 javascript document code> 属性,并且表单的 .submit() 方法不起作用,

感谢您的敏锐洞察力或富有洞察力的建议!

On a page there is a link that calls a method called submitForm with two parameters; the method uses the parameters to set form values, and then submits the form.

I use a GreaseMonkey (GM) script (below) and jQuery to parse and access those parameters from the link's href attribute, and then I want to automatically submit the form. I have tried three approaches (commented out), none of which seem to work, so I think I am missing something about how GM works.

$(document).ready(function() {
    var regex = /\'([0-9]+)\',\'([0-9]+\'/g;
    var link = $('td.dataContend:first a');
    var match = regex.exec($(link).attr('href'));
    if (match != null) {
        $('input[name="field1"]').val(match[1]);
        $('input[name="field2"]').val(match[2]);

        try {
            // 1. The next line says "document.submitForm is not a function"
            //document.submitForm(match[1], match[2]);
            // 2. The next line says "document.billViewForm is undefined"
            //document.billViewForm.submit();
            // 3. The next line throws no error but the page does not change
            //$('form[name="billViewForm"]').trigger('submit');
        }
        catch (err) {
            alert(err);
        }
    } else {
        alert('no match');
    }
});

I have confirmed that everything up until the try/catch blocks works correctly; the regex is parsing the values correctly and jQuery changes the form field values.

Approach 1. and 2. fail with errors that look like I cannot access the document from GM for some reason. If I use the FireBug console and type in either of 1. or 2. the page submits perfectly. This is really freaking me out as I have been able to access the document before without any problems.

Approach 3. doesn't throw an error, but the page does not refresh with the result of the form submission. I have tried using the .submit() method as well, to no avail. If I enter 3. in the FireBug console, I get an error about $('form... being undefined.

It seems like by using jQuery I cannot access the normal javascript document properties, and the .submit() method of the form doesn't work.

Thanks for any sharp eyes or insightful suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-11-11 17:43:26

您无法以通常的方式访问 GM 中窗口的用户定义属性。
如果您想访问页面内的节点,请始终使用 DOM 方法。

如果您需要访问 GM 中窗口的用户定义属性,则需要使用 unsafeWindow -object

这意味着:

//1->submitForm is a user-defined function
unsafeWindow.document.submitForm(match[1], match[2]);

//2->use DOMDocument::getElementsByName() to access the form
document.getElementsByName('billViewForm')[0].submit();

//3-> jQuery is a user-defined object
unsafeWindow.$('form[name="billViewForm"]').trigger('submit');

You cannot access user-defined properties of a window in GM the usual way.
And if you like to access nodes inside a page always use DOM-methods.

If you need to access user-defined properties of a window in GM, you'll need to use the unsafeWindow-object

This means:

//1->submitForm is a user-defined function
unsafeWindow.document.submitForm(match[1], match[2]);

//2->use DOMDocument::getElementsByName() to access the form
document.getElementsByName('billViewForm')[0].submit();

//3-> jQuery is a user-defined object
unsafeWindow.$('form[name="billViewForm"]').trigger('submit');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文