如何使用firefox addon sdk向gmail内容注入脚本即gmail.js api directlty?

时间:2015-04-21 05:20:54

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

在lib / Main.js文件中:

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

pageMod.PageMod({
    include: "https://mail.google.com/*",
    contentScriptWhen: 'end',
    contentScriptFile: [data.url("./inject.js")],
    contentScriptWhen: "end",
    attachTo: 'top'   //<-- add this property to only attach to top level documen
});  

在data / inject.js中:

var script1 = document.createElement('script');
script1.type = "text/javascript";
script1.src = "resource://jid1-ecwxnrqlkw2cja-at-jetpack/my-addon/data/jquery.min.js";
document.body.appendChild(script1);

var script2 = document.createElement('script');
script2.type = "text/javascript";
script2.src = "resource://jid1-ecwxnrqlkw2cja-at-jetpack/my-addon/data/gmail.js";
document.body.appendChild(script2);

var script3 = document.createElement('script');
script3.type = "text/javascript";
script3.src = "resource://jid1-ecwxnrqlkw2cja-at-jetpack/my-addon/data/addbutton.js";
document.body.appendChild(script3);
data / addbutton.js中的

$(function () {     
    $(document).on("click", ".aic",function () {
        alert("clicked");
        setInterval(function () {
            if(!$(".dw").hasClass("np"))
            {
                if($(".dw .no").eq(0).children().length > 2)
                {       
                    var latestComposeTab = $(".dw .no").eq(0).children().length - 2;
                    $(".dw .no").eq(0).children().eq(latestComposeTab).find("td.Up").children().children().eq(1).text("Secure Send");
                    $(".dw .no").eq(0).children().eq(latestComposeTab).find("td.a8X").children().children().eq(1).hide();
                    $(".dw .no").eq(0).children().eq(latestComposeTab).find("td.a8X").children().children().eq(2).hide();
                    var gmail = Gmail();

                    gmail.observe.on("upload_attachment", function(file, xhr) {
                        alert("upload attachment"+file);
                        console.log("file", file, 'xhr', xhr);
                    })

                    gmail.observe.on('view_thread', function(obj) {
                        alert("view_thread"+obj)
                        console.log('view_thread', obj);
                    });

                    // now we have access to the sub observers view_email and load_email_menu
                    gmail.observe.on('view_email', function(obj) {
                        alert("view_email"+obj)
                        console.log('view_email', obj);
                    });

                    gmail.observe.on("refresh", function(url, body, data, xhr) {
                        alert("refresh")
                        console.log("url:", url, 'body', body, 'data', data, 'xhr', xhr);
                    })

                }   
            }
        }, 100);

    });

})

https://github.com/KartikTalwar/gmail.js/tree/master

获取gmail.js个文件

在gmail.js中触发upload_Attachment观察者时发生附件失败问题。请帮助触发gmail.js中的upload_attachment observer。

1 个答案:

答案 0 :(得分:1)

使用require("sdk/self").data.load("addbutton.js")从您的数据文件中读取源代码,然后使用contentScriptOptions将该src传递给内容脚本

pageMod.PageMod({
    include: "https://mail.google.com/*",
    contentScriptWhen: 'end',
    contentScriptFile: [data.url("./inject.js")],
    contentScriptWhen: "end",
    contentScriptOptions: {
      "addbutton": require("sdk/self").data.load("addbutton.js")
    },
    attachTo: 'top'   //<-- add this property to only attach to top level documen
});

然后在inject.js中创建脚本标记,如下所示:

var script1 = document.createElement('script');
script1.type = "text/javascript";
script1.innerText = self.options.addbutton;