开发者

Greasemonkey+jQuery cannot submit form, or access DOM!

开发者 https://www.devze.com 2023-03-01 16:39 出处:网络
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.

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!


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消