2010-09-21 28 views
2

我想寫一個Firefox擴展。此擴展不是一個通用的擴展,但專門爲需要突出顯示特定html組件的域而工作。限制Firefox的擴展到一個特定的域

我該怎麼做?我只想在用戶瀏覽特定域時加載js。

我現在overaly.js基本上是空的(通過擴展嚮導生成的):

var myextension = { 
    onLoad: function() { 
    // initialization code 
    this.initialized = true; 
    this.strings = document.getElementById("myextension-strings"); 
    }, 

    onMenuItemCommand: function(e) { 
    var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] 
            .getService(Components.interfaces.nsIPromptService); 
    promptService.alert(window, this.strings.getString("helloMessageTitle"), 
           this.strings.getString("helloMessage")); 
    }, 

    onToolbarButtonCommand: function(e) { 
    // just reuse the function above. you can change this, obviously! 
    myextension.onMenuItemCommand(e); 
    } 
}; 

window.addEventListener("load", myextension.onLoad, false); 

而且我ff-overlay.xul是:

myextension.onFirefoxLoad = function(event) { 
    document.getElementById("contentAreaContextMenu") 
      .addEventListener("popupshowing", function (e){ myextension.showFirefoxContextMenu(e); }, false); 
}; 

myextension.showFirefoxContextMenu = function(event) { 
    // show or hide the menuitem based on what the context menu is on 
    document.getElementById("context-myextension").hidden = gContextMenu.onImage; 
}; 

window.addEventListener("load", myextension.onFirefoxLoad, false); 

我的想法去尼安德特人和裏面做的檢查myextension.onFirefoxLoad以查看當前頁面是否是我想要的頁面,但需要用戶單擊上下文菜單上的適當項目。

回答

3

我不是完全遵循你所擁有的,因爲這兩個看起來像JS文件,而不是XUL文件。但你可能想要做的是listen for the load event coming from the web pages that are loaded。然後,在您的事件加載器中,只需查看加載的每個頁面,看看它是否來自您想要的特定域。

一個偉大的(儘管並不總是那麼簡單,因爲它聽起來)的方式來找出如何在Firefox的插件做一些事情是找到另一個插件做類似的事情。 DOM InspectorInspect Context是你的朋友!在這種情況下想到的第一個這樣的插件是WikiTrust,所以你可以試着看那個,看看它是否給你任何啓發。

+0

這是'onPageLoad'我正在尋找。我有第二個問題與此有關。由於我不需要編輯瀏覽器UI,我是否仍然需要'overlay.xul'?在這一點上,它應該是一個默認的'overlay.xul',只有'script'標籤來調用擴展js文件? – dierre 2010-09-23 11:52:25

+0

是的,我想是的。只需包含您的腳本標籤,並且可能需要一個字符串來將字符串外部化。然後,如果您稍後決定添加其他內容,則始終可以這樣做。 – MatrixFrog 2010-09-23 18:17:23

相關問題