简体   繁体   English

从HTML页面中的Javascript访问Firefox扩展XPCOM对象

[英]Access Firefox extension XPCOM object from Javascript inside an HTML page

I'm trying to get the most basic XPCOM javascript object to be accessible to the javascript I load into my webpage. 我正在尝试将最基本的XPCOM javascript对象添加到我加载到我的网页的javascript中。 I'm using the example code from this tutorial: https://developer.mozilla.org/en-US/docs/How_to_Build_an_XPCOM_Component_in_Javascript 我正在使用本教程中的示例代码: https//developer.mozilla.org/en-US/docs/How_to_Build_an_XPCOM_Component_in_Javascript

Here is my set up: 这是我的设置:


install.rdf: install.rdf的:

<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:em="http://www.mozilla.org/2004/em-rdf#">

    <Description about="urn:mozilla:install-manifest">
        <em:id>helloworld@thellamatesting.com</em:id>
        <em:name>Hello World</em:name>
        <em:version>1.0</em:version>
        <em:type>2</em:type>
        <em:creator>The Llama</em:creator>
        <em:description>Testing</em:description>

        <em:targetApplication>
            <Description>
                <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
                <em:minVersion>2.0</em:minVersion>
                <em:maxVersion>20.0</em:maxVersion>
            </Description>
        </em:targetApplication>
    </Description>      
</RDF>



chrome.manifest chrome.manifest用于

content     helloworld    chrome/content/
content     helloworld    chrome/content/ contentaccessible=yes
overlay chrome://browser/content/browser.xul chrome://helloworld/content/browser.xul

component {4762b5c0-5b32-11e2-bcfd-0800200c9a66} components/HelloWorld.js
contract @thellamatesting.com/helloworld;1 {4762b5c0-5b32-11e2-bcfd-0800200c9a66}

locale  helloworld  en-US   locale/en-US/



components/HelloWorld.js 组件/ HelloWorld.js

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function HelloWorld() {
    // If you only need to access your component from Javascript, uncomment the following line:
    this.wrappedJSObject = this;
}

HelloWorld.prototype = {
    classDescription: "My Hello World Javascript XPCOM Component",
    classID:          Components.ID("{4762b5c0-5b32-11e2-bcfd-0800200c9a66}"),
    //Also tried
    //classID:          Components.ID("4762b5c0-5b32-11e2-bcfd-0800200c9a66"),
    contractID:       "@thellamatesting.com/helloworld;1",
    QueryInterface: XPCOMUtils.generateQI(),
    // Also tried
    //QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIHelloWorld]),
    hello: function() { 
        return "Hello World!"; 
    }
};

var components = [HelloWorld];
if ("generateNSGetFactory" in XPCOMUtils)
  var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);  // Firefox 4.0 and higher
else
  var NSGetModule = XPCOMUtils.generateNSGetModule(components);    // Firefox 3.x



Testing HTML: 测试HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="application/javascript">

            function go() {
                try {
                    var coms = Components;
                    alert(Components.classes);
                    var myComponent = Components.classes['@thellamatesting.com/helloworld;1'].getService().wrappedJSObject;
                    alert(myComponent.hello());
                } catch (anError) {
                        dump("ERROR: " + anError);
                }
            };

        </script>
    </head>
    <body>

        <button onclick="javascript:go()">Click to go</button>

    </body>
</html>

After all this, I end up with "Components.classes is undefined". 毕竟,我最终得到“Components.classes未定义”。 Does anyone know what I'm doing wrong here? 有谁知道我在这里做错了什么?

Thanks so much! 非常感谢!

In order to gain access to the Components object from a javascript context, you need to have extended capabilities, that is, run from a chrome:// URL. 为了从javascript上下文访问Components对象,您需要具有扩展功能,即从chrome:// URL运行。 There used to be a way for a regular web page (served from http://) to request extended capabilities (called UniversalXPConnect ) but it has been removed out of security concerns. 曾经有一个常规的网页的方式(从http服务://)要求扩展功能(称为UniversalXPConnect ),但它已被删除出于安全考虑。

I think you should tell us a little more about what it is you're trying to achieve. 我想你应该多告诉我们你想要实现的目标。 If you're trying to export data from your addon into a webpage, the AddonSDK (see https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/ ) has a very good protocol for doing that called page-mod; 如果您尝试将插件中的数据导出到网页中,AddonSDK(请参阅https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/ )非常好这样做的协议称为page-mod; it allows you to inject data into web pages. 它允许您将数据注入网页。

Thanks to Jonathan's advice I was able to come up with a great solution to this problem. 感谢Jonathan的建议,我能够为这个问题找到一个很好的解决方案。 Here is the code I'm using: 这是我正在使用的代码:

main.js: main.js:

var data = require("self").data;
var pageMod = require("page-mod");
const {Cc,Ci} = require("chrome");

pageMod.PageMod({
    include: "*",
    contentScriptFile: data.url("copy-helper.js"),
    onAttach: function(worker) {
        worker.port.on("handleCopy", function(copyInfo) {

            var gClipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
            gClipboardHelper.copyString(copyInfo.dataToCopy);
        });
    }
});

copy-helper.js: 复制helper.js:

window.addEventListener("copyEvent", function (event) {

    self.port.emit('handleCopy', event.detail.copyInfo);

}, false);

in my apps javascript 在我的应用程序JavaScript

var event = new CustomEvent("copyEvent", {
    detail:{
        copyInfo: {dataToCopy:"my string"}
    }
});
window.dispatchEvent(event);

Hope this helps anyone else who's ran into this issue! 希望这可以帮助其他遇到此问题的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM