GN_PersistentStorage

GN_PersistentStorageD

Από την 20/11/2015. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        GN_PersistentStorage
// @namespace   Gradient
// @description GN_PersistentStorageD
// @include     /.+(heroeswm|178\.248\.235\.15).+/
// @exclude     /.+(heroeswm|178\.248\.235\.15).*?\/(login|war|cgame|frames|chat|chatonline|ch_box|chat_line|ticker|chatpost)\.php.*/
// @exclude     /.+daily\.heroeswm\.ru.+/
// @version     1.0.2
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// ==/UserScript==

"use strict";

//----------------------------------------------------------------------------//  

(function(){  // wrapper start
  
//----------------------------------------------------------------------------//    

//var operations = [ 'save', 'load', 'remove' ];
//var states     = [ 'complete', 'incomplete' ];
  
var div = document.createElement('div');
div.setAttribute('id',        'GN_GM_Handler');
div.setAttribute('desc',      '');
div.setAttribute('value',     '');
div.setAttribute('operation', 'unknown');
div.setAttribute('state',     'incomplete');
div.setAttribute('is_null',   'false');    
  
div.addEventListener('click', function(e){
  e.preventDefault();
  
  div.setAttribute('state', 'incomplete');
  
  var operation = div.getAttribute('operation');
  
  if(operation == 'save'){
    save_value(div.getAttribute('desc'), div.getAttribute('value'));
    div.setAttribute('value', '');
  }
  else if(operation == 'load'){
    var value = load_value(div.getAttribute('desc'));

    div.setAttribute('is_null', !value ? 'true' : 'false');
    div.setAttribute('value', value ? value : '');
  }
  else if(operation == 'remove'){
    remove_value(div.getAttribute('desc'));
    div.setAttribute('value', '');
  }
  
  div.setAttribute('desc',      '');
  div.setAttribute('operation', 'unknown');
  div.setAttribute('state',     'complete');
});

document.body.appendChild(div);
  
//----------------------------------------------------------------------------// 
  
function save_value(desc, value){
  if(check_gm_function()){
    GM_setValue(desc, value);
    return;
  }
  
  check_local_storage();
  
  try{
    localStorage.setItem(desc, value);
  }
  catch(e){
    show_error('Ошибка при сохранении значения');
  }
}
  
//----------------------------------------------------------------------------//  

function load_value(value){
  if(check_gm_function())
    return GM_getValue(value, null);

  check_local_storage();
  
  return localStorage.getItem(value);
}
  
//----------------------------------------------------------------------------//

function remove_value(value){
  if(check_gm_function()){
    GM_deleteValue(value);
    return;
  }
  
  check_local_storage();
  
  localStorage.removeItem(value);
}

//----------------------------------------------------------------------------//  

function check_local_storage(){
  if('localStorage' in window && window['localStorage'] !== null)
    return;

  show_error('Не найдено локальное хранилище');
}

//----------------------------------------------------------------------------//  

function check_gm_function(){
  return typeof GM_setValue == 'function';
}
  
//----------------------------------------------------------------------------//
  
function show_error(error_string){
  throw new Error(error_string);
}
  
//----------------------------------------------------------------------------//  
  
}()); // wrapper end

//----------------------------------------------------------------------------//