Last active
March 16, 2018 14:25
-
-
Save esterTion/7677828625d0f3b774194f2a9d29a937 to your computer and use it in GitHub Desktop.
GreaseMonkey scripts to ViolentMonkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author esterTion | |
* before use: npm install xml2js jszip https://github.com/mapbox/node-sqlite3/tarball/master | |
* place inside profile\gm_scriptss, run, then import violentmonkey.zip | |
*/ | |
var fs = require('fs'); | |
var parseString = require('xml2js').parseString; | |
var JSZip = require('jszip'); | |
var sqlite3 = require('sqlite3'); | |
function readAllRows(select) { | |
var rows = { data: {}, count: 0 }; | |
var promise = new Promise(function (resolve, reject) { | |
select.each(function (err, row) { | |
rows.data[row.name] = 'o' + row.value; | |
rows.count++ | |
}, function () { | |
resolve(rows); | |
}); | |
}); | |
return promise; | |
} | |
var configData = fs.readFileSync('./config.xml'); | |
parseString(configData, async function (err, gmConfig) { | |
var vmConfig = { scripts: {}, "settings": { "isApplied": true, "autoUpdate": true, "lastUpdate": 1521190736112, "lastModified": 1521203461412, "showBadge": "unique", "exportValues": true, "closeAfterInstall": false, "trackLocalFile": false, "autoReload": false, "features": { "version": "sync", "data": { "settings": 1, "sync": 1 } }, "syncScriptStatus": true, "importSettings": true, "notifyUpdates": false, "version": 1, "filters": { "sort": "exec" } }, "values": {} }; | |
var zip = new JSZip; | |
var position = 1; | |
for (var item of gmConfig.UserScriptConfig.Script) { | |
var scriptConfig = { | |
custom: { | |
"origInclude": true, | |
"origExclude": true, | |
"origMatch": true, | |
"origExcludeMatch": true, | |
"homepageURL": item.$.homepageurl, | |
"lastInstallURL": item.$.updateurl, | |
"pathMap": { | |
} | |
}, | |
config: { | |
enabled: (item.$.enabled == 'true') | 0, | |
shouldUpdate: (item.$.checkRemoteUpdates == '1') | 0, | |
removed: 0 | |
}, | |
position: position++ | |
}; | |
var fileData = fs.readFileSync(item.$.basedir + '/' + item.$.filename, { encoding: 'utf8' }); | |
var scriptName = fileData.match(/@name[ \t]+?([^ ].+)/)[1]; | |
if (fs.existsSync(item.$.basedir + '.db')) { | |
var namespace = fileData.match(/@namespace[ \t]+?([^ ].+)/)[1]; | |
var valueKey = escape(namespace) + ':' + escape(scriptName) + ':'; | |
var db = new sqlite3.Database(item.$.basedir + '.db', sqlite3.OPEN_READONLY); | |
var select = db.prepare('SELECt * FROM scriptvals'); | |
var rows = await readAllRows(select); | |
vmConfig.values[valueKey] = rows.data; | |
console.log(`---Exported ${rows.count} rows value`); | |
select.finalize(); | |
db.close(); | |
} | |
if (vmConfig.scripts[scriptName] == undefined) { | |
vmConfig.scripts[scriptName] = scriptConfig; | |
} else { | |
for (var i = 1; vmConfig.scripts[scriptName + '_' + i] != undefined; i++) { } | |
scriptName = scriptName + '_' + i; | |
vmConfig.scripts[scriptName] = scriptConfig; | |
} | |
console.log(scriptName, /*item.$.basedir, */scriptConfig.config); | |
zip.file(scriptName + '.user.js', fileData); | |
} | |
zip.file('violentmonkey', JSON.stringify(vmConfig)); | |
zip.generateAsync({ type: "nodebuffer" }) | |
.then(function (content) { | |
fs.writeFile('violentmonkey.zip', content, function () { }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment