我正在使用 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/