Skip to content

Instantly share code, notes, and snippets.

@esperia
Created June 15, 2011 18:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esperia/1027730 to your computer and use it in GitHub Desktop.
Save esperia/1027730 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name test
// @author esperia
// @namespace http://d.hatena.ne.jp/esperia/
// @description テスト用のuserjs
// @include http://localhost:8080/
// @version 0.01
// ==/UserScript==
//
(function(_doc){
GM_XBrowse();
GM_setValue("hoge", "fuga");
alert(GM_getValue("hoge"));
GM_setValue("foo", "bar");
alert(GM_listValues());
GM_deleteValue("hoge");
alert(GM_listValues());
GM_deleteValue("foo");
alert(GM_listValues());
})(document);
/**
* 各関数が実装されていない場合、localStorageで代用する
*/
function GM_XBrowse() {
var gmFuncs = {};
// functions ----
gmFuncs.GM_setValue = function (key,value) {
return localStorage.setItem(key, value);
};
gmFuncs.GM_getValue = function (key) {
return localStorage.getItem(key);
};
gmFuncs.GM_deleteValue = function (key) {
return localStorage.removeItem(key);
};
gmFuncs.GM_addStyle = function (doc, css) {
var head, style;
head = document.getElementsByTagName("head")[0];
if (!head) { return; }
style = document.createElement("style");
style.type = "text/css";
style.innerHTML = css;
head.appendChild(style);
};
gmFuncs.GM_listValues = function () {
var list = [];
for(var i=0, len=localStorage.length; i<len; i++) {
list.push(localStorage.key(i));
}
return list;
};
// set ----
if(typeof GM_setValue == 'function') {
try {
if(GM_setValue.toString().indexOf("not supported") > -1) {
GM_setValue = gmFuncs.GM_setValue;
}
} catch(e) {}
} else {
GM_setValue = gmFuncs.GM_setValue;
}
if(typeof GM_getValue == 'function') {
try {
if(GM_getValue.toString().indexOf("not supported") > -1) {
GM_getValue = gmFuncs.GM_getValue;
}
} catch(e) {}
} else {
GM_getValue = gmFuncs.GM_getValue;
}
if(typeof GM_deleteValue == 'undefined') {
GM_deleteValue = gmFuncs.GM_deleteValue;
}
if(typeof GM_listValues == 'undefined') {
GM_listValues = gmFuncs.GM_listValues;
}
if(typeof GM_addStyle == 'undefined') {
GM_addStyle = gmFuncs.GM_addStyle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment