1
사이트를 사용자 정의 할 수있는 스크립트를 작성하고 싶습니다. 이렇게하려면 영구적 인 사전이 있어야합니다. Tampermonkey로 가능합니까?세션간에 영구 목록을 저장 하시겠습니까?
EG :
persist var mylist = {}; // loads mylist from the HD/cache if it exists, else create a new one.
사이트를 사용자 정의 할 수있는 스크립트를 작성하고 싶습니다. 이렇게하려면 영구적 인 사전이 있어야합니다. Tampermonkey로 가능합니까?세션간에 영구 목록을 저장 하시겠습니까?
EG :
persist var mylist = {}; // loads mylist from the HD/cache if it exists, else create a new one.
참조, 또한, "How/Where to store data in a Chrome Tampermonkey script?".
이 경우 GM_getValue
, GM_setValue
및 JSON 인코딩을 사용하십시오. EG :
// ==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에 대한 액세스 권한을 부여하는 것이 중요하다는 점을 지적해야합니다. – Archival