我正在开发我的第一个扩展,并尝试创建一个简单的扩展来在页面上注入(inject)可拖动的 div。这很好用,但我想保留 div 在后台页面上的位置(我也在尝试本地存储,但想了解为什么这不起作用)。
我不需要按钮,因此没有创建 popup.html 文件,我相信该文件完全是可选的。到目前为止,仅注入(inject) javascript 文件确实有效。
但是,我现在在执行 chrome.extension.getBackgroundPage() 时抛出以下错误:
未捕获错误:chrome.extension.getBackgroundPage 只能在扩展进程中使用。有关更多详细信息,请参阅内容脚本文档。
内容脚本文档似乎没有发现我的方法有任何问题:http://code.google.com/chrome/extensions/content_scripts.html
这是我正在使用的经过编辑的 list :
{
"name": "My helper",
"version": "1.0",
"description": "Tastes great",
"background_page": "background.html",
"content_scripts": [
{
"matches":["https://page.of.interest/*"],
"run_at":"document_idle",
"js":[ "jquery.js", "jquery-ui-1.8.17.custom.min.js", "my_content_script.js"],
"css": [ "my_content_script.css" ]
}
],
"permissions": [
"background"
]
}
所以我在“my_content_script.js”中运行此语句。这是否不被视为扩展过程的一部分?我只能在 popup.html (或其他可能的?)文件上运行它吗?
如果是这种情况,那么也许只使用本地存储比尝试通过 dom 与扩展进程进行通信更容易。
希望我已经说清楚了,尽管我对其中一些概念一无所知。
最佳答案
我认为文档并没有明确表示您不能从 content_script
使用 chrome.extension.getBackgroundPage()
,而是因为 content_script
code> 具有更面向其正在运行的页面的权限,不允许访问。 docs do mention不过,您可以使用一些方法。
Unlike the other chrome.* APIs, parts of chrome.extension can be used by content scripts:
您必须使用message passing在 background_page
和 content_script
之间进行通信。
关于google-chrome-extension - 执行 chrome.extension.getBackgroundPage() 时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9165810/