Advertisement
jcunews

ViolentmonkeyToTampermonkey.js

Jun 13th, 2020 (edited)
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ViolentmonkeyToTampermonkey.js v1.0.1.
  3.  
  4. Requirement: Windows Scripting Host.
  5. Note: Run using Windows Scripting Host. Not anything else.
  6.  
  7. Both Tampermonkey and Violentmonkey can import each other's backup data. But Violentmonkey's exported data use different structure where if imported to Tampermonkey, scripts' enabled state, position, storage data, and settings, are lost.
  8.  
  9. This tool converts Violentmonkey backup data folder to Tampermonkey backup data folder so that Violentmonkey backup data can be used by Tampermonkey while preserving script data as much as possible. Full data preserving is not possible due to design difference between the two addons. The backup data folder in this context is the folder which contains the extracted files of the ZIP-ed backup data.
  10.  
  11. Before using this tool, extract the contents of a Violentmonkey ZIP file, then run this tool. The tool will ask for a folder. Select the folder where the extracted files are contained, then press OK. The converted backup data will be placed in a folder named "tampermonkey_from_violentmonkey". Once the conversion process is done, ZIP the contents of that folder (excluding the folder itself), and use that ZIP file as the source data to import from Tampermonkey.
  12.  
  13. Note: the tool may show warning messages stating that it can not find a script storage data. Due to uncommon algorithm used by Violentmonkey, script storage detection is not yet perfect. The script storage data may actually exist but isn't detected by this tool. But most of them should be detectable for now.
  14. */
  15.  
  16. //Modified code from: https://stackoverflow.com/a/34681141/1510085
  17. JSONstringify = (function () {
  18.   var toString = Object.prototype.toString;
  19.   var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
  20.   var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
  21.   var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
  22.   var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
  23.   return function stringify(value) {
  24.     if (value == null) {
  25.       return 'null';
  26.     } else if (typeof value === 'number') {
  27.       return isFinite(value) ? value.toString() : 'null';
  28.     } else if (typeof value === 'boolean') {
  29.       return value.toString();
  30.     } else if (typeof value === 'object') {
  31.       if (isArray(value)) {
  32.         var res = '[';
  33.         for (var i = 0; i < value.length; i++)
  34.           res += (i ? ', ' : '') + stringify(value[i]);
  35.         return res + ']';
  36.       } else if (toString.call(value) === '[object Object]') {
  37.         var tmp = [];
  38.         for (var k in value) {
  39.         if (value.hasOwnProperty(k))
  40.             tmp.push(stringify(k) + ': ' + stringify(value[k]));
  41.         }
  42.          return '{' + tmp.join(', ') + '}';
  43.       }
  44.     }
  45.     return '"' + value.toString().replace(escRE, escFunc) + '"';
  46.   };
  47. })();
  48.  
  49. fs = new ActiveXObject("scripting.filesystemobject");
  50. sa = new ActiveXObject("shell.application");
  51. sp = null;
  52. while (true) {
  53.   sp = sa.browseForFolder(0,
  54.     "Select extracted Violentmonkey backup data folder.", 0x8075, sp);
  55.   if (sp === null) WScript.quit();
  56.   if (fs.fileExists((sp = sp.self.path) + "\\violentmonkey")) break;
  57.   WScript.echo(
  58.     "Selected folder is not a valid Violentmonkey backup data folder."
  59.   );
  60. }
  61. op = "tampermonkey_from_violentmonkey";
  62. if (fs.folderExists(op)) fs.deleteFolder(op);
  63. fs.createFolder(op);
  64. f = fs.openTextFile(sp + "\\violentmonkey");
  65. eval("vm=" + f.readall());
  66. f.close();
  67. for (key in vm.scripts) {
  68.   scr = vm.scripts[key];
  69.   f = fs.createTextFile(op + "\\" + key + ".options.json", true);
  70.   f.write(JSONstringify({
  71.     meta: {modified: scr.lastModified},
  72.     options: {
  73.       check_for_updates: !!scr.config.shouldUpdate,
  74.       override: {
  75.         use_excludes: scr.custom.exclude,
  76.         use_includes: scr.custom.include,
  77.         use_matches: scr.custom.match
  78.       }
  79.     },
  80.     settings: {enabled: !!scr.config.enabled, position: scr.position}
  81.   }));
  82.   f.close();
  83.   n = new RegExp("-0a" + encodeURIComponent(key).replace(/%26/g, "&")
  84.     .replace(/-/g, "-2[dfa]").replace(/\./g, "\\.")
  85.     .replace(/%[0-9A-F]{2}/g, function(s) {
  86.       return "-" + s.substr(1).toLowerCase();
  87.     }) + "-0a$");
  88.   k = "";
  89.   for (val in vm.values) {
  90.     if (n.test(val)) {
  91.       k = val;
  92.       break;
  93.     }
  94.   };
  95.   if (k) {
  96.     f = fs.createTextFile(op + "\\" + key + ".storage.json", true);
  97.     f.write(JSONstringify({data: vm.values[k]}));
  98.     f.close();
  99.   } else {
  100.     WScript.echo("Warning:\n'" + key + "' storage is not found.");
  101.   }
  102.   fs.copyFile(sp + "\\" + key + ".user.js", op + "\\" + key + ".user.js", true);
  103. }
  104. WScript.echo("Done");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement