2012-07-28 129 views
0

我想開發一個firefox擴展來記錄每個瀏覽器選項卡/窗口的所有資源加載URL。我搜索了幾個小時,但找不到將每個攔截的http請求與其始發選項卡相關聯的方法。這是我到目前爲止。firefox擴展記錄每個標籤的http請求url

Components.classes["@mozilla.org/observer-service;1"] 
    .getService(Components.interfaces.nsIObserverService) 
    .addObserver({ 
    observe: function(aSubject, aTopic, aData) { 
     if ("http-on-modify-request" == aTopic) { 
     var url = aSubject 
       .QueryInterface(Components.interfaces.nsIHttpChannel) 
       .originalURI.spec; 
     alert(url); 
     } 
    } 
}, "http-on-modify-request", false); 

我可以得到HTTP請求的網址,但我不知道有辦法將它鏈接到瀏覽器窗口/選項卡。

我通讀了MDN的文檔,但沒有提到它。 (https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads)

有什麼建議嗎?

回答

3

如果你想建立你的擴展,不僅適用於Firefox,也適用於Chrome,IE &只有1(javascript)代碼的Safari,我建議你使用Crossrider

你可以很容易地實現你正在尋找的東西。

appAPI.onRequest(function(resourceUrl, tabUrl) { 
    // Where: 
    // * resourceUrl contains the URL of the requested resource 
    // * tabUrl contains the URL of the tab requesting the resource 

    // Block the loading of js scripts 
    if (resourceUrl.match(/.*/) { 
    // Do what ever you need with the specific resource 
    // For example - save it in the extension database using appAPI.db.set() 
    } 
}); 

將進入擴展的background.js,將讓你做你想做的任何行動的加載資源的每一個: 可以使用他們的API onRequest聽所有外出的請求每個頁面/標籤。