繁体   English   中英

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

[英]Access extension's functions from webpage in Firefox


我正在尝试为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

};

因此,从我的网站上,我可以访问内部变量(实际上也是全局定义的变量,也可以对其进行修改),我可以访问内部函数(不是外部函数-不包括在代码中),但是内部函数不能互相调用,我想能够做到这一点。

要在e10s内容流程中运行chrome特权代码,您需要使用框架脚本

该SDK有一些包装模块,以简化之间的通信和模块加载的父过程

如果只需要exportFunction / cloneInto / createObjectIn那么使用page-mod模块就足够了,如果我正确地记得的话,可以在其上下文中使用这些帮助器。

暂无
暂无

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

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