取消镀铬扩展程序的网站请求

时间:2016-10-31 21:09:36

标签: google-chrome google-chrome-extension request-cancelling

我试图取消谷歌浏览器对我的扩展程序所做的每个网址请求,但我无法实现它。这是代码:

// Headers.js

var tabId = parseInt(window.location.search.substring(1));

function cancel(requestDetails) {
  console.log("Canceling: " + requestDetails.url);
  return {cancel: true};
}

chrome.webRequest.onBeforeRequest.addListener(
  cancel,
  {urls: ["<all_urls>"]},
  ["blocking"]
);

window.addEventListener("load", function() {
  chrome.debugger.sendCommand({tabId:tabId}, "Network.enable");
  chrome.debugger.onEvent.addListener(onEvent);
});

window.addEventListener("unload", function() {
  chrome.debugger.detach({tabId:tabId});
});

var requests = {};

function onEvent(debuggeeId, message, params) {
  if (tabId != debuggeeId.tabId)
    return;

  if (message == "Network.requestWillBeSent") {
    var requestDiv = requests[params.requestId];
    if (!requestDiv) {
      var requestDiv = document.createElement("div");
      requests[params.requestId] = requestDiv;
      requestDiv.textContent = params.request.url;
    }

    if (params.redirectResponse)
      appendResponse(params.requestId, params.redirectResponse);

    document.getElementById("container").appendChild(requestDiv);

  } else if (message == "Network.responseReceived") {
    appendResponse(params.requestId, params.response);
  }
}

这里是 manifest.json

{
  "name": "Live HTTP headers",
  "description": "Displays the live log with the http requests headers",
  "version": "0.7",
  "permissions": [
    "debugger",
    "webRequest",
    "webRequestBlocking"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Live HTTP headers"
  },
  "manifest_version": 2
}

这里是 background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.debugger.attach({tabId:tab.id}, version,
      onAttach.bind(null, tab.id));
});

var version = "1.0";

function onAttach(tabId) {
  if (chrome.runtime.lastError) {
    alert(chrome.runtime.lastError.message);
    return;
  }

  chrome.windows.create({url: "headers.html?" + tabId, type: "popup", width: 800, height: 600});
}

这是 headers.html

<html>
        <head>
                <style>
                        body {
                          font-family: monospace;
                          word-wrap: break-word;
                        }

                        #container {
                          white-space: pre;
                        }

                        .request {
                          border-top: 1px solid black;
                          margin-bottom: 10px;
                          /*background-color:red;*/
                        }
                </style>

                <script src="headers.js">

                </script>
        </head>

        <body>
                <div id="container"></div>
        </body>
</html>

我已经查看了有关此主题的stackoverflow,但我无法解决此问题。有人可以帮我一把吗?感谢

0 个答案:

没有答案