问题描述
将Chrome的扩展程序移植到FF中
遵循本教程(在Chrome中可以正常工作):
从网页发送消息到扩展名:
在(web)pagescript.js中有:
函数IsExist(extensionId,callback){
chrome.runtime.sendMessage(extensionId,{message:installed},
function(reply){
if(reply){
callback(true);
} else {
callback(false);
}
});
IsExist(你的扩展名,函数(已安装){
如果(!已安装){
alert(请安装扩展名);
}
});
从扩展程序中的网页接收邮件:
chrome.runtime.onMessageExternal.addListener(
函数(req,sender,callback){
if(req){$ b $如果(req.message){
if(req.message ==installed){
callback(true);
}
}
}
返回true;
});
我试图达到的目标
当我的网站上没有安装扩展程序时,需要将网页重定向回主页。因此,这些网页需要能够找出(自己)是否安装了扩展程序。
打开网页时出现错误
ReferenceError:chrome没有定义。 (我也尝试过browser.runtime.onMessageExternal,但它抛出浏览器没有定义)。
是否有没有办法做到这一点类似于在Chrome中可以做什么?
感谢所有评论这是我想出的。 (我必须去document_end(altho comments advise document_start),因为我在content_script.js中发生了其他事情。
$ b 在我的附加组件content_script.js中
document.body.classList.add(plugininstalledblabla);
在我的插件的manifest.json中
<$ p
$匹配:[*:// * / *],
all_frames :true,
js:[content_script.js],
run_at:document_end
}
]
在我的网站的main.js脚本中
$ b
$(window).on(load,function(){// !! Window onLoad cause:document_end - > DOM应该在这里加载
// Set
body = $('body');
if(document.body.classList.contains(plugininstalledblabla)){
console.log('addon is installed');
}
}) ;
Porting extension from Chrome into FF
Followed this tutorial (which works fine in Chrome): http://www.codingscripts.com/check-whether-user-has-a-chrome-extension-installed/
Sending message from webpage to extension:In (web)pagescript.js this has:
function IsExist(extensionId,callback){
chrome.runtime.sendMessage(extensionId, { message: "installed" },
function (reply) {
if (reply) {
callback(true);
}else{
callback(false);
}
});
}
IsExist("Your extension id",function(installed){
if(!installed){
alert("Please install extension ");
}
});
Receiving message from webpage in extension:
chrome.runtime.onMessageExternal.addListener(
function(req, sender, callback) {
if (req) {
if (req.message) {
if (req.message == "installed") {
callback(true);
}
}
}
return true;
});
What I'm trying to achieve
A couple of html pages on my website need to redirect back to the homepage when the extension is NOT installed. So those pages need to be able to figure out (on their own) if the extension is installed or not.
Error I'm getting when I open webpage
ReferenceError : chrome is not defined. (I also tried with browser.runtime.onMessageExternal but then it throws "browser" is not defined).Is there no way to do this similar to what can be done in Chrome ?
Thanks to all the comments this is what I came up with. (I had to go for document_end (altho comments advise document_start) cause I had other things going on in content_script.js
In my add-on's content_script.js
document.body.classList.add("plugininstalledblabla");
In my add-on's manifest.json
"content_scripts":
[
{
"matches": ["*://*/*"],
"all_frames": true,
"js": ["content_script.js"],
"run_at": "document_end"
}
]
in my website's main.js script
$(window).on("load", function() { // !! Window onLoad cause : document_end -> DOM should be loaded here
// Set
$body = $('body');
if(document.body.classList.contains("plugininstalledblabla")){
console.log('addon is installed');
}
});
这篇关于Firefox WebExtension:检查我的扩展名是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!