pbtweetのlocalstorageコード晒し
localstorageがないときはcookie使うようにしてる。ちなみに下記コードだとFirefoxでは判定に失敗する。原因を探すのも面倒なのでGreasemonkey版は強制的にcookieに保存するようにしてる。
簡単な設定値程度なら下記コードでcookie or localstorageに保存すればいいけど、長いデータはlocalstorageに保存することになるね。
//Client side storage handler function restore_pb_values() { chain_count = get_storage_Value( 'pb_chain_count' ) ? get_storage_Value( 'pb_chain_count' ) : 4 ; pb_lang = get_storage_Value( 'pb_lang' ) ? get_storage_Value( 'pb_lang' ) : navigator.language.substr( 0 , 2 ) ; enable_accesskey = get_storage_Value( 'pb_enable_accesskey' ) ? get_storage_Value( 'pb_enable_accesskey' ) : false ; window.onbeforeunload = function () { return save_storage_Changes(); }; } function clearAll() { localStorage.clear(); sessionStorage.clear(); restore_pb_values(); } function set_storage_Value( key , value ) { if ( is_localstorage() ) { localStorage.setItem( key , value ); sessionStorage.setItem( key , value ); } else { document.cookie = key + "=" + value + "; expires = Thu, 1-Jan-2030 00:00:00 GMT;" ; } } function get_storage_Value(key) { if ( is_localstorage() ) { return( localStorage.getItem( key ) ); } else { var cookies = document.cookie.split("; "); for ( var i = 0 ; i < cookies.length ; i++ ) { var str = cookies[i].split( "=" ); if ( str[0] == key ) { return( unescape( str[1] ) ); break ; } } } } function save_storage_Changes() { set_storage_Value( 'pb_chain_count' , chain_count ); set_storage_Value( 'pb_lang' , pb_lang ); return NULL; } function clearValue( value ) { if (value == 'myfield1') { sessionStorage.removeItem( alue ); } else { localStorage.removeItem( value ); } document.getElementById(value).value = ''; } function is_localstorage(){ if (typeof(sessionStorage) == 'undefined' || typeof(localStorage) == 'undefined') { return(false); } else { return(true); } }