[英]I get undefined error when trying to access iframes in gmail from my chrome extension but not from the developer console
I'm making a chrome extension (content script) that will add text after the "More" in the top black bar in gmail. 我正在制作一个Chrome扩展程序(内容脚本),该扩展程序将在gmail顶部黑色栏中的“更多”后面添加文本。 Those elements are encapsulated in the
canvas_frame
iframe. 这些元件被封装在
canvas_frame
iframe中。
When I try to access those elements with: 当我尝试通过以下方式访问这些元素时:
document.getElementById('canvas_frame').contentWindow.document.getElementById('gbz').innerHTML = scr;
I get Uncaught TypeError: Cannot read property 'document' of undefined
. 我收到
Uncaught TypeError: Cannot read property 'document' of undefined
。 But if I type that code directly into the chrome developer console it works. 但是,如果我直接在chrome开发人员控制台中键入该代码,它将可以正常工作。 Why is this?
为什么是这样? How can I access and modify that element from my chrome extension.
如何访问和从我的浏览器扩展程序修改该元素。
I'm new with javascript so I am likely missing something obvious. 我是新的JavaScript,所以我可能失去了一些东西明显。 Thanks.
谢谢。
尝试
document.getElementById('canvas_frame').contentDocument.document.getElementById('gbz').innerHTML = src;
I have built Chrome Extensions myself and have encountered this issued, there are a couple of ways to mitigate it, one is to run a timer loop in JS looking for #canvas_frame and stop it when you find it, the other is to listen for DOM node insert events and again, if the element you are looking for has been inserted, start doing what you want to do. 我自己构建了Chrome扩展程序,并遇到了此问题,有两种方法可以缓解它,一种是在JS中运行计时器循环以查找#canvas_frame并在找到它时将其停止,另一种方法是侦听DOM节点插入事件,如果已插入您要查找的元素,请再次执行该操作。
While canvas_frame has been the "content area" element for Gmail to interact with if that's what you need to do, currently (as of a few days ago) I've encountered instances of Google Apps Gmail that DO NOT have a content_frame, or rather, have one for an instant when the page loads and then it gets removed, I'm not sure what shenanigans Google is pulling, but since they don't provide an official API for making Gmail Chrome Extensions (so very unfortunately), we are at their mercy as Extension developers... 尽管canvas_frame一直是Gmail可以与之交互的“内容区域”元素,但目前(截至几天前),我遇到的Google Apps Gmail实例没有content_frame,或者,请在页面加载后立即将其删除,然后再将其删除。我不确定Google在拉扯什么恶作剧,但是由于它们没有提供用于制作Gmail Chrome浏览器扩展程序的官方API(因此非常遗憾),作为扩展开发人员摆布...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.