2012-02-29 73 views
1

即時通過Mozilla提供的addon-sdk開發一個小小的Firefox插件。該插件只能在一個特定的網站上工作,它需要阻止來自該網站的js文件。我正在尋找如何阻止此類請求的小時。用火狐插件封鎖JS

希望有人知道答案

+0

阻止請求的東西,你通常會通過創建XPCOM組件實現[nsIContentPolicy]做(HTTPS ://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIContentPolicy)。這是非常不平凡的,但SDK並沒有給你任何工具。 – 2012-03-01 07:07:25

回答

1

是的,你必須手工操作。 SDK在這裏根本不會幫你什麼,但有些可能。

這是你需要做的事情。請注意,這沒有經過測試,並且不能直接使用,只是爲了讓您瞭解涉及哪些組件以及在哪裏可以找到更多資源。

const { Cc, Ci, Cm, components } = require("chrome"); 
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); 
const CategoryManager = Cc["@mozilla.org/categorymanager;1"] 
           .getService(Ci.nsICategoryManager); 

function PolicyComponent() { } 

PolicyComponent.prototype = { 
    desc:    "My nsIContentPolicy XPCOM Component", 
    classID:   components.ID("{3ffd2f60-3784-11e1-b86c-0800200c9a66}"), 
    contractID:  "@abc.def.com/policycomp;1", 
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy]), 

    shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra) { 
    if (contentLocation.spec != BLOCKED_JS) { return return Ci.nsIContentPolicy.ACCEPT; } 
    else { return Ci.nsIContentPolicy.REJECT_REQUEST; } 
    }, 
    shouldProcess: function() { 
    return CI.nsIContentPolicy.ACCEPT; 
    } 
} 

var pc = new PolicyComponent() 

// Register the Interface 
Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory(pc.uuid, pc.desc, pc.contractID, pc); 

// Add the content policy 
CategoryManager.addCategoryEntry("content-policy",pc.className,pc.contractID, true, true); // not sure you should replace (last true statement) 

看到這個職位更多: What is missing in my nsIContentPolicy Firefox/IceWeasel extension XPCOMponent implementation for the shouldLoad to be called?

而且看看這些文檔:https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#Content_Policy