[英]Intercept submit event
I want to build a simple Google extensions that would stop users to submit a tweet if it contains a blacklisted word.我想构建一个简单的 Google 扩展程序,如果它包含列入黑名单的单词,它将阻止用户提交推文。 This requires that I can intercept the click on "Tweet" that would submit it.
这要求我可以拦截将提交它的“Tweet”点击。
After inspecting the Twitter page, I currently use the following code in the content script:检查 Twitter 页面后,我目前在内容脚本中使用以下代码:
$('button.tweet-action').click(function(e) {
console.log(e);
e.preventDefault();
e.stopPropagation();
return false;
});
When I click "Tweet" then I get the console output but the tweet is still submitted.当我点击“Tweet”时,我得到了控制台输出,但仍然提交了推文。 Interestingly, when I use only
$('button')
then it works with many buttons.有趣的是,当我只使用
$('button')
它可以与许多按钮一起使用。 For example, I cannot delete a tweet anymore.例如,我不能再删除推文了。 So it kind of works just not for submitting new tweets, and I cannot tell why that's the case.
所以它只是不适用于提交新推文,我不知道为什么会这样。 Here's the snippet from Twitter HTML page:
这是 Twitter HTML 页面的片段:
How can I intercept and stop the submission of tweets.如何拦截和停止提交推文。
Update: I've tried intercepting the form submit as suggest below.更新:我试过按照下面的建议拦截表单提交。 Since the form doesn't have an id, I used a class selector:
由于表单没有 id,我使用了一个类选择器:
$('.tweet-form').submit(function(e) {
console.log(e);
e.preventDefault();
e.stopPropagation();
return false;
});
But it's not working.但它不起作用。 For a trivial test I tried
$('.tweet-form').css(...)
which successfully changed some style changes.对于一个简单的测试,我尝试了
$('.tweet-form').css(...)
它成功地改变了一些样式更改。 However intercepting the submit doesn't work;但是拦截提交不起作用; I don't even see the console output.
我什至没有看到控制台输出。 Maybe that's because there's no
type="submit"
anywhere between the form tags.也许那是因为表单标签之间没有
type="submit"
。
You have to intercept the <Form />
submit event to prevent the post of a new element.您必须拦截
<Form />
submit 事件以防止发布新元素。
You can add it directly in your <Form />
element:您可以直接在
<Form />
元素中添加它:
<form onsubmit="myFunction()">
Enter name: <input type="text">
<input type="submit">
</form>
Or you can use a selector:或者您可以使用选择器:
document.querySelector("#myForm").addEventListener("submit", function(e){
if(!isValid){
e.preventDefault(); //stop form from submitting
}
});
Intercepting the button click event works with stopImmediatePropagation()
instead stopPropagation()
.拦截按钮单击事件适用于
stopImmediatePropagation()
而不是stopPropagation()
。 The problem is that this button already has an click handler attached to it, and stopPropagation()
only affects parent even handlers.问题是这个按钮已经附加了一个点击处理程序,而
stopPropagation()
只影响父偶数处理程序。 And adding a new click event handlers doesn't overwrite existing ones.添加新的点击事件处理程序不会覆盖现有的。
stopImmediatePropagation()
prevents other listeners of the same event from being called. stopImmediatePropagation()
防止调用同一事件的其他侦听器。
Thus, the working solutions looks like:因此,工作解决方案如下所示:
$('button.tweet-action').click(function(e) {
console.log(e);
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.