Wordle save file helper

Allows to you export and import your Wordle save data

当前为 2022-01-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wordle save file helper
  3. // @namespace il0x89mvb33pzor0fqyj
  4. // @description Allows to you export and import your Wordle save data
  5. // @match https://www.powerlanguage.co.uk/wordle/
  6. // @grant GM.registerMenuCommand
  7. // @grant GM.setClipboard
  8. // @grant GM.getValue
  9. // @grant GM.setValue
  10. // @grant GM.deleteValue
  11. // @version 1.0
  12. // @run-at document-start
  13. // @inject-into content
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. "use strict";
  19.  
  20.  
  21. function getLocalStorageAsString() {
  22. const result = Object.create(null);
  23.  
  24. for (let i = 0, l = localStorage.length; i < l; ++i) {
  25. const key = localStorage.key(i);
  26. result[key] = localStorage.getItem(key);
  27. }
  28.  
  29. return JSON.stringify(result);
  30. }
  31.  
  32.  
  33. function importLocalStorageFromString(data) {
  34. try {
  35. data = JSON.parse(data);
  36. } catch (e) {
  37. return false;
  38. }
  39.  
  40. for (const [k, v] of Object.entries(data)) {
  41. localStorage.setItem(k, v);
  42. }
  43. return true;
  44. }
  45.  
  46.  
  47. async function autoSave() {
  48. const savedata = await GM.getValue("autosave");
  49.  
  50. if (savedata) {
  51. const playedSave = JSON.parse(JSON.parse(savedata).statistics).gamesPlayed;
  52. const playedActual = JSON.parse(localStorage.getItem("statistics")).gamesPlayed;
  53. if (playedActual < playedSave) {
  54. return;
  55. }
  56. }
  57.  
  58. // No save data, or number of games is equal/higher
  59. GM.setValue("autosave", getLocalStorageAsString());
  60. }
  61.  
  62.  
  63. // Save every minute while the tab is active,
  64. // and when it gains/loses focus
  65. let saveInterval;
  66.  
  67. function onFocus() {
  68. clearInterval(saveInterval);
  69. autoSave();
  70. saveInterval = setInterval(autoSave, 60000);
  71. }
  72.  
  73. function onBlur() {
  74. clearInterval(saveInterval);
  75. autoSave();
  76. }
  77.  
  78.  
  79. window.addEventListener("focus", onFocus);
  80. window.addEventListener("blur", onBlur);
  81.  
  82.  
  83. GM.registerMenuCommand("Export Wordle save data", () => {
  84. GM.setClipboard(btoa(getLocalStorageAsString()));
  85. alert("Save data copied to clipboard.");
  86. });
  87.  
  88.  
  89. GM.registerMenuCommand("Import Wordle save data", () => {
  90. const imported = prompt("Paste save data here:");
  91.  
  92. if (imported && importLocalStorageFromString(atob(imported))) {
  93. alert("Import successful!");
  94. location.reload();
  95. }
  96. });
  97.  
  98.  
  99. GM.registerMenuCommand("Restore latest autosave", async () => {
  100. const savedata = await GM.getValue("autosave");
  101.  
  102. if (savedata) {
  103. const playedSave = JSON.parse(JSON.parse(savedata).statistics).gamesPlayed;
  104.  
  105. if (confirm(`The autosave has ${playedSave} games on record. Really restore it?`) && importLocalStorageFromString(savedata)) {
  106. alert("Restore successful!");
  107. location.reload();
  108. }
  109. }
  110. });
  111.  
  112.  
  113. GM.registerMenuCommand("Delete autosave", () => {
  114. if (confirm("Really DELETE the stored autosave?")) {
  115. // Disable autosaving on this page load
  116. window.removeEventListener("focus", onFocus);
  117. window.removeEventListener("blur", onBlur);
  118. clearInterval(saveInterval);
  119.  
  120. GM.deleteValue("autosave");
  121.  
  122. alert("Autosave cleared.");
  123. }
  124. });
  125. })();