Chrome扩展程序 - 从所有标签收集数据

时间:2018-01-09 09:59:43

标签: javascript google-chrome google-chrome-extension

我写了一个Chrome扩展程序,用于从/向特定服务器收集网络请求。

The Manifest:

"manifest_version": 2,

  "name": "title",
  "description": "some text",
  "version": "0.1337",

  "permissions": [
    "activeTab",
    "webRequest",
    "tabs",
    "<all_urls>"
  ],

  "background": {
    "scripts": ["js/background.js"]
  },

  "browser_action": {
    "default_icon": "icons/icon.png",
    "default_title": "title"
  }
}

backgroudnd.js

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.windows.create({url: "headers.html?" + tab.id, type: "popup", width: 1800, height: 600});
  function moveWin() {
      myWindow.moveTo(500, 100);                                  // Doesnt Work
      myWindow.focus();                                           // Doesnt Work
  }
});

headers.js

var tabId = parseInt(window.location.search.substring(1));
var filters = {urls: ["*://www.specific-page.de/*"], tabId: tabId };

function addListeners() {
    chrome.webRequest.onBeforeRequest.addListener(handleEvent, filters, ['requestBody']);
    chrome.webRequest.onSendHeaders.addListener(handleEvent, filters, ['requestHeaders']);
    chrome.webRequest.onBeforeRedirect.addListener(handleEvent, filters, ['responseHeaders']);
    chrome.webRequest.onCompleted.addListener(handleEvent, filters, ['responseHeaders']);
    chrome.webRequest.onErrorOccurred.addListener(handleEvent, filters);
}

function removeListeners() {
    chrome.webRequest.onBeforeRequest.removeListener(handleEvent);
    chrome.webRequest.onSendHeaders.removeListener(handleEvent);
    chrome.webRequest.onBeforeRedirect.removeListener(handleEvent);
    chrome.webRequest.onCompleted.removeListener(handleEvent);
    chrome.webRequest.onErrorOccurred.removeListener(handleEvent);
}

function handleEvent(details) {
    var addressDiv = $('div.address[id="req-' + details.requestId + '"]');
    var url_stringpk = details.url;
    var url = new URL(url_stringpk);
    var encode = url.searchParams.get("d");
    var decode = atob(encode);
    var ts = new Date();


    if (addressDiv.length === 0) {
      addressDiv = $('<div>').addClass("address").attr("id", "req-" + details.requestId);
      $("#container").append(addressDiv);
      $('<h2>').addClass("url").text(ts.toLocaleTimeString()).appendTo(addressDiv);
      $('<div>').addClass("decode").text(decode).appendTo(addressDiv);
      $('<div>').addClass("url").text(encode).appendTo(addressDiv);
    }

}


function formatPost(postData) {
    var text = "";
    for (var name in postData) {
        text += name + ": " + postData[name] + "\n";
    }
    var div = $('<div>').addClass("post").text(text);
    return div;
}

function formatHeaders(headers) {
    var text = "";
    for (var i in headers) {
        text += headers[i].name + ": " + headers[i].value + "\n";
    }
    var div = $('<div>').addClass("headers").text(text);
    return div;
}

我的问题:在打开扩展程序之前,我必须导航到我要检查的页面。

我的愿望:打开扩展程序,让它听取所有标签中的所有拟合请求。

我在https://developer.chrome.com/extensions/devtools的研究没有解决方案。

0 个答案:

没有答案