可编写脚本的 NPAPI 插件不适用于 Firefox
我正在开发协同工作的 FF 扩展和插件。我的扩展将 npapi 插件注入到 html 中,并在事件发生后调用插件的某些方法。
这是我用于注入的代码:
if (window.content.document.getElementById("rondyoHookMessageElement") == null) {
var element = window.content.document.createElement("object");
element.type = "application/x-hook-msg";
element.id = "rondyoHookMessageElement";
element.width = 0;
element.height = 0;
window.content.document.body.appendChild(element);
}
当我需要使用插件的方法时,我会执行以下操作:
var element = window.content.document.getElementById("rondyoHookMessageElement");
element.SomeFunc();
我确认已找到该元素,但记录 element.SomeFunc
返回 undefined
。
如果我手动注入 npapi 插件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
<object id="plugin" type="application/plugin-mime" width=200 height=200 border=5></object>
<script type="text/javascript">
var plugin = document.getElementById("plugin");
dump(plugin.SomeFunc + "\n");
</script>
</body>
</html>
它返回 function SomeFunc() { [native code] }
操作系统:Mac OS X 10.6.7
FF: 3.6.13
I'm developing FF extension and plugin that work in tandem. My Extension injects npapi plugin into the html and calls some method of the plugin after an event occures.
Here is the code I use for injection:
if (window.content.document.getElementById("rondyoHookMessageElement") == null) {
var element = window.content.document.createElement("object");
element.type = "application/x-hook-msg";
element.id = "rondyoHookMessageElement";
element.width = 0;
element.height = 0;
window.content.document.body.appendChild(element);
}
And when I need to use a method of the plugin I do the following:
var element = window.content.document.getElementById("rondyoHookMessageElement");
element.SomeFunc();
I confirm that element is found, but logging the element.SomeFunc
returns undefined
.
If I inject the npapi plugin manually:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
<object id="plugin" type="application/plugin-mime" width=200 height=200 border=5></object>
<script type="text/javascript">
var plugin = document.getElementById("plugin");
dump(plugin.SomeFunc + "\n");
</script>
</body>
</html>
It returns function SomeFunc() { [native code] }
OS: Mac OS X 10.6.7
FF: 3.6.13
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 FireFox 4 中执行此操作,则很有可能导致浏览器崩溃(该错误已被记录,但尚未修复)。在将对象标签注入 DOM 之前设置其类型并不是一个好主意;您会在每个浏览器上看到不同的行为。等到你将对象放入 dom 中,然后注入它。
另一个可能的问题是,有时浏览器在将其注入 DOM 后需要一段时间才能访问插件,因此如果您使用 setTimeout 等待半秒左右,它可能会在那时开始工作。
If you do this in FireFox 4 you have a decent chance of crashing the browser (the bug has been logged, but not yet fixed). it's not a good idea to set the type of the object tag before injecting it into the DOM; you'll get different behavior on each browser. Wait until you've put the object into the dom and then inject it.
Another possible problem is that it sometimes takes the browser some time after injecting it into the DOM before the plugin is accessible, so if you use a setTimeout to wait for a half second or so it might start working at that point.
我通过扩展调用 SomeFunc 的脚本解决了这个问题:
当我需要从扩展调用此函数时,我这样做:
I solved the problem by extending script which make a call of the SomeFunc:
When I need to call this function from the extension I do: