javascript - 如何使用 getElementsByClassName 函数来获取 Gmail 撰写页面中的 "send"按钮?

标签 javascript google-chrome google-chrome-extension gmail

我正在使用 JavaScript 为 Chrome 浏览器编写 Gmail 扩展程序。 我正在尝试使用以下代码从 Compose 页面获取 send 按钮元素:

SendButton  = document.getElementById('js_frame').contentDocument.getElementsByClassName('T-I J-J5-Ji Bq nS T-I-KE L3')[0];

但后来我不断收到一条消息,说 typeof SendButton 未定义

我该如何解决这个问题?

最佳答案

您正在查看错误的文档。

Gmail 的 #js_frame是一个不可见的框架,负责非 UI 内容(其主体仅包含 <script> 标签,仅供引用)。在你的previous question ,我已经展示了如何获得正确的引用:

  • document.getElementById('canvas_frame').contentDocument如果存在的话
  • document如果#canvas_frame不存在,但是 js_frame做。 js_frame` 用于排除 Gmail 域中的非 Gmail 页面,例如帮助页面。

所以,你在得到doc2之后(参见 previous answer ),您需要查询该文档上的元素:

// Paste declaration of doc2 from https://stackoverflow.com/a/14032326/938089
// And to get the button when available
if (doc2) {
    sendButton = doc2.getElementsByClassName('T-I J-J5-Ji Bq nS T-I-KE L3')[0];
}

关于javascript - 如何使用 getElementsByClassName 函数来获取 Gmail 撰写页面中的 "send"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032553/

相关文章:

javascript - http 到 https - 使浏览器为 http URL 请求相应的 https URL,而不需要编辑所有页面并手动将所有 URL 更改为 https?

javascript - Node.dispatchEvent(new CustomEvent ('click' )) 自 Chrome v53 起不再触发 anchor

html - Firefox/Chrome 兼容性 : HTML table with fixed header and column

javascript - Chrome 扩展程序内容脚本在页面刷新之前不会加载

javascript - Chrome 扩展中的 popup.js 中的变量值是否会刷新?

google-chrome-extension - 从弹出窗口发送消息到内容脚本 - Chrome 扩展

javascript - 跨域通信

javascript - 如何仅替换元素上的文本内容?

javascript - 如何将对象值转换为JavaScript中的函数?

javascript - Chrome 扩展 - 在全屏视频顶部显示自定义通知/弹出窗口(HTML 元素)