在Firefox中从网页访问扩展程序的功能

时间:2015-05-18 16:15:36

标签: javascript firefox firefox-addon firefox-addon-sdk


我试图写一个Firefox扩展,允许我从我的网页访问它的内部函数/类/对象。我希望它们在DOM中可见并且可访问。当扩展作为chrome.manifest文件中的一个组件加载时它起作用,但它似乎不再适用于e10s(多进程Firefox)。
所以我在尝试和尝试,到目前为止我找到的最佳选项似乎是使用exportFunction,createObjectIn和cloneInto函数。当预期从扩展本身加载的页面而不是从远程加载的页面中看到对象时,它们可以正常工作 我现在正在使用Addon-SDK,我的代码是

然后

function injectTest(event) {
    let domWindow = event.subject;

//This creates and object that is always visible but never accesible from page not loaded by the extension     
foo = Cu.createObjectIn(domWindow.wrappedJSObject, {defineAs: "testSDK"});     
//This exports my function fine but I can export it only into an existing object
//That's why I'm using "crypto" here
Cu.exportFunction(test.bind(this, domWindow),
                    domWindow.crypto.wrappedJSObject,
                    { defineAs: "test" });


//This exports my function to my object but only on pages loaded by the extension    
Cu.exportFunction(test.bind(this, domWindow),
                    foo,
                    { defineAs: "test2" });

//Same here, cloned_var seems to be not accesible from remote webpage
var to_be_cloned = {"greet" : "hey"};
    foo.cloned_var = Cu.cloneInto(to_be_cloned, foo);
}



  exports.main = function(options, callbacks) {
      if (!gInitialized &&
          (options.loadReason == "startup" ||
           options.loadReason == "install" ||
           options.loadReason == "enable")) {
        log("initializing - " +  options.loadReason);
        try {
          events.on("content-document-global-created", injectTest);
        } catch (error) {
          log(error);
        }
        gInitialized = true;
      }
    };

我对javascript和Firefox扩展程序全新,所以我不知道如何让它工作。我做错了什么?有没有更好的主意来访问扩展程序的对象?

提前感谢您的帮助。

@edit 19.05.15 尝试使用page-mod。它确实有效,但不如我所需。 main.js文件

var data = require("sdk/self").data; 
var pageMod = require("sdk/page-mod"); 

pageMod.PageMod({
  include: "mywebsite",
    contentScriptFile: [data.url("cscript.js")],
    contentScript: 'window.alert("Page matches ruleset");'
}); 

cscript.js文件(在数据文件夹中)

var contentScriptObject = {
    "greeting" : "hello from add-on",
    b: 1,

    powitaj: function(){
    return(this.greeting);
    }, //when called from console returns "hello from add-on"


    is_b: function(){
        if(b){
        return true;
        }else{
        return false;
        }
    }, //undefined

    is_thisb: function(){
        if(this.b){
        return true;
        }else{
        return false;
        }
    }, //returns 1

    func: function(){
    console.log("ok")
    }, //returns "ok"

    is_func: function(){
        func();         
    }, //undefined

    is_thisfunc:function(){
        this.func();
    } //undefined

};

所以从我的网站我可以访问内部变量(实际上也是全局定义的变量并且也可以修改它们),我可以访问内部函数(不是外部 - 不包含在代码中)但是我的内部函数不能相互调用我和#39;我希望能够做到这一点。

1 个答案:

答案 0 :(得分:0)

要在e10s内容流程中运行镀铬特权代码,您需要framescripts

SDK有一些包装模块,可以简化parentchild进程之间的通信和模块加载

如果您只需要exportFunction / cloneInto / createObjectIn,那么使用page-mod模块可能就足够了,如果我没记错的话,这些帮助程序在其上下文中可用。

相关问题