Переглянути джерело

feat: DataStoreSerializer support

Sven 9 місяців тому
батько
коміт
5bb002bef1
5 змінених файлів з 31 додано та 3 видалено
  1. 1 1
      src/config.ts
  2. 1 1
      src/dialogs/autoLike.ts
  3. 15 0
      src/index.ts
  4. 1 1
      src/menu/menu_old.ts
  5. 13 0
      src/storeSerializer.ts

+ 1 - 1
src/config.ts

@@ -124,7 +124,7 @@ function useNewDefaultIfUnchanged<TConfig extends Partial<FeatureConfig>>(
 
 let canCompress = true;
 
-const cfgDataStore = new DataStore({
+export const cfgDataStore = new DataStore({
   id: "bytm-config",
   formatVersion,
   defaultData,

+ 1 - 1
src/dialogs/autoLike.ts

@@ -44,7 +44,7 @@ export async function getAutoLikeDialog() {
   
   if(!autoLikeImExDialog) {
     autoLikeImExDialog = new ExImDialog({
-      id: "auto-like-channels-import-export",
+      id: "auto-like-channels-export-import",
       width: 800,
       height: 600,
       // try to compress the data if possible

+ 15 - 0
src/index.ts

@@ -30,6 +30,7 @@ import {
   // general
   initVersionCheck,
 } from "./features/index.js";
+import { storeSerializer } from "./storeSerializer.js";
 
 //#region cns. watermark
 
@@ -433,6 +434,20 @@ function registerDevMenuCommands() {
     }
   });
 
+  GM.registerMenuCommand("Export data using DataStoreSerializer", async () => {
+    const ser = await storeSerializer.serialize();
+    dbg("Serialized data stores:", JSON.stringify(JSON.parse(ser)));
+    alert("See console.");
+  });
+
+  GM.registerMenuCommand("Import data using DataStoreSerializer", async () => {
+    const input = prompt("Enter the serialized data to import:");
+    if(input && input.length > 0) {
+      await storeSerializer.deserialize(input);
+      alert("Imported data. Reload the page to apply changes.");
+    }
+  });
+
   log("Registered dev menu commands");
 }
 

+ 1 - 1
src/menu/menu_old.ts

@@ -190,7 +190,7 @@ async function mountCfgMenu() {
   const exportDataSpecial = () => JSON.stringify({ formatVersion, data: getFeatures() });
 
   const exImDlg = new ExImDialog({
-    id: "bytm-config-import-export",
+    id: "bytm-config-export-import",
     width: 800,
     height: 600,
     // try to compress the data if possible

+ 13 - 0
src/storeSerializer.ts

@@ -0,0 +1,13 @@
+import { DataStoreSerializer } from "@sv443-network/userutils";
+
+import { cfgDataStore } from "./config.js";
+import { autoLikeStore } from "./features/input.js";
+
+/** Central serializer for all data stores */
+export const storeSerializer = new DataStoreSerializer([
+  cfgDataStore,
+  autoLikeStore,
+], {
+  addChecksum: true,
+  ensureIntegrity: true,
+});