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

时间:2015-05-22 14:58:26

标签: javascript tampermonkey

我试图编写一个tampermonkey脚本,它收集字典中的document.location和标题。谷歌搜索了一下,并认为我应该使用某种全局变量,但它没有按我的意愿工作。

这是脚本:

// ==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);

运行时我收到以下错误:

  

错误:执行脚本'我喜欢的新用户'失败! unsafeWindow未定义

也许我想做的事情甚至不可能?

更新: 试着更清楚一点。最终结果应该产生一个字典,该字典将document.location作为键,并且字典包含所述位置的标题名称和标题值作为值。

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

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

enter image description here

/帕特里克

1 个答案:

答案 0 :(得分:1)

请将此作为您应该完成的内容的简要示例,并注意在继续执行项目时可能会遇到一些问题,因为有时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对象(存储),有三个主要方法:save(存储key =&gt; value,其中value必须是字符串或json字符串元素,因为你不能将数组存储在本地存储中), get(从key获取)并销毁(从key)。
  2. domready开头的构造函数:如果持有元素的键不存在,它将创建它并填充当前文档位置,使用文档的href识别它。
  3. 使用一些示例来保存/检索您需要的内容。
  4. 请注意,这只是一个例子(尽管如此)。

    另外,在您的tampermonkey脚本设置中,不要忘记将其设置为在 document-end 上运行。

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

    enter image description here

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

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