可编写脚本的 NPAPI 插件不适用于 Firefox

发布于 2024-11-02 06:39:26 字数 1399 浏览 2 评论 0原文

我正在开发协同工作的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-11-09 06:39:26

如果您在 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.

蓝天 2024-11-09 06:39:26

我通过扩展调用 SomeFunc 的脚本解决了这个问题:

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 script = doc.createElement("script");
            script.type = "text/javascript";
            script.innerHTML = 'function f(doc, messageId, data) { document.getElementById("rondyoHookMessageElement").SomeFunc(doc, messageId, data); };';
            doc.body.appendChild(script);
}

当我需要从扩展调用此函数时,我这样做:

window.content.document.defaultView.wrappedJSObject.f(null, mes, false);

I solved the problem by extending script which make a call of the SomeFunc:

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 script = doc.createElement("script");
            script.type = "text/javascript";
            script.innerHTML = 'function f(doc, messageId, data) { document.getElementById("rondyoHookMessageElement").SomeFunc(doc, messageId, data); };';
            doc.body.appendChild(script);
}

When I need to call this function from the extension I do:

window.content.document.defaultView.wrappedJSObject.f(null, mes, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文