拦截Chrome内容扩展程序中的表单提交 [英] Intercepting a form submission in a Chrome content extension
问题描述
我想拦截特定网站上的表单提交。表格没有ID,但它有一个名字。我不想使用jQuery,因为我的扩展非常轻,并且没有依赖关系。表单不是通过按钮提交的,而是通过submit()方法提交的。
我以为我可以只处理onSubmit事件,但它似乎没有得到
现在我很难过,有什么想法吗?
您可以使用
var myForm = document.getElementsByName('myForm');
返回一个 nodeList
(类似于 Array
)。然后你可以用两种方法重写 submit
事件:
myForm.onsubmit = function (evt){...};
或
myForm.addEventListener('submit',function(evt){...});
小心只使用小写活动名称的字母。在第二个示例中,您可以将多个侦听器绑定到事件,但旧版浏览器不支持(< IE 9)。
示例:
html:
< form name =myFormaction =#method = POST >
< input type =submitvalue =发送>
< / form>
js:
var myForm;
myForm = document.getElementsByName('myForm')[0];
myForm.onsubmit = function(evt){
//在传递或停止提交
之前执行此操作。
I'd like to intercept the submission of a form on a particular website. The form has no ID, but it does have a name. I'd prefer not to use jQuery, as my extension is very light weight and has no dependencies. The form is not submitted by a button, but by the submit() method.
I thought I could just handle the onSubmit event, but it doesn't seem to get raised.
Now I'm stumped, any ideas?
You can catch your form with
var myForm = document.getElementsByName('myForm');
Which returns a nodeList
(similar to Array
). Then you can override the submit
event in two ways:
myForm.onsubmit = function (evt) {...};
or
myForm.addEventListener('submit', function (evt) {...});
Be careful to only use lowercase letters for the event name. With the second example you can bind multiple listeners to the event but it isn't supported by older browsers (< IE 9).
Example:
html:
<form name="myForm" action="#" method="post">
<input type="submit" value="Send">
</form>
js:
var myForm;
myForm = document.getElementsByName('myForm')[0];
myForm.onsubmit = function (evt) {
// Do things here before passing on or stop the submit
};
这篇关于拦截Chrome内容扩展程序中的表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!