2048 save game

This script allows to save and restore any previous state of the popular 2048 game.

  1. // ==UserScript==
  2. // @name 2048 save game
  3. // @description This script allows to save and restore any previous state of the popular 2048 game.
  4. // @namespace https://github.com/Lorentz83/
  5. // @include http://gabrielecirulli.github.io/2048/
  6. // @version 1.0
  7. // @grant none
  8. // @icon https://raw.githubusercontent.com/Lorentz83/userscripts/master/2048SaveGame/icon.png
  9. // @license GPLv2; http://www.gnu.org/licenses/
  10. // ==/UserScript==
  11.  
  12. var game = {
  13. stateBox : null,
  14. isOver : function() {
  15. var over = document.querySelector('div.game-message.game-over');
  16. return over !== null;
  17. },
  18. state : function() {
  19. var bk = window.localStorage.getItem('gameStateBK');
  20. var date = 'none';
  21. if ( bk !== null) {
  22. date = JSON.parse(bk).date;
  23. }
  24. game.stateBox.textContent = date;
  25. },
  26. save : function() {
  27. if ( game.isOver() ){
  28. alert('the game is over!');
  29. return;
  30. }
  31. var state = window.localStorage.getItem('gameState');
  32. var date = new Date();
  33. date = date.getFullYear()+'-'+date.getMonth()+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();;
  34. var bk = {
  35. date : date,
  36. state : state
  37. }
  38. window.localStorage.setItem('gameStateBK', JSON.stringify(bk));
  39. game.state();
  40. this.blur();
  41. } ,
  42. restore : function() {
  43. var bk = window.localStorage.getItem('gameStateBK');
  44. if ( bk !== null) {
  45. window.localStorage.setItem('gameState', JSON.parse(bk).state);
  46. window.location.reload();
  47. }
  48. this.blur();
  49. }
  50. }
  51.  
  52.  
  53. var gc = document.querySelector('.game-container');
  54. var saveBox = document.createElement('div');
  55. gc.parentNode.insertBefore(saveBox, gc.nextSibling);
  56.  
  57.  
  58. var saveBtn = document.createElement('input');
  59. saveBtn.type='button';
  60. saveBtn.value='save';
  61. saveBtn.addEventListener('click', game.save);
  62. saveBox.appendChild(saveBtn);
  63.  
  64. var restoreBtn = document.createElement('input');
  65. restoreBtn.type='button';
  66. restoreBtn.value='restore';
  67. restoreBtn.addEventListener('click', game.restore);
  68. saveBox.appendChild(restoreBtn);
  69.  
  70. var span = document.createElement('span');
  71. saveBox.appendChild(span);
  72. span.textContent = 'last saved: ';
  73. game.stateBox = document.createElement('span');
  74. span.appendChild(game.stateBox);
  75.  
  76. game.state();

QingJ © 2025

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