使用 tampermonkey 获取与页面相关的所有请求的 headers

Get headers of all requests related to a page with tampermonkey

我正在尝试编写一个 tampermonkey 脚本,它在字典中收集 document.location 和 header。在谷歌上搜索了一下,我想我应该使用某种全局变量,但它并没有像我想要的那样工作。

这是脚本:

// ==UserScript==
// @name         My Fancy New Userscript
// @namespace    http://your.homepage/
// @version      0.1
// @description  enter something useful
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==


if (unsafeWindow.resources == undefined) {
   var unsafeWindow.resources = [];
}

var host = window.location;
unsafeWindow.resources.push(host);
console.log(unsafeWindow.resources);

当 运行 时,我得到以下错误:

ERROR: Execution of script 'My Fancy New Userscript' failed! unsafeWindow is not defined

也许我想做的事情根本不可能?

更新: 试图更清楚一点。最终结果应该生成一个以 document.location 作为键的字典,以及一个包含 header 名称和所述位置的 header 值作为值的字典。

{document.location = {"Headername" = "Header value", "Headername" = "Header value"}}

最终结果将用于生成 table 字典中的信息。像这样:

/帕特里克

请将此作为您应该完成的事情的简短示例,并注意在继续您的项目时可能会遇到一些问题,因为有时 document.location 可能有点难以检索。

除此之外,代码:

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      *://*/*
// @copyright  2015+, You
// ==/UserScript==

var storage = (function(win){
    var localDrive = win.localStorage;

    return {
        save:  function (/* <string> */ key, /* <string | JSONstringified object> */ value) {
            localDrive.setItem(key, value);
        },
        destroy:  function (/* <string> */ key) {
            return localDrive.removeItem(key) ? true : false;   
        },
        get:    function (/* <string> */ key) {
            return localDrive.getItem(key) == '' || localDrive.getItem(key) == null ? false : localDrive.getItem(key);
        }
    }
})(window);

window.storage = storage;

document.addEventListener("DOMContentLoaded", function(e) {
    // Dom ready, start:

    // Check whether the array exists or not :
    if (!storage.get("myDataList")) {
        storage.save("myDataList", JSON.stringify(
                       [{
                           'href'     :     document.location.href,
                           'location' :     document.location,
                           'test1'    :     'test',
                           'test2'    :     'test2'
                       }]
                    )
                   );   
    }
    else {
        // If exists, log every single object: 
        var currentStorageList = JSON.parse(storage.get("myDataList"));
        for (var i = 0; i < currentStorageList.length; ++i) {
            console.log(currentStorageList[i]);
        }
    }

    // Check whether this element exists in the current list, else add :

    var currentStorageList = JSON.parse(storage.get("myDataList"));

    var elementExists = currentStorageList.some(function(el,i,arr) {
       return el.href === document.location.href;
    });

    if (!elementExists) {
        console.log("current elements doesn't exist, let's push it!");
        storage.save("myDataList", JSON.stringify(JSON.parse(storage.get("myDataList")).push({
                           'href'     :     document.location.href,
                           'location' :     document.location,
                           'test1'    :     'test',
                           'test2'    :     'test2'
                       })));
    }
});

这是纯粹的 javascript,因为我没有看到你使用 jQuery。

我在那里提供了:

  1. 一个舒适的 DOM 对象(存储),具有三个主要方法:保存(存储键 => 值,其中值必须是字符串或 json 字符串化元素,因为你不能在本地存储中存储数组),获取(从键中获取)和销毁(从键中)。
  2. domready 开头的构造函数:如果保存元素的键不存在,它将创建它并填充当前文档位置,使用文档的 href 识别它。
  3. 为了保存/检索您需要的内容,可以使用一些示例。

请注意,这只是一个示例(尽管它确实有效)。

此外,在您的 tampermonkey 脚本设置中,不要忘记在 document-end.

将其设置为 运行

xkcd 上的输出(用于测试)是这样的:

http://prntscr.com/784zw7(图片直接link)

希望这对您的项目有所帮助 ;)