Chrome Extension: "No resource with given identifier found" when trying to Network.getResponseBody
10:24 24 Dec 2017

I'm writing a Chrome Extension that can get HTTP response for a site. I try to use debugger for getting response body:

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"]);

But I always get

Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32000,"message":"No resource with given identifier found"} at chrome-extension://ikphgobkghdkjkfplgokmapjlbdfeegl/background.js:11:29

error, and the body is undefined.

Does anyone have idea about why this happen? Thanks!

javascript google-chrome google-chrome-extension