2015-08-27 15:03:39 +02:00
|
|
|
/* BetterDiscordApp Utils and Helper functions
|
2016-05-06 20:34:40 +02:00
|
|
|
* Version: 1.5
|
2015-08-27 15:03:39 +02:00
|
|
|
* Author: Jiiks | http://jiiks.net
|
|
|
|
* Date: 25/08/2015 - 09:19
|
2016-05-06 20:34:40 +02:00
|
|
|
* Last Updated: 06/05/2016
|
2015-08-27 15:03:39 +02:00
|
|
|
* https://github.com/Jiiks/BetterDiscordApp
|
|
|
|
*/
|
|
|
|
|
2015-08-29 21:20:11 +02:00
|
|
|
var https = require('https');
|
2015-10-26 05:12:39 +01:00
|
|
|
var http = require('http');
|
2016-03-30 03:37:03 +02:00
|
|
|
var _fs = require('fs');
|
2015-08-27 15:03:39 +02:00
|
|
|
var _mainWindow;
|
2016-03-30 03:37:03 +02:00
|
|
|
var eol = require('os').EOL;
|
|
|
|
var logs = "";
|
2015-08-27 15:03:39 +02:00
|
|
|
|
|
|
|
function Utils(mainWindow) {
|
|
|
|
_mainWindow = mainWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get browser mainwindow
|
|
|
|
function getMainWindow() {
|
|
|
|
return _mainWindow;
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:12:39 +01:00
|
|
|
//Download using https
|
2015-08-29 21:20:11 +02:00
|
|
|
Utils.prototype.download = function(host, path, callback) {
|
2016-04-30 23:24:15 +02:00
|
|
|
this.log("Downloading Resource: " + host + path);
|
2015-08-29 21:20:11 +02:00
|
|
|
var options = {
|
|
|
|
host: host,
|
|
|
|
path: path,
|
|
|
|
headers: {'user-agent': 'Mozilla/5.0'},
|
|
|
|
}
|
|
|
|
|
|
|
|
https.get(options, function(res) {
|
2015-08-27 15:03:39 +02:00
|
|
|
var data = "";
|
|
|
|
res.on('data', function(chunk) {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
res.on("end", function() {
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
}).on("error", function() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
2015-10-26 05:12:39 +01:00
|
|
|
//Download using http
|
|
|
|
Utils.prototype.downloadHttp = function(url, callback) {
|
2016-04-30 23:24:15 +02:00
|
|
|
this.log("Downloading Resource: " + url);
|
2015-10-26 05:12:39 +01:00
|
|
|
http.get(url, function(result) {
|
|
|
|
var data = '';
|
|
|
|
result.on('data', function(chunk) {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
result.on('end', function() {
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
}).on('error', function() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
2015-08-27 15:03:39 +02:00
|
|
|
|
2015-10-27 21:37:53 +01:00
|
|
|
Utils.prototype.getHash = function(beta, callback) {
|
2016-03-30 03:37:03 +02:00
|
|
|
var branch = beta ? "beta" : "master";
|
2015-10-27 21:37:53 +01:00
|
|
|
this.download("api.github.com", "/repos/Jiiks/BetterDiscordApp/commits/"+branch+"", function(data) {
|
2015-08-29 21:20:11 +02:00
|
|
|
callback(JSON.parse(data).sha);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:12:39 +01:00
|
|
|
Utils.prototype.sendIcpAsync = function(message) {
|
|
|
|
this.execJs('betterDiscordIPC.send("asynchronous-message", "'+message+'");');
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:03:39 +02:00
|
|
|
//Get Webcontents
|
|
|
|
Utils.prototype.getWebContents = function() {
|
|
|
|
return getMainWindow().webContents;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Js logger
|
2015-08-29 21:20:11 +02:00
|
|
|
Utils.prototype.jsLog = function(message, type) {
|
2015-08-27 15:03:39 +02:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case "log":
|
|
|
|
this.execJs('console.log("BetterDiscord: ' + message + '");');
|
|
|
|
break;
|
|
|
|
case "warn":
|
|
|
|
this.execJs('console.warn("BetterDiscord: ' + message + '");');
|
|
|
|
break;
|
|
|
|
case "error":
|
|
|
|
this.execJs('console.error("BetterDiscord: ' + message + '");');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:12:39 +01:00
|
|
|
Utils.prototype.updateLoading = function(message, cur, max) {
|
2016-04-30 23:24:15 +02:00
|
|
|
this.log(message);
|
2015-10-26 05:12:39 +01:00
|
|
|
this.execJs('document.getElementById("bd-status").innerHTML = "BetterDiscord - '+message+' : ";');
|
|
|
|
this.execJs('document.getElementById("bd-pbar").value = '+cur+';');
|
|
|
|
this.execJs('document.getElementById("bd-pbar").max = '+max+';');
|
|
|
|
}
|
|
|
|
|
2015-08-29 21:20:11 +02:00
|
|
|
//Logger
|
|
|
|
Utils.prototype.log = function(message) {
|
2016-04-20 16:57:13 +02:00
|
|
|
console.log("[BetterDiscord INF] " + message);
|
|
|
|
var d = new Date();
|
|
|
|
var ds = ("00" + (d.getDate() + 1)).slice(-2) + "/" +
|
|
|
|
("00" + d.getMonth()).slice(-2) + "/" +
|
|
|
|
d.getFullYear() + " " +
|
|
|
|
("00" + d.getHours()).slice(-2) + ":" +
|
|
|
|
("00" + d.getMinutes()).slice(-2) + ":" +
|
|
|
|
("00" + d.getSeconds()).slice(-2);
|
|
|
|
logs += "[INF]["+ds+"] " + message + eol;
|
|
|
|
}
|
|
|
|
Utils.prototype.err = function(err) {
|
|
|
|
console.log("[BetterDiscord ERR] " + err.message);
|
|
|
|
var d = new Date();
|
|
|
|
var ds = ("00" + (d.getDate() + 1)).slice(-2) + "/" +
|
|
|
|
("00" + d.getMonth()).slice(-2) + "/" +
|
|
|
|
d.getFullYear() + " " +
|
|
|
|
("00" + d.getHours()).slice(-2) + ":" +
|
|
|
|
("00" + d.getMinutes()).slice(-2) + ":" +
|
|
|
|
("00" + d.getSeconds()).slice(-2);
|
|
|
|
logs += "[ERR]["+ds+"] " + err.message + eol;
|
|
|
|
}
|
|
|
|
Utils.prototype.warn = function(message) {
|
|
|
|
console.log("[BetterDiscord WRN] " + message);
|
|
|
|
var d = new Date();
|
|
|
|
var ds = ("00" + (d.getDate() + 1)).slice(-2) + "/" +
|
|
|
|
("00" + d.getMonth()).slice(-2) + "/" +
|
|
|
|
d.getFullYear() + " " +
|
|
|
|
("00" + d.getHours()).slice(-2) + ":" +
|
|
|
|
("00" + d.getMinutes()).slice(-2) + ":" +
|
|
|
|
("00" + d.getSeconds()).slice(-2);
|
|
|
|
logs += "[WRN]["+ds+"] " + message + eol;
|
2016-03-30 03:37:03 +02:00
|
|
|
}
|
|
|
|
Utils.prototype.saveLogs = function(path) {
|
2016-04-20 16:57:13 +02:00
|
|
|
try {
|
|
|
|
_fs.writeFileSync(path + "/logs.log", logs);
|
|
|
|
}catch(err) {}
|
2015-08-29 21:20:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:03:39 +02:00
|
|
|
//Execute javascript
|
|
|
|
Utils.prototype.execJs = function(js) {
|
|
|
|
this.getWebContents().executeJavaScript(js);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Parse and execute javascript
|
|
|
|
Utils.prototype.execJsParse = function(js) {
|
|
|
|
this.execJs(js); //TODO
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:57:13 +02:00
|
|
|
//Inject variable
|
|
|
|
Utils.prototype.injectVar = function(variable, data) {
|
|
|
|
this.execJs('var ' + variable + ' = "' + data + '";');
|
|
|
|
}
|
|
|
|
Utils.prototype.injectVarRaw = function(variable, data) {
|
|
|
|
this.execJs('var ' + variable + ' = ' + data + ';');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Alert
|
|
|
|
Utils.prototype.alert = function(title, message) {
|
2016-04-30 23:24:15 +02:00
|
|
|
var id = 'bdalert-';
|
|
|
|
for( var i=0; i < 5; i++ )
|
|
|
|
id += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".charAt(Math.floor(Math.random() * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".length));
|
2016-04-30 11:19:49 +02:00
|
|
|
var bdAlert = '\
|
|
|
|
<div id=\''+id+'\' class=\'modal\' style=\'opacity:1\'>\
|
|
|
|
<div class=\'modal-inner\'>\
|
|
|
|
<div class=\'markdown-modal\'>\
|
|
|
|
<div class=\'markdown-modal-header\'>\
|
|
|
|
<strong style=\'float:left\'><span>BetterDiscord - </span><span>'+title+'</span></strong>\
|
|
|
|
<span></span>\
|
|
|
|
<button class=\'markdown-modal-close\' onclick=document.getElementById(\''+id+'\').remove();></button>\
|
|
|
|
</div>\
|
|
|
|
<div class=\'scroller-wrap fade\'>\
|
|
|
|
<div style=\'font-weight:700\' class=\'scroller\'>'+message+'</div>\
|
|
|
|
</div>\
|
|
|
|
<div class=\'markdown-modal-footer\'>\
|
|
|
|
<span style=\'float:right\'> for support.</span>\
|
|
|
|
<a style=\'float:right\' href=\'https://discord.gg/0Tmfo5ZbOR9NxvDd\' target=\'_blank\'>#support</a>\
|
|
|
|
<span style=\'float:right\'>Join </span>\
|
|
|
|
</div>\
|
|
|
|
</div>\
|
|
|
|
</div>\
|
|
|
|
</div>\
|
|
|
|
';
|
|
|
|
|
|
|
|
this.execJs("document.body.insertAdjacentHTML('afterbegin', \""+bdAlert+"\");");
|
2016-04-20 16:57:13 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:03:39 +02:00
|
|
|
//Css internal style injector
|
|
|
|
Utils.prototype.injectStylesheet = function(url) {
|
|
|
|
var self = this;
|
|
|
|
this.download(url, function(data) {
|
|
|
|
var js = 'var style = document.createElement("style"); style.type = "text/css"; style.innerHTML = "'+data+'";';
|
|
|
|
self.injectToElementByTag("head", js, "style");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:12:39 +01:00
|
|
|
Utils.prototype.injectStylesheetSync = function(url, callbackMessage) {
|
2015-11-30 20:46:20 +01:00
|
|
|
this.execJs('$("head").append(" <link rel=\'stylesheet\' href=\''+url+'\'> ");');
|
|
|
|
this.sendIcpAsync(callbackMessage);
|
|
|
|
};
|
2015-10-26 05:12:39 +01:00
|
|
|
|
2015-08-28 09:52:36 +02:00
|
|
|
Utils.prototype.headStyleSheet = function(url) {
|
|
|
|
this.execJs('(function() { var stylesheet = document.createElement("link"); stylesheet.type = "text/css"; document.getElementsByTagName("head")[0].appendChild(stylesheet); stylesheet.href = "'+url+'" })();')
|
|
|
|
}
|
|
|
|
|
2015-10-26 05:12:39 +01:00
|
|
|
Utils.prototype.injectJavaScriptSync = function(url, callbackMessage) {
|
|
|
|
this.execJs(' (function() { var script = document.createElement("script"); script.type = "text/javascript"; script.onload = function() { betterDiscordIPC.send("asynchronous-message", "'+callbackMessage+'"); }; document.getElementsByTagName("body")[0].appendChild(script); script.src = "'+url+'"; })(); ');
|
|
|
|
}
|
|
|
|
|
2015-08-29 21:20:11 +02:00
|
|
|
Utils.prototype.injectJavaScript = function(url, jquery) {
|
|
|
|
if(!jquery) {
|
|
|
|
this.execJs('(function() { var script = document.createElement("script"); script.type = "text/javascript"; document.getElementsByTagName("body")[0].appendChild(script); script.src = "' + url + '"; })();');
|
|
|
|
}else {
|
|
|
|
this.execJs(' (function() { function injectJs() { var script = document.createElement("script"); script.type = "text/javascript"; document.getElementsByTagName("body")[0].appendChild(script); script.src = "' + url + '"; } function jqDefer() { if(window.jQuery) { injectJs(); }else{ setTimeout(function() { jqDefer(); }, 100) } } jqDefer(); })(); ');
|
|
|
|
}
|
2015-08-28 09:52:36 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 11:19:49 +02:00
|
|
|
Utils.prototype.mkdirSync = function(path) {
|
|
|
|
if(!_fs.existsSync(path)) {
|
|
|
|
this.log("Directory " + path + " does not exist. Creating");
|
|
|
|
_fs.mkdirSync(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-06 20:34:40 +02:00
|
|
|
Utils.prototype.attemptSync = function(func, attempts, attempt, message, success, err) {
|
2016-05-05 12:04:15 +02:00
|
|
|
var self = this;
|
|
|
|
attempt = attempt || 0;
|
|
|
|
attempt++;
|
|
|
|
|
|
|
|
if(attempt > attempts) {
|
|
|
|
err();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
if(!func()) {
|
2016-05-06 20:34:40 +02:00
|
|
|
self.warn(message + ", retrying #" + attempt);
|
2016-05-05 12:04:15 +02:00
|
|
|
self.try(func, attempts, attempt, message, success, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
success();
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
2016-05-06 20:34:40 +02:00
|
|
|
Utils.prototype.attempt = function(func, attempts, attempt, message, success, err) {
|
|
|
|
var self = this;
|
|
|
|
attempt = attempt || 0;
|
|
|
|
attempt++;
|
|
|
|
|
|
|
|
if(attempt > attempts) {
|
|
|
|
err();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
func(function(ok) {
|
|
|
|
if(!ok) {
|
|
|
|
self.warn(message + ", retrying #" + attempt);
|
|
|
|
self.try(func, attempts, attempt, message, success, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
success();
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
2016-05-05 12:04:15 +02:00
|
|
|
Utils.prototype.openDir = function(path) {
|
|
|
|
switch(process.platform) {
|
|
|
|
case "win32":
|
|
|
|
require("child_process").exec('start "" "' + path + '"');
|
|
|
|
break;
|
|
|
|
case "darwin":
|
|
|
|
require("child_process").exec('open ' + path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-27 15:03:39 +02:00
|
|
|
exports.Utils = Utils;
|