2014-10-02 2 views
0

...크롬 확장에서 onBeforeRequest 이벤트 리스너를 업데이트하려면 어떻게해야합니까? 나는 배경 페이지에 다음과 같은 이벤트 리스너를 통해 확장이

chrome.webRequest.onBeforeRequest.addListener(
    function(details){ 
     if (... details.url is in ArrayOfURLs ... ){ 
      ... do stuff ... 
     } 
    }, { 
     urls: ["<all_urls>"], 
     types: ["main_frame"] 
    }, ["blocking"] 
); 

내가 뭘 원하는 것은 처음에 이벤트 리스너에 URL의 배열을 전달합니다. 그래서

chrome.webRequest.onBeforeRequest.addListener(
    function(details){ 
     ... do stuff ... 
    }, { 
     urls: ArrayOfURLs, 
     types: ["main_frame"] 
    }, ["blocking"] 
); 

내가 ArrayOfURLs 배열에서 항목을 추가/제거 할 때 내가 이벤트 리스너를 업데이트 할 수있는 방법이 있나요 ... 같이해야합니까?

+0

규칙에 대한 섹션과 동적 할당에 관심이있을 것이라고 생각합니다. https://developer.chrome.com/extensions/events – gpgekko

+0

@gpgekko 규칙 시스템에서 호출 할 수있는 작업의 수는 극히 제한되어 있습니다. 필요한 액션을 제공하는'declarativeWebRequest' API는 안정적이지 않고 사용할 수있는 타임 라인이 없습니다. – Xan

답변

1

일반적인 방법은 이전 처리기를 제거하고 새 처리기를 추가하는 것입니다.

function requestHandler(details){ 
    /* ... do stuff ... */ 
} 

function setListener(ArrayOfURLs) { 
    chrome.webRequest.onBeforeRequest.removeListener(requestHandler); 
    chrome.webRequest.onBeforeRequest.addListener(
    requestHandler, 
    { urls: ArrayOfURLs, types: ["main_frame"] }, 
    ["blocking"] 
); 
    chrome.webRequest.handlerBehaviorChanged(); 
} 

는 마지막 전화가 크롬이 규칙을 캐시하기 때문에 필요하지만 very expensive in terms of performance 야한다.

+0

대단한데, 자주 전화를 걸지 않으므로 괜찮을 것입니다. 감사 –

관련 문제