我已经创建了一个扩展,并且正在尝试检测该扩展是否存在于我的虚拟主机之一中。
以下是检测扩展是否存在的代码:(script.js
)
var detect = function(base, if_installed, if_not_installed) {
var s = document.createElement('script');
s.onerror = if_not_installed;
s.onload = if_installed;
document.body.appendChild(s);
s.src = base + '/manifest.json';
};
detect('chrome-extension://' + "myExtensionId", function() {alert('boom!');},
function() {alert("buf. nothin")});
当我在浏览器中打开虚拟主机时,它会提醒我:buf。什么都没有并且在控制台日志中出现此错误:
- 拒绝加载 chrome-extension://myExtensionId/manifest.json。资源必须列在 web_accessible_resources list 键中,才能由扩展程序外部的页面加载
如果我单击我创建的扩展程序,它会发出警报 boom 并且我没有收到任何错误,因此这意味着在扩展程序中它会检测到我的扩展程序,但在我的虚拟主机中却不会。
因此,可以检测扩展程序中是否存在扩展程序,但是可以从网页中检测扩展程序是否存在吗?
最佳答案
如果您拥有该页面,则可以向扩展程序发送消息。如果有响应,则已安装。文件:https://developer.chrome.com/extensions/messaging#external-webpage
list .json
"externally_connectable": {
"matches": ["https://example.com/*"]
}
页面.js
var id = "randomlettersfromwebstoreurl"
chrome.runtime.sendMessage(id, "installed?", function(response) {
if(response) {
// The extension is installed
} else {
// The extension is not installed or disabled
} })
扩展.js
chrome.runtime.onMessageExternal.addListener(function(request, sender, respond) {
if(request == "installed?") {
respond(true)
} })
关于javascript - 我可以检测网页中是否存在扩展程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675092/