Javascript 如何使firefox扩展在指定域上运行js

Javascript 如何使firefox扩展在指定域上运行js,javascript,firefox,firefox-addon,Javascript,Firefox,Firefox Addon,我以前做过一个Chrome扩展,它只在一个域上运行(比如test.com),并在该域打开后立即执行我的JS文件(比如my.JS)。我想把它移植到Firefox 如何做到这一点?我是Firefox插件开发的新手。我试着阅读Firefox文档,但无法理解。它是引导加载的插件还是覆盖加载的插件 无论如何。。。。请尝试上给出的代码 我自己做的是(例如仅在imdb.com上运行): 如果您在Firefox中使用插件SDK,那么您几乎只需要,只要域/URL与页面mod期望的匹配,它就会执行脚本。看看这一点,

我以前做过一个Chrome扩展,它只在一个域上运行(比如
test.com
),并在该域打开后立即执行我的JS文件(比如
my.JS
)。我想把它移植到Firefox


如何做到这一点?我是Firefox插件开发的新手。我试着阅读Firefox文档,但无法理解。

它是引导加载的插件还是覆盖加载的插件

无论如何。。。。请尝试上给出的代码

我自己做的是(例如仅在imdb.com上运行):


如果您在Firefox中使用插件SDK,那么您几乎只需要,只要域/URL与页面mod期望的匹配,它就会执行脚本。看看这一点,以及内容脚本如何与插件通信。

你的意思是想在从特定主机加载的任何页面上运行JS吗?Greasemonkey脚本对此很有用。在扩展中,我不确定,但我猜您必须以某种方式监听选项卡位置的更改,并从处理程序有条件地执行JS代码。
var myExtension = {
    init: function() {
        // The event can be DOMContentLoaded, pageshow, pagehide, load or unload.
        if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false);
    },
    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
        // test desired conditions and do something
        // if (doc.nodeName != "#document") return; // only documents
        // if (win != win.top) return; //only top window.
        // if (win.frameElement) return; // skip iframes/frames
        alert("page is loaded \n" +doc.location.href);
    }
}
window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed
    myExtension.init();  
},false);
let doc = aEvent.originalTarget;
if (doc.location.hostname.match(/\.imdb\.com$/)) {
    // this is the page that you want to run on
}