相当于Firefox扩展中的内容脚本?

时间:2012-02-28 16:29:20

标签: javascript firefox google-chrome google-chrome-extension firefox-addon

我对Firefox及其扩展很新。有没有类似于使用JavaScript的脚本,每次加载页面时都会运行?我的chrome扩展插入了一个css文件,使用js创建了一个链接标记。如何将其移植到Firefox?

3 个答案:

答案 0 :(得分:7)

您需要pageMod API。

var pageMod = require("page-mod");
pageMod.PageMod({
  include: "*.example.org",
  contentScript: 'window.alert("Page matches ruleset");'
});

答案 1 :(得分:0)

每次mozIJSSubScriptLoader时,您都可以使用webpage loads执行JavaScript (chrome://myExtension/content/script.js) 。以下是您需要根据chrome目录修改的示例代码

window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed
    myExtension.init();  
},false);

var myExtension = {
  init: function() {
    var appcontent = document.getElementById("appcontent");   // browser
    if(appcontent){
      appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
    }
    var messagepane = document.getElementById("messagepane"); // mail
    if(messagepane){
      messagepane.addEventListener("load", function(event) { myExtension.onPageLoad(event); }, true);
    }
  },

  onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
    // do something with the loaded page.
    // doc.location is a Location object (see below for a link).
    // You can use it to make your code executed on certain pages only.
    var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
              loader.loadSubScript("chrome://myExtension/content/script.js", doc); 

    // add event listener for page unload 
    aEvent.originalTarget.defaultView.addEventListener("unload", function(event){ myExtension.onPageUnload(event); }, true);
  },

  onPageUnload: function(aEvent) {
    // do something
  }
};

答案 2 :(得分:0)

我以前在Firefox上使用Greasemonkey。事实上,Chrome扩展程序最初是作为Greasemonkey的模仿,并且是内容脚本的起源。