简体   繁体   English

如何使用Greasemonkey启用禁用按钮?

[英]How can I enable a disabled button with Greasemonkey?

I am targeting this website: https://ib.ing.cz/transactional-cz/#login/credentials 我的目标是这个网站: https//ib.ing.cz/transactional-cz/#login/credentials

It has an input form that I can fill in using 它有一个输入表单,我可以填写使用

document.querySelector('input[name=customer-id]').value = "1234567890";

However, the button that would let me go further is disabled. 但是,禁止让我走得更远的按钮。 No matter what I try, it stays so until I start to fill in the form manually. 无论我尝试什么,它都会保持这种状态,直到我开始手动填写表格。 How can I enable the button (and ideally click it programatically too)? 如何启用按钮(最好以编程方式单击它)? I tried simulating keypresses or manipulating the "disabled" attribute of the button but I did not suceed. 我尝试模拟按键或操纵按钮的“禁用”属性,但我没有成功。

UPDATE: I finally got it working, the code I am using is here: https://greasyfork.org/en/scripts/375326-p%C5%99ihla%C5%A1-se-na-ing (for some reason, I had to remove the if/else statement fro the answer, the condition was not true in the context of the script, even though it was in Firefox console) 更新:我终于搞定了,我使用的代码在这里: https//greasyfork.org/en/scripts/375326-p%C5%99ihla%C5%A1-se-na-ing (出于某种原因,我不得不删除答案的if / else语句,在脚本的上下文中条件不正确,即使它是在Firefox控制台中)

However, there was some more magic that prevented this from working when I click the button (manually or via the script), getting an error message saying "the website is out of order". 然而,当我点击按钮(手动或通过脚本)时,有一些更多的魔法阻止它工作,收到一条错误消息“网站乱序”。

It turned out one also needed to add "change" event. 事实证明,还需要添加“更改”事件。 The original link in the answer leads to a oldish question referencing Firebug, which has since been incorporated into Firefox. 回答中的原始链接引出了一个引用Firebug的旧问题,后者已被合并到Firefox中。 The key for me was using the debugger. 对我来说关键是使用调试器。 One just clicks right on the website and selects "inspect this element". 只需在网站上点击右键并选择“检查此元素”即可。 Events associated with those elements then can be found after clicking on these little triangles in the inspector: 然后,在检查器中单击这些小三角形后,可以找到与这些元素关联的事件: 在此输入图像描述

And for reading the websites code, it is best to put the scrabmle they send into a beautifier like this: https://beautifier.io/ 并且为了阅读网站代码,最好将他们发送的scrabmle放入这样的美化器: https ://beautifier.io/

Reference Choosing and activating the right controls on an AJAX-driven site . 参考在AJAX驱动的站点上选择并激活正确的控件

Your target site runs its "Client number" validation (to enable that button, etc.) on a keyup event placed on an ancestor div of the input. 您的目标站点在放置在输入的祖先 div上的keyup事件上运行其“客户端编号”验证(以启用该按钮等)。

This means that you must send a keyup event after setting the value, and make sure that the event bubbles up from the input. 这意味着您必须在设置值后发送keyup事件,并确保事件从输入中冒泡。

Like so: 像这样:

var cIdInpt = document.querySelector ('input[name=customer-id]');
if (cIdInpt) {
    var keyupEvnt   = new Event ('keyup', {bubbles: true} );  //  no key code needed in this case.
    cIdInpt.value   = "1234567890";

    cIdInpt.dispatchEvent (keyupEvnt);
}
else {
    console.error ("cIdInpt not found.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM