0

我正在编写将更改浏览器中谷歌布局的扩展程序。当浏览器显示 google.com 时,我的脚本使用外部 css 文件。在我打开新标签之前它工作正常 - css 被清除。如何仅将我的 CSS 匹配到谷歌搜索页面?

window.addEventListener("load", function() { 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 patt_g=new RegExp('google.com','g');
    Firebug.Console.log('Hide my ass started');
    this.doc = aEvent.originalTarget; // doc is document that triggered "onload" event

    CSSProvider.init();
   if(patt_g.test(this.doc.location.href)) {
        Firebug.Console.log('Hide my ass -> in');
        CSSProvider.loadCSS();
    }  else {
        Firebug.Console.log('Hide my ass -> out');
        CSSProvider.unloadCSS();
    }

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

  onPageUnload: function(aEvent) {
  Firebug.Console.log('Hide my ass deleted');
    if(patt_g.test(this.doc.location.href) ) {
        Firebug.Console.log('Hide my ass -> out');
        CSSProvider.unloadCSS();
    }
  }

};


var CSSProvider = {
    init: function(){
        this.sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
                    .getService(Components.interfaces.nsIStyleSheetService);
        this.ios = Components.classes["@mozilla.org/network/io-service;1"]
                        .getService(Components.interfaces.nsIIOService);
        this.uri = this.ios.newURI("chrome://hms/content/style.css", null, null);
        this.isRegistered = this.sss.sheetRegistered(this.uri, this.sss.USER_SHEET)
    },
    loadCSS: function(){
        if(!this.isRegistered){
            this.sss.loadAndRegisterSheet(this.uri, this.sss.USER_SHEET);
        }
    },
    unloadCSS: function(){
        if(this.isRegistered){
            this.sss.unregisterSheet(this.uri, this.sss.USER_SHEET);
        }
    }
};
4

1 回答 1

0

不要在每次打开选项卡时加载/卸载 CSS 文件 - 添加用户样式表将始终将其应用于所有现有选项卡,卸载它会将其从所有选项卡中删除。只需在扩展加载时添加样式表,并将样式表中的 CSS 规则放入一个@-moz-document部分:

@-moz-document domain(google.com)
{
  ...
}

文档:https ://developer.mozilla.org/en/CSS/@-moz-document

于 2012-01-05T20:11:19.007 回答