【问题标题】:Store a persistent list between sessions?在会话之间存储一个持久列表?
【发布时间】:2013-05-16 22:29:33
【问题描述】:

我想编写一个允许我自定义网站的脚本。为此,我需要有一个持久字典。 Tampermonkey 可以做到这一点吗?

EG:

persist var mylist = {}; // loads mylist from the HD/cache if it exists, else create a new one.

【问题讨论】:

    标签: javascript tampermonkey


    【解决方案1】:

    另请参阅"How/Where to store data in a Chrome Tampermonkey script?"

    为此,请使用GM_getValueGM_setValue 和 JSON 编码。例如:

    var myList = JSON.parse (GM_getValue ("myListArray",  null) )  ||  {};
    

    GM_setValue ("myListArray",  JSON.stringify (myList) );
    


    这是一个完整的工作脚本,它演示了获取、更改和存储关联数组:

    // ==UserScript==
    // @name     _Persist a list between sessions
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include  https://stackoverflow.com/questions/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
    // @grant    GM_getValue
    // @grant    GM_setValue
    // ==/UserScript==
    
    var myList      = {};
    var myListObj   = GM_getValue ("myListArray",  "");
    if (myListObj) {
        myList      = JSON.parse (myListObj);
    }
    
    //-- DEBUG: List items to console.
    console.log ("There are ", Object.keys (myList).length, " items in the list.");
    for (var myItem in myList) {
        if (myList.hasOwnProperty  &&  myList.hasOwnProperty (myItem) ) {
            console.log (myItem + ": ", myList[myItem]);
        }
    }
    
    //-- Demo buttons showing how to change the list and store the new value(s).
    $("body").prepend (
          '<div id="gmDemoCntrls">'
        +    '<button data-type="add">Add Item</button>'
        +    '<button data-type="del">Delete Item</button>'
        +    '<button data-type="sav">Save list</button>'
        + '</div>'
    );
    
    $("#gmDemoCntrls button").click ( function (zEvent) {
        var numItems    = Object.keys (myList).length;
    
        switch ( $(zEvent.target).data ("type") ) {
            case "add":
                var newKey      = "autoname_" + (numItems + 1);
                var newVal      = new Date().toString ();
                myList[newKey]  = newVal;
                console.log ("Added ", newKey, " = ", newVal);
                break;
    
            case "del":
                if (numItems) {
                    var oldKey      = Object.keys (myList)[numItems - 1];
                    delete myList[oldKey];
                    console.log ("Deleted ", oldKey);
                }
                else
                    console.log ("No items left!");
                break;
    
            case "sav":
                GM_setValue ("myListArray",  JSON.stringify (myList) );
                console.log ("Saved the list of ", numItems, " items.");
                break;
    
            default:
                alert ("Oops! script error with button handling!");
                break;
        }
    } );
    

    【讨论】:

    • 谢谢。我应该指出,授予对 GM_getValue 和 GM_setValue 的访问权限至关重要......
    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 2014-04-01
    • 2016-08-07
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多