2012-05-28 2 views
3

나는 NsIContentPolicy을 읽고 NsIContentPolicy를 구현하기위한 적절한 자습서 전체 Stackoverflow를 검색했지만 모두 헛된 것입니다. Adblock이 NsIContentPolicy를 주요 무기로 사용한다는 것을 알고 있습니다. 리버스 엔지니어링 Adblock은 NsIContentPolicy를 구현하는 방법을 이해하는 데 도움이되지 않았습니다. 학습을 위해 NsIContentPolicy를 사용하는 단순한 애드온이나 NsIContentPolicy의 유용한 튜토리얼이 있습니까?firefox addon에 대한 nsIContentPolicy의 예는 무엇입니까?

답변

9

나는 어떤 좋은 튜토리얼을 인식하지 오전하지만 난 당신에게 최소한의 예제 코드를 제공 할 수 있습니다 :

Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 
Cu.import("resource://gre/modules/Services.jsm"); 

let policy = 
{ 
    classDescription: "Test content policy", 
    classID: Components.ID("{12345678-1234-1234-1234-123456789abc}"), 
    contractID: "@adblockplus.org/test-policy;1", 
    xpcom_categories: ["content-policy"], 

    init: function() 
    { 
    let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 
    registrar.registerFactory(this.classID, this.classDescription, this.contractID, this); 

    let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); 
    for each (let category in this.xpcom_categories) 
     catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true); 

    onShutdown.add((function() 
    { 
     for each (let category in this.xpcom_categories) 
     catMan.deleteCategoryEntry(category, this.contractID, false); 

     // This needs to run asynchronously, see bug 753687 
     Services.tm.currentThread.dispatch(function() 
     { 
     registrar.unregisterFactory(this.classID, this); 
     }.bind(this), Ci.nsIEventTarget.DISPATCH_NORMAL); 
    }).bind(this)); 
    }, 

    // nsIContentPolicy interface implementation 
    shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) 
    { 
    dump("shouldLoad: " + contentType + " " + 
          (contentLocation ? contentLocation.spec : "null") + " " + 
          (requestOrigin ? requestOrigin.spec : "null") + " " + 
          node + " " + 
          mimeTypeGuess + "\n"); 
    return Ci.nsIContentPolicy.ACCEPT; 
    }, 

    shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) 
    { 
    dump("shouldProcess: " + contentType + " " + 
          (contentLocation ? contentLocation.spec : "null") + " " + 
          (requestOrigin ? requestOrigin.spec : "null") + " " + 
          node + " " + 
          mimeTypeGuess + "\n"); 
    return Ci.nsIContentPolicy.ACCEPT; 
    }, 

    // nsIFactory interface implementation 
    createInstance: function(outer, iid) 
    { 
    if (outer) 
     throw Cr.NS_ERROR_NO_AGGREGATION; 
    return this.QueryInterface(iid); 
    }, 

    // nsISupports interface implementation 
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIFactory]) 
}; 

policy.init(); 

이 내가 콘텐츠 정책 구현에 문제를보고 사용하는 최소한의 콘텐츠 정책의 구현에서 온다 -을 모든 콘텐츠 정책 호출을 콘솔에 덤프하는 것 외에는 아무 것도하지 않습니다 (window.dump documentation). 실제 구현에서 필드 classDescription, classIDcontractID은 분명히 적절한 것으로 변경되어야합니다. onShutdown은 내가 사용하고있는 개인 프레임 워크에 속합니다.이 확장 프로그램은 "수동으로"구성 요소를 등록해야하는 이유 때문에이 확장 프로그램을 다시 시작하지 않으며 브라우저 세션 중에 종료되면이 코드를 실행하여 제거합니다.

전체 내선 번호 (testpolicy.xpi)를 다운로드 할 수도 있습니다.

+0

블라디미르는, 그 testpolicy.xpi 내가가 :) 감사 찾던 정확히 많은 : –

+0

예를 들어 링크가 깨진이, 수입은 여전히,이 블록 만 네트워크 콘텐츠 및 방법 변경에 대한 업데이트,는하지만, 될 필요가 돔 나무? – msangel

+0

@msangel : 링크를 업데이트했습니다. 여기 코드는 간단한 부트 스트랩 된 확장의 예제 일뿐입니다. 확장이 SDK 기반 인 경우 조정할 수 있으므로 자유롭게 조정할 수 있지만 게시물을 편집하지 마십시오. 문제는 네트워크 요청을 차단하는 데 유용한 콘텐츠 정책에 대한 것이지만 DOM 수정은 다른 방법으로 이루어져야합니다. –

관련 문제