Google chrome extension 内容脚本中chrome扩展的管理权限
我有一个chrome扩展,它应该检查是否有另一个扩展,并在Google chrome extension 内容脚本中chrome扩展的管理权限,google-chrome-extension,Google Chrome Extension,我有一个chrome扩展,它应该检查是否有另一个扩展,并在mywebsite.com中的每个网页上添加一个div,如果没有。所以我把这个放在清单上: "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["http://my-website.com/*"], "js": ["content.js"], "run_at":
mywebsite.com
中的每个网页上添加一个div,如果没有。所以我把这个放在清单上:
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://my-website.com/*"],
"js": ["content.js"],
"run_at": "document_end",
"all_frames": true
}
],
"permissions": [
"<all_urls>",
"management",
"activeTab",
"webRequest",
"webRequestBlocking"
]
但我明白了
Uncaught TypeError: Cannot read property 'getAll' of undefined
在网页上。但是,当我打开扩展的开发工具时(chrome://extensions ->我可以使用
chrome.management
很好。如何在内容脚本上使用chrome.management
(或执行类似操作)?可能的情况是,您的内容脚本要求后台脚本获取安装的应用程序列表
将其放入内容脚本中
chrome.runtime.sendMessage({messageName: 'getAllApps'}, function(apps) {
// do what you want in with the apps list
});
在后台脚本中,侦听请求并返回应用程序列表
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if(message.messageName === 'getAllApps') {
chrome.management.getAll(function (apps) {
sendResponse(apps);
});
}
}
);
编辑
正如在帖子的评论中所说,内容脚本对chrome api的访问权限有限,这就是为什么您唯一的解决方案是与可以访问所有内容的后台脚本通信。可能的情况是,您的内容脚本要求后台脚本获取安装的应用程序列表 将其放入内容脚本中
chrome.runtime.sendMessage({messageName: 'getAllApps'}, function(apps) {
// do what you want in with the apps list
});
在后台脚本中,侦听请求并返回应用程序列表
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if(message.messageName === 'getAllApps') {
chrome.management.getAll(function (apps) {
sendResponse(apps);
});
}
}
);
编辑
正如在帖子的评论中所说,内容脚本对chrome api的访问权限有限,这就是为什么您唯一的解决方案是与可以访问所有内容的后台脚本通信。可能的情况是,您的内容脚本要求后台脚本获取安装的应用程序列表 将其放入内容脚本中
chrome.runtime.sendMessage({messageName: 'getAllApps'}, function(apps) {
// do what you want in with the apps list
});
在后台脚本中,侦听请求并返回应用程序列表
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if(message.messageName === 'getAllApps') {
chrome.management.getAll(function (apps) {
sendResponse(apps);
});
}
}
);
编辑
正如在帖子的评论中所说,内容脚本对chrome api的访问权限有限,这就是为什么您唯一的解决方案是与可以访问所有内容的后台脚本通信。可能的情况是,您的内容脚本要求后台脚本获取安装的应用程序列表 将其放入内容脚本中
chrome.runtime.sendMessage({messageName: 'getAllApps'}, function(apps) {
// do what you want in with the apps list
});
在后台脚本中,侦听请求并返回应用程序列表
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if(message.messageName === 'getAllApps') {
chrome.management.getAll(function (apps) {
sendResponse(apps);
});
}
}
);
编辑
正如在帖子的评论中所说,内容脚本对chrome api的访问有限,这就是为什么您唯一的解决方案是与可以访问所有内容的后台脚本进行通信。我认为您遗漏了问题中的某些内容!!我不知道你的意思,我把问题精炼了一点…我想你在问题中遗漏了什么!!我不知道你的意思,我把问题精炼了一点…我想你在问题中遗漏了什么!!我不知道你的意思,我把问题精炼了一点…我想你在问题中遗漏了什么!!我不确定你的意思,我对问题进行了一点改进……你应该在回答中强调内容脚本。你应该在回答中强调内容脚本。你应该在回答中强调内容脚本。你应该在回答中强调内容脚本。