2017-12-24 1 views
0

사이트에 대한 HTTP 응답을 얻을 수있는 Chrome 확장 프로그램을 작성 중입니다. 나는 응답 본문 얻기 위해 디버거를 사용하려고하지 :Chrome 확장 프로그램 : Network.getResponseBody를 시도 할 때 '제공된 식별자가있는 리소스가 없습니다.'

 
var gCurrentTab; 

chrome.debugger.onEvent.addListener(function (source, method, params) { 
     if (gCurrentTab.id != source.tabId) { 
      return; 
     } 
     if (method == "Network.loadingFinished") { 
      var tabId = source.tabId; 
      var requestId = params.requestId; 
      chrome.debugger.sendCommand(
       source, 
       "Network.getResponseBody", 
       {"requestId": requestId}, 
       function (body) { 
        console.log(body); 
        chrome.debugger.detach(source); 
       }); 
     } 
    } 
); 

chrome.webRequest.onBeforeRequest.addListener(function (details) { 
     var url = details.url; 
     if (url.indexOf('/mypage') >= 0) { 
      chrome.tabs.query({ 
       currentWindow: true, 
       active: true 
      }, function (tabs) { 
       gCurrentTab = tabs[0]; 
       chrome.debugger.attach({ 
        tabId: gCurrentTab.id 
       }, "1.0", function() { 
        chrome.debugger.sendCommand({ 
         tabId: gCurrentTab.id 
        }, "Network.enable"); 
       }); 
      }); 
     } 
    }, 
    {urls: []}, ["requestBody", "blocking"]); 

을하지만 debugger.sendCommand을 실행하는 동안 난 항상

체크되지 runtime.lastError를 얻을 : { "코드"- 32000, "메시지": "어떤 자원을 주어진 식별자가있는 "} 의 chrome-extension : //ikphgobkghdkjkfplgokmapjlbdfeegl/background.js : 11 : 29

오류이며 본문은 정의되지 않았습니다.

왜 이런 일이 일어나는 지 아는 사람이 있습니까? 감사!

답변

0

웹 사이트에서 많은 응답을 전송했기 때문에이 코드는 내가 원하는 것 외의 다른 요청을 보게 될 것이므로 디버거를 분리하여 결과를 얻을 수 없기 때문입니다.

이 문제를 해결하려면 단일 디버거를 사용하고 분리하지 마십시오. 안전 할 때만 분리하십시오.

 
var gAttached = false; 
var gRequests = []; 
var gObjects = []; 

chrome.debugger.onEvent.addListener(function (source, method, params) { 
     if (method == "Network.requestWillBeSent") { 
      // If we see a url need to be handled, push it into index queue 
      var rUrl = params.request.url; 
      if (getTarget(rUrl) >= 0) { 
       gRequests.push(rUrl); 
      } 
     } 
     if (method == "Network.responseReceived") { 
      // We get its request id here, write it down to object queue 
      var eUrl = params.response.url; 
      var target = getTarget(eUrl); 
      if (target >= 0) { 
       gObjects.push({ 
        requestId: params.requestId, 
        target: target, 
        url: eUrl 
       }); 
      } 
     } 
     if (method == "Network.loadingFinished" && gObjects.length > 0) { 
      // Pop out the request object from both object queue and request queue 
      var requestId = params.requestId; 
      var object = null; 
      for (var o in gObjects) { 
       if (requestId == gObjects[o].requestId) { 
        object = gObjects.splice(o, 1)[0]; 
        break; 
       } 
      } 
      // Usually loadingFinished will be immediately after responseReceived 
      if (object == null) { 
       console.log('Failed!!'); 
       return; 
      } 
      gRequests.splice(gRequests.indexOf(object.url), 1); 
      chrome.debugger.sendCommand(
       source, 
       "Network.getResponseBody", 
       {"requestId": requestId}, 
       function (response) { 
        if (response) { 
         dispatch(source.tabId, object.target, JSON.parse(response.body)); 
        } else { 
         console.log("Empty response for " + object.url); 
        } 
        // If we don't have any request waiting for response, re-attach debugger 
        // since without this step it will lead to memory leak. 
        if (gRequests.length == 0) { 
         chrome.debugger.detach({ 
          tabId: source.tabId 
         }, function() { 
          chrome.debugger.attach({ 
           tabId: source.tabId 
          }, "1.0", function() { 
           chrome.debugger.sendCommand({ 
            tabId: source.tabId 
           }, "Network.enable"); 
          }); 
         }); 
        } 
       }); 
     } 
    } 
); 

var initialListener = function (details) { 
    if (gAttached) return; // Only need once at the very first request, so block all following requests 
    var tabId = details.tabId; 
    if (tabId > 0) { 
     gAttached = true; 
     chrome.debugger.attach({ 
      tabId: tabId 
     }, "1.0", function() { 
      chrome.debugger.sendCommand({ 
       tabId: tabId 
      }, "Network.enable"); 
     }); 
     // Remove self since the debugger is attached already 
     chrome.webRequest.onBeforeRequest.removeListener(initialListener); 
    } 
}; 

// Attach debugger on startup 
chrome.webRequest.onBeforeRequest.addListener(initialListener, {urls: [""]}, ["blocking"]); 

// Filter if the url is what we want 
function getTarget(url) { 
    for (var i in TARGETS) { 
     var target = TARGETS[i]; 
     if (url.match(target.url)) { 
      return i; 
     } 
    } 
    return -1; 
} 

const TARGETS = [ 
    {url: '/path1', desc: 'target1'}, 
    {url: '/path2', desc: 'target2'} 
] 
+0

이 작업을 수행 할 수 있습니까? 동일한 문제가 있지만 문제를 해결하는 방법을 찾을 수 없습니다. – cen0r

+0

여기 있습니다. 요점은 올바른 요청 ID와 동일한 디버거를 사용해야한다는 것입니다. –

0

비슷한 문제가 있습니다. sendCommand가 즉시 실행되지 않는다고 생각했습니다. 나는 "Network.enable"을 보내기 전에 보내지는 요청에 대한 문제를 직면했다. 완료 추가를 시도하십시오

chrome.debugger.sendCommand({ 
         tabId: gCurrentTab.id 
        }, "Network.enable") 
관련 문제