Greasemonkey:设置表单值在 Firefox 中不起作用

发布于 2024-09-15 03:48:03 字数 582 浏览 7 评论 0原文

这在 Chrome 下完美运行,但在 Firefox 中,直到我第二次在页面上点击“提交”后,字段才会填充(在第一次告诉我无效的用户/密码后返回)。我缺少什么?

这是我的 Greasemonkey 脚本代码:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";
},true);

This works flawlessly under Chrome, but in Firefox the fields don't populate until the SECOND time I hit submit on the page (after it comes back from the first time telling me invalid user/pass). What am I missing?

This is my code for the Greasemonkey script:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";
},true);

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

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

发布评论

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

评论(2

玩世 2024-09-22 03:48:03

您应该发布表单代码,但问题很可能是事件传播。

将函数更改为:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";

    event.preventDefault();
    event.stopPropagation();
    return false;
},true);

You should post your form code, but the problem is most likely, event propagation.

Change the function to:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";

    event.preventDefault();
    event.stopPropagation();
    return false;
},true);
迷途知返 2024-09-22 03:48:03

或者也许...

document.getElementById('loginForm').addEventListener("submit", function(e)
{
    var fields = e.target.elements;
    fields.namedItem("serverLogin").selectedIndex = 2;
    fields.namedItem("usernameLogin").value = "username";
    fields.namedItem("passwordLogin").value = "password";
    e.target.setAttribute("action", "urltosubmit");
}, false);

or maybe...

document.getElementById('loginForm').addEventListener("submit", function(e)
{
    var fields = e.target.elements;
    fields.namedItem("serverLogin").selectedIndex = 2;
    fields.namedItem("usernameLogin").value = "username";
    fields.namedItem("passwordLogin").value = "password";
    e.target.setAttribute("action", "urltosubmit");
}, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文