简体   繁体   English

为什么这个JQuery事件不会在gmail中触发?

[英]Why doesn't this JQuery event to fire in gmail?

I am having problem with binding a Jquery event to gmail 'body' 我在将Jquery事件绑定到gmail'body'时遇到问题

$('body').on('click', function(event) {
        console.log("Entered function");
});

Visit IMDB.com (for eg) and in google chrome developer console paste in the above code. 访问IMDB.com(例如)和谷歌Chrome开发者控制台粘贴上面的代码。 When you click on the page, you can see "Entered function" printed on the console. 单击该页面时,您可以在控制台上看到“已输入的功能”。

But when I try the same with gmail, I get the following message: 但是,当我尝试使用gmail进行相同操作时,收到以下消息:

TypeError: Object #<HTMLBodyElement> has no method 'on'

What is going on with gmail, and how do I work around this? gmail发生了什么,我该如何解决这个问题?

EDIT :- 编辑: -

Non JQuery Version: 非JQuery版本:

document.addEventListener("click", function() {
   console.log("Entered function");
}, false);

Like the commenters have pointed out, JQuery is not loaded with Gmail, here is a non JQuery version, it works when you click on gmail, but when you click on a link (open an email with some links in them) it doesn't work anymore. 就像评论者指出的那样,JQuery没有加载Gmail,这里是一个非JQuery版本,它可以在你点击gmail时工作,但当你点击一个链接(打开一个包含其中一些链接的电子邮件)时它不会工作了。

Most DOM events, including the click event are dispatched as follows: 大多数DOM事件(包括click事件)都按如下方式分派:

  1. Capture phase (from document ( window , document , document.documentElement , ...) down the tree to the clicked element). 捕获阶段(从document (从windowdocumentdocument.documentElement ,...)向下到树中的单击元素。
  2. Bubble phase (from the clicked element up the tree to document ..., window ). 气泡阶段(从树上的点击元素到document ......, window )。

Events bound with jQuery are always triggered in #2, at the bubbling phase. 与jQuery绑定的事件总是在#2处触发,处于冒泡阶段。 Gmail appears to bind an event add capture phase, and calls event.stopPropagation(); Gmail似乎绑定了一个事件添加捕获阶段,并调用event.stopPropagation(); when the user clicks on <a> elements. 当用户点击<a>元素时。 This methods stops the propagation of the event, so jQuery will never know about the event because #2 never occurs. 该方法停止了事件的传播,因此jQuery将永远不会知道该事件,因为永远不会发生#2。

So, drop jQuery and use vanilla JavaScript to bind the event: 因此,删除jQuery并使用普通JavaScript绑定事件:

document.addEventListener('click', function(event) {
    // Do whatever you want
}, true);

By setting the third argument of addEventListener to true , the listener is triggered at capture phase. 通过将addEventListener的第三个参数设置为true ,将在捕获阶段触发侦听器。


Regarding the jQuery error (from the question): 关于jQuery错误(来自问题):
Gmail doesn't load jQuery, so you cannot use it. Gmail不会加载jQuery,因此您无法使用它。 Usually, you would get ReferenceError: "$ is not defined". 通常,您将获得ReferenceError:“未定义$”。 Instead, you saw "TypeError: Object # has no method 'on'", because $ is defined as a shorthand for document.querySelector in Chrome's developer tools when the page doesn't declare $ . 相反,你看到“TypeError:Object#在''上没有方法',因为当页面没有声明$时, $被定义为Chrome开发者工具中document.querySelector的简写。

If you included jQuery with your extension, it can be used by switching to the appropriate context. 如果您在扩展中包含jQuery,则可以通过切换到适当的上下文来使用它。 For more details, see Why will jQuery not load in Facebook? 有关更多详细信息,请参阅为什么jQuery无法在Facebook中加载?

我戳了一下正在加载的gmail,看来gmail并没有加载jquery。

Not all websites use jQuery. 并非所有网站都使用jQuery。 If you want to have access to a specific jQuery library version you're going to need to load that so you have access to it in your extension. 如果您想要访问特定的jQuery库版本,则需要加载该版本,以便在扩展中访问它。

So I tested it, and it works just fine. 所以我测试了它,它工作得很好。 Like I said in my comment, you need to inject jQuery yourself in all cases. 就像我在评论中说的那样,在任何情况下都需要自己注入jQuery。 You cannot access the page's javascript, nor any javascript from other extensions. 您无法访问页面的javascript,也不能访问其他扩展名的任何javascript。 Some example code: 一些示例代码:

manifest.json 的manifest.json

{
  "name": "Gmail jQuery Test",
  "version": "0.1",
  "description": "Gmail jQuery Test",
  "manifest_version": 2,
  "permissions": [
    "https://mail.google.com/*"
  ],
  "content_scripts": [{
    "matches": ["https://mail.google.com/*"],
    "js": ["jquery.js", "script.js"],
    "all_frames": true
  }]
}

script.js 的script.js

$('body').on('click', function(event) {
  console.log("Entered function");
});

This shows the message on the inbox page, and inside of any message and other pages. 这会在收件箱页面上以及任何消息和其他页面的内部显示该消息。

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

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