简体   繁体   English

Gmail Chrome扩展程序“ chrome.extension”未定义

[英]Gmail Chrome Extension “chrome.extension” undefined

I have a content script running inside the Gmail UI and I am injecting some new HTML at different points. 我在Gmail用户界面中运行了一个内容脚本,并且在不同的位置注入了一些新的HTML。 This is all working, but to keep the code clean I wanted to insert pre-defined HTML markup into certain parts of the page in a single function call. 一切正常,但是为了保持代码整洁,我想在单个函数调用中将预定义的HTML标记插入页面的某些部分。

This all seems feasible using chrome.extension.getURL(), but when I was testing this, it looks like chrome.extension is undefined! 使用chrome.extension.getURL()这一切似乎都是可行的,但是当我进行测试时,好像chrome.extension是未定义的! I was unable to find anywhere in the documentation that says this shouldn't be defined and several places where it says it should. 我在文档中找不到该定义的地方,也找不到它应该定义的地方。

I am using chrome Version 39.0.2171.95 (64-bit) 我正在使用Chrome版本39.0.2171.95(64位)

Here is what the chrome object looks like in the javascript console: chrome对象在javascript控制台中的外观如下:

铬物体

First off, anyone know why chrome.extension might be undefined? 首先,有人知道为什么chrome.extension可能未定义吗? Secondly, is there another (perhaps better) way to do this? 其次,还有另一种(也许更好)的方法吗? I'd rather not just programmatically inject a large amount HTML into the page in code using jQuery, etc. in the content script, but can resort to this if there is no other way. 我宁愿不只是使用内容脚本中的jQuery等以编程方式在页面中注入大量HTML,而且如果没有其他方法,可以采用这种方法。

If you're testing this in the console, you must be aware of the concept of the Isolated World . 如果要在控制台中进行测试,则必须了解孤立世界的概念。 When you add a content script, it has a separate JavaScript context for itself that is isolated from the page. 添加内容脚本时,它具有与页面隔离的单独的JavaScript上下文。

At the top of the Console tab of Dev Tools, you'll see <top frame> in a dropdown. 在“开发工具”的“控制台”选项卡的顶部,您会在下拉列表中看到<top frame> This is a dropdown that selects JS context it is executing in. It will list frames inside the document and all extensions that have content scripts injected. 这是一个下拉列表,用于选择要在其中执行的JS上下文。它将列出文档内的框架以及所有注入了内容脚本的扩展。

The chrome object you show in the screenshot corresponds to what the webpage normally sees. 您在屏幕快照中显示的chrome对象与该网页通常看到的内容相对应。 If you switch context, you'll see a different picture: 如果切换上下文,则会看到不同的图片:

切换上下文

In any case, if you are really executing chrome.extension.getURL() in the content script - it will be well defined. 无论如何,如果您确实在内容脚本中执行chrome.extension.getURL() -它将得到很好的定义。


Now, if you injected a <script> tag in the page and tried it in that code - it would fail again, since the code will be in the page context. 现在,如果您在页面中注入了<script>标记并在该代码中对其进行了尝试-它将再次失败,因为该代码将位于页面上下文中。 See this question for this situation. 有关这种情况,请参阅此问题

Finally, most of the Chrome API will not be exposed to content scripts for security reasons. 最后,出于安全原因, 大多数 Chrome API不会公开给内容脚本。 If an API is undefined when it shouldn't be , you may need a background page to do the job for you. 如果未定义API时未定义,则可能需要一个后台页面来为您完成这项工作。

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

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