GN_PersistentStorage

Операции с БД

  1. // ==UserScript==
  2. // @name GN_PersistentStorage
  3. // @namespace Gradient
  4. // @description Операции с БД
  5. // @include /^https{0,1}:\/\/(www\.heroeswm\.ru|178\.248\.235\.15|my\.lordswm\.com)\/.+/
  6. // @exclude /^https{0,1}:\/\/(www\.heroeswm\.ru|178\.248\.235\.15|my\.lordswm\.com)\/(login|war|cgame|frames|chat|chatonline|ch_box|chat_line|ticker|chatpost|chat2020|battlechat|campaign)\.php.*/
  7. // @version 1.0.5
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_deleteValue
  11. // ==/UserScript==
  12.  
  13. "use strict";
  14.  
  15. //----------------------------------------------------------------------------//
  16.  
  17. var script_name = 'GN_PersistentStorage'; // Enter your script name here
  18.  
  19. //----------------------------------------------------------------------------//
  20.  
  21. (function(){ try{ // wrapper start
  22.  
  23. //----------------------------------------------------------------------------//
  24.  
  25. //var operations = [ 'save', 'load', 'remove' ];
  26. //var states = [ 'complete', 'incomplete' ];
  27.  
  28. var div = document.createElement('div');
  29. div.id = 'GN_GM_Handler';
  30. div.setAttribute('desc', '');
  31. div.setAttribute('value', '');
  32. div.setAttribute('operation', 'unknown');
  33. div.setAttribute('state', 'incomplete');
  34. div.setAttribute('is_null', 'false');
  35.  
  36. div.addEventListener('click', function(e){
  37. e.preventDefault();
  38.  
  39. div.setAttribute('state', 'incomplete');
  40.  
  41. var operation = div.getAttribute('operation');
  42.  
  43. if(operation == 'save'){
  44. save_value(div.getAttribute('desc'), div.getAttribute('value'));
  45. div.setAttribute('value', '');
  46. }
  47. else if(operation == 'load'){
  48. var value = load_value(div.getAttribute('desc'));
  49.  
  50. div.setAttribute('is_null', !value ? 'true' : 'false');
  51. div.setAttribute('value', value ? value : '');
  52. }
  53. else if(operation == 'remove'){
  54. remove_value(div.getAttribute('desc'));
  55. div.setAttribute('value', '');
  56. }
  57.  
  58. div.setAttribute('desc', '');
  59. div.setAttribute('operation', 'unknown');
  60. div.setAttribute('state', 'complete');
  61. });
  62.  
  63. document.body.appendChild(div);
  64.  
  65. //----------------------------------------------------------------------------//
  66.  
  67. function save_value(desc, value){
  68. if(check_gm_function()){
  69. GM_setValue(desc, value);
  70. return;
  71. }
  72.  
  73. check_local_storage();
  74.  
  75. try{
  76. localStorage.setItem(desc, value);
  77. }
  78. catch(e){
  79. show_error('Ошибка при сохранении значения');
  80. }
  81. }
  82.  
  83. //----------------------------------------------------------------------------//
  84.  
  85. function load_value(value){
  86. if(check_gm_function())
  87. return GM_getValue(value, null);
  88.  
  89. check_local_storage();
  90.  
  91. return localStorage.getItem(value);
  92. }
  93.  
  94. //----------------------------------------------------------------------------//
  95.  
  96. function remove_value(value){
  97. if(check_gm_function()){
  98. GM_deleteValue(value);
  99. return;
  100. }
  101.  
  102. check_local_storage();
  103.  
  104. localStorage.removeItem(value);
  105. }
  106.  
  107. //----------------------------------------------------------------------------//
  108.  
  109. function check_local_storage(){
  110. if('localStorage' in window && window['localStorage'] !== null)
  111. return;
  112.  
  113. show_error('Не найдено локальное хранилище');
  114. }
  115.  
  116. //----------------------------------------------------------------------------//
  117.  
  118. function check_gm_function(){
  119. return typeof GM_setValue == 'function';
  120. }
  121.  
  122. //----------------------------------------------------------------------------//
  123.  
  124. function show_error(error_string){
  125. throw new Error(error_string);
  126. }
  127.  
  128. //----------------------------------------------------------------------------//
  129.  
  130. } catch(e){
  131. alert('Ошибка в скрипте ' + script_name + ', обратитесь к разработчику:\n' + e);
  132. throw e;
  133. }}()); // wrapper end
  134.  
  135. //----------------------------------------------------------------------------//

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址