Browse Source

fix: import and export new version

Gerald 8 years ago
parent
commit
4d41e6556b

+ 4 - 4
src/background/app.js

@@ -101,12 +101,12 @@ const commands = {
       });
     });
   },
-  SetValue({ id, values }) {
-    return vmdb.setValues(id, values)
-    .then(() => {
+  SetValue({ where, values }) {
+    return vmdb.setValues(where, values)
+    .then(data => {
       broadcast({
         cmd: 'UpdateValues',
-        data: { id, values },
+        data,
       });
     });
   },

+ 15 - 6
src/background/utils/db.js

@@ -311,7 +311,7 @@ export function getScript(where) {
   if (where.id) {
     script = store.scriptMap[where.id];
   } else {
-    const uri = getNameURI({ meta: where.meta, id: '@@should-have-name' });
+    const uri = where.uri || getNameURI({ meta: where.meta, id: '@@should-have-name' });
     const predicate = item => uri === object.get(item, 'props.uri');
     script = store.scripts.find(predicate);
   }
@@ -331,8 +331,13 @@ export function getScriptCode(id) {
   return storage.code.getOne(id);
 }
 
-export function setValues(id, values) {
-  return storage.value.dump(id, values);
+export function setValues(where, values) {
+  return (where.id
+    ? Promise.resolve(where.id)
+    : getScript(where).then(script => object.get(script, 'props.id')))
+  .then(id => {
+    if (id) storage.value.dump(id, values).then(() => ({ id, values }));
+  });
 }
 
 /**
@@ -470,9 +475,13 @@ export function updateScriptInfo(id, data) {
 }
 
 export function getExportData(ids, withValues) {
-  return Promise.all(ids.map(id => getScript({ id })))
-  .then(scripts => {
-    const data = { scripts };
+  return Promise.all([
+    Promise.all(ids.map(id => getScript({ id }))),
+    storage.code.getMulti(ids),
+  ])
+  .then(([scripts, codeMap]) => {
+    const data = {};
+    data.items = scripts.map(script => ({ script, code: codeMap[script.props.id] }));
     if (withValues) {
       return storage.value.getMulti(ids)
       .then(values => {

+ 1 - 1
src/injected/web/index.js

@@ -333,7 +333,7 @@ function wrapGM(script, metaStr, cache) {
     bridge.post({
       cmd: 'SetValue',
       data: {
-        id: script.props.id,
+        where: { id: script.props.id },
         values: getValues(),
       },
     });

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

@@ -136,7 +136,7 @@ function exportData(selectedIds) {
       ids: selectedIds,
     },
   })
-  .then((data) => {
+  .then(data => {
     const names = {};
     const vm = {
       scripts: {},
@@ -144,28 +144,31 @@ function exportData(selectedIds) {
     };
     delete vm.settings.sync;
     if (withValues) vm.values = {};
-    const files = data.scripts.map((script) => {
-      let name = script.custom.name || script.meta.name || 'Noname';
+    const files = data.items.map(({ script, code }) => {
+      let name = script.custom.name || script.meta.name || script.props.id;
       if (names[name]) {
         names[name] += 1;
         name = `${name}_${names[name]}`;
       } else names[name] = 1;
-      vm.scripts[name] = ['custom', 'enabled', 'update', 'position']
-      .reduce((res, key) => {
-        res[key] = script[key];
-        return res;
-      }, {});
+      const info = {
+        custom: script.custom,
+        config: script.config,
+        position: script.props.position,
+      };
       if (withValues) {
-        const values = data.values[script.uri];
-        if (values) vm.values[script.uri] = values;
+        // `values` are related to scripts by `props.id` in Violentmonkey,
+        // but by the global `props.uri` when exported.
+        const values = data.values[script.props.id];
+        if (values) vm.values[script.props.uri] = values;
       }
+      vm.scripts[name] = info;
       return {
         name: `${name}.user.js`,
-        content: script.code,
+        content: code,
       };
     });
     files.push({
-      name: 'ViolentMonkey',
+      name: 'violentmonkey',
       content: JSON.stringify(vm),
     });
     return files;

+ 33 - 32
src/options/views/tab-settings/vm-import.vue

@@ -65,26 +65,7 @@ function getVMConfig(text) {
   } catch (e) {
     console.warn('Error parsing ViolentMonkey configuration.');
   }
-  vm = vm || {};
-  forEachItem(vm.values, (value, key) => {
-    if (value) {
-      sendMessage({
-        cmd: 'SetValue',
-        data: {
-          uri: key,
-          values: value,
-        },
-      });
-    }
-  });
-  if (options.get('importSettings')) {
-    const ignoreKeys = ['sync'];
-    forEachItem(vm.settings, (value, key) => {
-      if (ignoreKeys.includes(key)) return;
-      options.set(key, value);
-    });
-  }
-  return vm;
+  return vm || {};
 }
 
 function getVMFile(entry, vmFile) {
@@ -97,10 +78,12 @@ function getVMFile(entry, vmFile) {
       if (vm.scripts) {
         const more = vm.scripts[entry.filename.slice(0, -8)];
         if (more) {
-          if (more.custom) data.custom = more.custom;
-          data.more = more;
-          delete more.id;
-          delete more.custom;
+          data.custom = more.custom;
+          data.config = more.config;
+          data.position = more.position;
+          // Import data from older version
+          if ('enabled' in more) object.set(data, ['config', 'enabled'], more.enabled);
+          if ('update' in more) object.set(data, ['config', 'shouldUpdate'], more.update);
         }
       }
       sendMessage({
@@ -112,7 +95,7 @@ function getVMFile(entry, vmFile) {
 }
 
 function getVMFiles(entries) {
-  const i = entries.findIndex(entry => entry.filename === 'ViolentMonkey');
+  const i = entries.findIndex(entry => entry.filename && entry.filename.toLowerCase() === 'violentmonkey');
   if (i < 0) {
     return { entries };
   }
@@ -141,14 +124,32 @@ function readZip(file) {
 function importData(file) {
   readZip(file)
   .then(getVMFiles)
-  .then((data) => {
+  .then(data => {
     const { vm, entries } = data;
-    return Promise.all(entries.map(entry => getVMFile(entry, vm)));
-  })
-  .then(res => res.filter(Boolean).length)
-  .then(count => {
-    showMessage({ text: i18n('msgImported', [count]) });
-    sendMessage({ cmd: 'CheckPosition' });
+    if (options.get('importSettings')) {
+      const ignoreKeys = ['sync'];
+      forEachItem(vm.settings, (value, key) => {
+        if (ignoreKeys.includes(key)) return;
+        options.set(key, value);
+      });
+    }
+    return Promise.all(entries.map(entry => getVMFile(entry, vm)))
+    .then(res => res.filter(Boolean).length)
+    .then(count => {
+      forEachItem(vm.values, (value, key) => {
+        if (value) {
+          sendMessage({
+            cmd: 'SetValue',
+            data: {
+              where: { uri: key },
+              values: value,
+            },
+          });
+        }
+      });
+      showMessage({ text: i18n('msgImported', [count]) });
+      sendMessage({ cmd: 'CheckPosition' });
+    });
   });
 }
 </script>