chrome.extension.getBackgroundPage 在 iframe 的扩展页面中未定义
chrome.extension.getBackgroundPage is undefined in an extension page in an iframe
我正在尝试使用 chrome.extension.getBackgroundPage
函数访问我的扩展程序的后台页面。
但是,我收到以下错误:
Uncaught TypeError: chrome.extension.getBackgroundPage is not a function
我从我的 bar.js
文件中调用该函数,该文件在我的 manifest.json
中定义为 web_accessible_resource
如何让它发挥作用?
manifest.json
{
"manifest_version": 2,
"name": "XPath Helper",
"version": "1.0.13",
"description": "Extract, edit, and evaluate XPath queries with ease.",
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["content.css"],
"js": ["content.js"]
}
],
"permissions": ["http://*/", "tabs", "identity", "identity.email"],
"icons": {
"32": "static/icon32.png",
"48": "static/icon48.png",
"128": "static/icon128.png"
},
"web_accessible_resources": [
"bar.css",
"bar.html",
"bar.js"
]
}
bar.js是bar.html
里面的脚本(不是内容脚本):
// ...
document.addEventListener('DOMContentLoaded',function(){
// previosuly chrome.extension.getBackgroundPage()
chrome.runtime.getBackgroundPage(function(page){
alert("hello");
})
})
content.js
// ...
this.barFrame_ = document.createElement('iframe');
this.barFrame_.src = chrome.extension.getURL('bar.html');
document.body.appendChild(this.barFrame_);
// ...
如果页面在扩展过程中运行,大多数扩展 API 只能使用,即顶级框架是非 sandboxed chrome-extension:
页。
chrome-extension:
-非扩展进程中的框架只能访问content scripts可用的扩展API和网页。与内容脚本不同的是,它们还可以在扩展的来源使用网络平台 API。例如,如果您在内容脚本中使用 localStorage
,则会访问运行内容脚本的页面的 DOM 存储。如果您在 chrome-extension:
页面中使用 localStorage
,那么您将获得扩展程序的存储空间。
如果您想在您的框架中访问背景页面的功能,请使用 extension messaging APIs 在您的框架和背景页面之间进行通信。
我正在尝试使用 chrome.extension.getBackgroundPage
函数访问我的扩展程序的后台页面。
但是,我收到以下错误:
Uncaught TypeError: chrome.extension.getBackgroundPage is not a function
我从我的 bar.js
文件中调用该函数,该文件在我的 manifest.json
web_accessible_resource
如何让它发挥作用?
manifest.json
{
"manifest_version": 2,
"name": "XPath Helper",
"version": "1.0.13",
"description": "Extract, edit, and evaluate XPath queries with ease.",
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["content.css"],
"js": ["content.js"]
}
],
"permissions": ["http://*/", "tabs", "identity", "identity.email"],
"icons": {
"32": "static/icon32.png",
"48": "static/icon48.png",
"128": "static/icon128.png"
},
"web_accessible_resources": [
"bar.css",
"bar.html",
"bar.js"
]
}
bar.js是bar.html
里面的脚本(不是内容脚本):
// ...
document.addEventListener('DOMContentLoaded',function(){
// previosuly chrome.extension.getBackgroundPage()
chrome.runtime.getBackgroundPage(function(page){
alert("hello");
})
})
content.js
// ...
this.barFrame_ = document.createElement('iframe');
this.barFrame_.src = chrome.extension.getURL('bar.html');
document.body.appendChild(this.barFrame_);
// ...
如果页面在扩展过程中运行,大多数扩展 API 只能使用,即顶级框架是非 sandboxed chrome-extension:
页。
chrome-extension:
-非扩展进程中的框架只能访问content scripts可用的扩展API和网页。与内容脚本不同的是,它们还可以在扩展的来源使用网络平台 API。例如,如果您在内容脚本中使用 localStorage
,则会访问运行内容脚本的页面的 DOM 存储。如果您在 chrome-extension:
页面中使用 localStorage
,那么您将获得扩展程序的存储空间。
如果您想在您的框架中访问背景页面的功能,请使用 extension messaging APIs 在您的框架和背景页面之间进行通信。