Wordle save file helper

Allows to you export and import your Wordle save data

当前为 2022-02-12 提交的版本,查看 最新版本

  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.nytimes.com/games/wordle/*
  6. // @grant GM.registerMenuCommand
  7. // @grant GM.setClipboard
  8. // @grant GM.getValue
  9. // @grant GM.setValue
  10. // @grant GM.deleteValue
  11. // @version 1.1
  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. if (key.startsWith("nyt-wordle-")) {
  27. result[key] = localStorage.getItem(key);
  28. }
  29. }
  30.  
  31. return JSON.stringify(result);
  32. }
  33.  
  34.  
  35. function importLocalStorageFromString(data) {
  36. try {
  37. data = JSON.parse(data);
  38. } catch (e) {
  39. return false;
  40. }
  41.  
  42. for (const [k, v] of Object.entries(data)) {
  43. localStorage.setItem(k, v);
  44. }
  45. return true;
  46. }
  47.  
  48.  
  49. async function autoSave() {
  50. const savedata = await GM.getValue("autosave");
  51.  
  52. if (savedata) {
  53. const playedSave = JSON.parse(JSON.parse(savedata)["nyt-wordle-statistics"]).gamesPlayed;
  54. const playedActual = JSON.parse(localStorage.getItem("nyt-wordle-statistics")).gamesPlayed;
  55. if (playedActual < playedSave) {
  56. return;
  57. }
  58. }
  59.  
  60. // No save data, or number of games is equal/higher
  61. GM.setValue("autosave", getLocalStorageAsString());
  62. }
  63.  
  64.  
  65. // Save every minute while the tab is active,
  66. // and when it gains/loses focus
  67. let saveInterval;
  68.  
  69. function onFocus() {
  70. clearInterval(saveInterval);
  71. autoSave();
  72. saveInterval = setInterval(autoSave, 60000);
  73. }
  74.  
  75. function onBlur() {
  76. clearInterval(saveInterval);
  77. autoSave();
  78. }
  79.  
  80.  
  81. window.addEventListener("focus", onFocus);
  82. window.addEventListener("blur", onBlur);
  83.  
  84.  
  85. GM.registerMenuCommand("Export Wordle save data", () => {
  86. GM.setClipboard(btoa(getLocalStorageAsString()));
  87. alert("Save data copied to clipboard.");
  88. });
  89.  
  90.  
  91. GM.registerMenuCommand("Import Wordle save data", () => {
  92. const imported = prompt("Paste save data here:");
  93.  
  94. if (imported && importLocalStorageFromString(atob(imported))) {
  95. alert("Import successful!");
  96. location.reload();
  97. }
  98. });
  99.  
  100.  
  101. GM.registerMenuCommand("Restore latest autosave", async () => {
  102. const savedata = await GM.getValue("autosave");
  103.  
  104. if (savedata) {
  105. const playedSave = JSON.parse(JSON.parse(savedata)["nyt-wordle-statistics"]).gamesPlayed;
  106.  
  107. if (confirm(`The autosave has ${playedSave} games on record. Really restore it?`) && importLocalStorageFromString(savedata)) {
  108. alert("Restore successful!");
  109. location.reload();
  110. }
  111. }
  112. });
  113.  
  114.  
  115. GM.registerMenuCommand("Delete autosave", () => {
  116. if (confirm("Really DELETE the stored autosave?")) {
  117. // Disable autosaving on this page load
  118. window.removeEventListener("focus", onFocus);
  119. window.removeEventListener("blur", onBlur);
  120. clearInterval(saveInterval);
  121.  
  122. GM.deleteValue("autosave");
  123.  
  124. alert("Autosave cleared.");
  125. }
  126. });
  127. })();