2

我已经编写了一个 NPAPI 插件,它实现了所需的所有逻辑,现在我正在编写一个扩展,预计将使用插件中提供的功能。这种架构让我有机会为 Mozilla Firefox 和 Google Chrome 编写相同的 C++ 代码。

在 Chrome 中,我通过将构造写入我的扩展拥有的单独文档来实例化插件中定义的对象<embed ...>(它自动提供给我的 Chrome 插件)。那行得通。在 C++ 代码中,我执行插件对象的构造函数中所需的所有工作。

我无法轻松地调整我的解决方案以在 Firefox 中使用它,因为扩展没有任何单独的文档支持,并且我的扩展无权写入任何已呈现的文档。

我最常见的主要问题是如何多次使用插件提供的功能,并在用户单击按钮或在下拉菜单中选择我的条目时将参数列表传递给我的本机函数(即使用参数应该在特定事件之后调用,而不仅仅是在任意时间)?

“补充”问题是:

如何在 Mozilla Firefox 中实例化插件?我在哪里可以获得将由 FF “解释”并且扩展能够写入的文档?

4

2 回答 2

1

我自己不知道该怎么做,但这是一个开源的 firefox 扩展:https ://github.com/kylehuff/webpg-firefox

于 2012-12-14T11:53:32.693 回答
0

Your Firefox extension needs to make use of a "browser overlay". There are many types of overlays, for various parts of the browser, and they are loaded (overlay'ed) within the specified document, as defined in the chrome.manifest file.

For example, the following applies an overlay to the "browser.xul" file (which is the main browser window)

overlay     chrome://browser/content/browser.xul            content/firefoxOverlay.xul

Now, within that overlay file you can load your plugin object and call the methods provided by the plugin.

Here is an example XUL file which does nothing more than load the NPAPI plugin of the content-type "application/x-example-plugin", and assign the plugin object to the variable "myPlugin"

<script type="text/javascript">
    myPlugin = document.getElementById("myPlugin");
</script>
<?xml version="1.0" encoding="UTF-8"?>
<overlay id="myOverlay" xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script type="text/javascript">
        myPlugin = document.getElementById("myPlugin");
        alert(myPlugin.someFunction());
    </scrpit>
    <window id="main-window">
        <vbox collapsed="true">
            <html:object id="myPlugin" type="application/x-example-plugin" height="1" width="1" style="visibility:hidden;"></html:object>
        </vbox>
    </window>
</overlay>

Some important things to note

  1. The "xmlns:html=..." declaration is critical, because the plugin object is being loaded in an html:object, and that declaration tells the XUL parser how to render the object.
  2. The id of the window ("main-window") is important, because that is how the XUL parser will overlay the item (within the "main" window)
  3. The object id ("myPlugin", in this example) is how you will reference the plugin object in via the JavaScript later.
  4. Because we are not showing content with our plugin, merely calling public methods provided by it, it is important to make the CSS visibility has "hidden", and the size to 1x1 pixels. Without doing this, you could end up with large blank spaces rendered in the browser UI. Additionally, it is important to use the CSS "visibility" property, not the "display" property. If you set the display property to "none", you will have issues with your plugin actually being initialized within the overlay.
  5. In your extension install.rdf file, you must specify the "unpack" property as "true", i.e.: <em:unpack>true</em:unpack>

Once your overlay is loaded within the context of the browser XUL, and your plugin initialized within the main browser window, you can reference your plugin from within the scope of the main window by the variable you have assigned it to ("myPlugin" in this example).

I will not go into depth here on how to obtain the context of the main window (see the links below), but you once you have a reference you can invoke the exposed methods from within content scripts, sidebars, toolbars, etc.

Reference links -

于 2013-04-29T22:45:50.613 回答