Просмотр исходного кода

feat: add Undo button in post-import toast

tophf 2 лет назад
Родитель
Сommit
24ee11d5ec

+ 0 - 1
src/_locales/ar/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/cs/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/de/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Rückgängig machen
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Aktualisierung

+ 0 - 1
src/_locales/el/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Αναίρεση
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/en/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Undo
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Update

+ 0 - 1
src/_locales/es/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Actualización

+ 0 - 1
src/_locales/es_419/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Deshacer
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Actualizar

+ 0 - 1
src/_locales/fi/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Peruuta
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/fr/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Annuler
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Mettre à jour

+ 0 - 1
src/_locales/hr/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/hu/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Frissítés

+ 0 - 1
src/_locales/id/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Perbarui

+ 0 - 1
src/_locales/it/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Annulla
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Aggiorna

+ 0 - 1
src/_locales/ja/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: 元に戻す
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/ko/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: 되돌리기
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: 업데이트

+ 0 - 1
src/_locales/lv/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/nl/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Bijwerken

+ 0 - 1
src/_locales/pl/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Cofnij
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Aktualizuj

+ 0 - 1
src/_locales/pt_BR/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Desfazer
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Atualizar

+ 0 - 1
src/_locales/pt_PT/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Anular
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/ro/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Revocă
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Actualizează

+ 0 - 1
src/_locales/ru/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Отменить
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/sk/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Späť
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Aktualizovať

+ 0 - 1
src/_locales/sr/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/th/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: ''
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/tr/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Geri Al
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Güncelle

+ 0 - 1
src/_locales/uk/messages.yml

@@ -101,7 +101,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Скасувати
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: Оновити

+ 0 - 1
src/_locales/vi/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: Hoàn tác
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: ''

+ 0 - 1
src/_locales/zh_CN/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: 撤销
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: 更新

+ 0 - 1
src/_locales/zh_TW/messages.yml

@@ -99,7 +99,6 @@ buttonSupport:
 buttonUndo:
   description: Button to undo removement of a script.
   message: 復原
-  touched: false
 buttonUpdate:
   description: Button to update a script.
   message: 更新

+ 21 - 1
src/background/utils/storage-cache.js

@@ -1,4 +1,4 @@
-import { ensureArray, initHooks, isEmpty } from '@/common';
+import { ensureArray, ignoreChromeErrors, initHooks, isEmpty } from '@/common';
 import initCache from '@/common/cache';
 import { INFERRED, WATCH_STORAGE } from '@/common/consts';
 import { deepCopy, deepCopyDiff, deepSize, forEachEntry } from '@/common/object';
@@ -127,6 +127,7 @@ window[WATCH_STORAGE] = fn => {
   return id;
 };
 browser.runtime.onConnect.addListener(port => {
+  if (port.name === 'undoImport') return undoImport(port);
   if (!port.name.startsWith(WATCH_STORAGE)) return;
   const { id, cfg, tabId } = JSON.parse(port.name.slice(WATCH_STORAGE.length));
   const fn = id ? watchers[id] : port.postMessage.bind(port);
@@ -229,3 +230,22 @@ async function apiChainNext() {
     apiChain = null;
   }
 }
+
+async function undoImport(port) {
+  let drop;
+  let old;
+  port.onDisconnect.addListener(() => {
+    ignoreChromeErrors();
+    drop = true;
+  });
+  port.onMessage.addListener(async () => {
+    valuesToFlush = {};
+    const cur = await apiCall(GET);
+    const toRemove = Object.keys(cur).filter(k => !(k in old));
+    if (toRemove.length) await apiCall(REMOVE, toRemove);
+    await apiCall(SET, old);
+    port.postMessage(true);
+  });
+  old = await api.get();
+  if (!drop) port.postMessage(true);
+}

+ 1 - 1
src/options/views/tab-settings/vm-export.vue

@@ -46,7 +46,7 @@ import loadZip from '@/common/zip';
 let ua;
 
 const tpl = ref();
-const dateTokens = ref(Object.keys(DATE_FMT).join(', '));
+const dateTokens = Object.keys(DATE_FMT).join(', ');
 const exporting = ref(false);
 const ffDownload = ref(IS_FIREFOX && {});
 const fileName = computed(() => {

+ 25 - 8
src/options/views/tab-settings/vm-import.vue

@@ -27,7 +27,7 @@ import { RUN_AT_RE } from '@/common/consts';
 import options from '@/common/options';
 import SettingCheck from '@/common/ui/setting-check';
 import loadZipLibrary from '@/common/zip';
-import { showConfirmation, showMessage } from '@/common/ui';
+import { showConfirmation } from '@/common/ui';
 import { store } from '../../utils';
 
 const reports = reactive([]);
@@ -87,6 +87,8 @@ async function doImportBackup(file) {
   const total = entries.reduce((n, entry) => n + entry.filename?.endsWith('.user.js'), 0);
   const vmEntry = entries.find(entry => entry.filename?.toLowerCase() === 'violentmonkey');
   const vm = vmEntry && await readContents(vmEntry) || {};
+  const undoPort = chrome.runtime.connect({ name: 'undoImport' });
+  await new Promise(resolveOnUndoMessage);
   if (!vm.scripts) vm.scripts = {};
   if (!vm.values) vm.values = {};
   await processAll(readScriptOptions, '.options.json');
@@ -100,15 +102,30 @@ async function doImportBackup(file) {
       toObjectArray(vm.settings, ([key, value]) => key !== 'sync' && { key, value }));
   }
   sendCmdDirectly('CheckPosition');
-  showMessage({
-    text: [
-      reportProgress(),
-      importScriptData ? '✔' + labelImportScriptData : '',
-      importSettings ? '✔' + labelImportSettings : '',
-    ]::trueJoin('\n'),
-  });
   await reader.close();
+  if (await showConfirmation([
+    reportProgress(),
+    importScriptData ? '✔' + labelImportScriptData : '',
+    importSettings ? '✔' + labelImportSettings : '',
+  ]::trueJoin('\n'), {
+    cancel: { text: i18n('buttonUndo'), class: 'has-error' },
+  })) {
+    undoPort.disconnect();
+  } else {
+    undoPort.postMessage(true);
+    await new Promise(resolveOnUndoMessage);
+    for (const wnd of chrome.extension.getViews()) {
+      if (wnd !== window) wnd.location.reload();
+    }
+    location.reload();
+  }
 
+  function resolveOnUndoMessage(resolve) {
+    undoPort.onMessage.addListener(function fn() {
+      undoPort.onMessage.removeListener(fn);
+      resolve();
+    });
+  }
   function parseJson(text, entry) {
     try {
       return JSON.parse(text);