Updated utils and 0.2.8 dev

This commit is contained in:
Jiiks 2016-04-30 12:19:49 +03:00
parent b556f0a7de
commit d85fd95050
2 changed files with 120 additions and 50 deletions

View File

@ -1,3 +1,5 @@
'use strict';
var _fs = require("fs");
var _config = require("./config.json");
var _utils = require("./utils");
@ -14,6 +16,7 @@ var _extData = {};
function BetterDiscord(mainWindow) {
console.log("WTHFHUFHEIUWHFEUIHFWIUHFEIU");
_mainWindow = mainWindow;
_cfg = _config.cfg;
@ -22,7 +25,7 @@ function BetterDiscord(mainWindow) {
_utils2 = new _utils.Utils(mainWindow);
createAndCheckData();
};
}
function createAndCheckData() {
getUtils().log("Checking data/cache");
@ -61,7 +64,7 @@ function createAndCheckData() {
getUtils().err(err);
exit(err.message);
}
};
}
function init() {
getUtils().log("Initializing");
@ -90,9 +93,8 @@ function init() {
getUtils().log("Hash: " + _cfg.hash);
initUpdater();
});
};
}
function initUpdater() {
getUtils().log("Getting updater");
@ -120,7 +122,7 @@ function initUpdater() {
updateExtData();
start();
});
};
}
function updateExtData() {
getUtils().log("Updating ext data");
@ -286,19 +288,21 @@ function updateExtData() {
'self': 'load-emoteData-bttv-2'
}
};
};
}
function start() {
getUtils().log("Starting up");
try {
var webContents = getUtils().getWebContents();
getUtils().log("Hooking dom-ready");
webContents.on('dom-ready', domReady);
getUtils().log("Hooked dom-ready");
webContents.on('did-finish-loading', function() {
if(domReadyHooked)return;
if(domReadyHooked) {
return;
}
getUtils().log("Hooking did-finish-loading failsafe");
domReady();
getUtils().log("Hooked did-finish-loading failsafe");
@ -307,7 +311,7 @@ function start() {
}catch(err) {
exit(err);
}
};
}
var domReadyHooked = false;
var ipcHooked = false;
@ -320,14 +324,12 @@ function domReady() {
}
ipcHooked = true;
load(false);
};
}
function load(reload) {
getUtils().log(reload ? "Reloading" : "Loading");
initLoaders();
};
}
function initLoaders() {
try {
@ -339,11 +341,10 @@ function initLoaders() {
loadPlugins();
loadThemes();
}catch(err) {
exit(err);
}
};
}
function loadPlugins() {
var pluginPath = _cfg.dataPath + "plugins/";
@ -357,8 +358,13 @@ function loadPlugins() {
var pluginErrors = [];
getUtils().injectVar("bdplugins", "{}");
files.forEach(function(fileName) {
if(!fileName.endsWith(".plugin.js")) return;
if(!fileName.endsWith(".plugin.js")) {
getUtils().log("Invalid plugin detected: " + fileName);
return;
}
var plugin = _fs.readFileSync(pluginPath + fileName, 'utf8');
var meta = plugin.split(_eol)[0];
@ -370,25 +376,85 @@ function loadPlugins() {
});
if(pluginErrors.length > 0) {
getUtils().alert("The following plugins could not be loaded", pluginErrors.join("<br>"));
getUtils().alert("The following plugin(s) could not be loaded", pluginErrors.join("<br>"));
}
});
};
}
function loadThemes() {
var themePath = _cfg.dataPath + "themes/";
_fs.readdir(themePath, function(err, files) {
if(err) {
getUtils().log(err);
getUtils().alert(err);
return;
}
};
var themeErrors = [];
getUtils().injectVar("bdthtmes", "{}");
files.forEach(function(fileName) {
if(!fileName.endsWith(".theme.css")) {
getUtils().log("Invalid theme detected " + fileName);
return;
}
var theme = _fs.readFileSync(themePath + fileName, 'utf8');
var split = theme.split(_eol);
var meta = split[0];
if(meta.indexOf('META') < 0) {
getUtils().warn("Theme META not found in file: " + fileName);
themeErrors.push(fileName + " Reason: Theme META not found");
return;
}
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var themeInfo;
try {
themeInfo = JSON.parse(themeVar);
}catch(err) {
getUtils().warn("Failed to parse theme META in file ("+err+"): " + fileName);
themeErrors.push(fileName + " Reason: Failed to parse theme META (" + err + ")");
return;
}
if(themeInfo['name'] == undefined) {
getUtils().warn("Missing theme name in file: " + fileName);
themeErrors.push(fileName + " Reason: Missing theme name");
return;
}
if(themeInfo['author'] == undefined) {
getUtils().warn("Missing author name in file: " + fileName);
}
if(themeInfo['description'] == undefined) {
getUtils().warn("Missing description in file: " + fileName);
}
if(themeInfo['version'] == undefined) {
getUtils().warn("Missing version in file: " + fileName);
}
getUtils().log("Loading theme: " + themeInfo['name']);
split.splice(0, 1);
theme = split.join('\n');
theme = theme.replace(/(\r\n|\n|\r)/gm, '');
});
if(themeErrors.length > 0) {
getUtils().alert("The following theme(s) could not be loaded", themeErrors.join("<br>"));
}
});
}
function getUtils() {
return _utils2;
};
}
function exit(reason) {
error = true;
getUtils().log("Exiting. Reason: " + reason);
getUtils().saveLogs(_cfg.dataPath);
getUtils().alert("Something went wrong :(", reason);
};
}
exports.BetterDiscord = BetterDiscord;

View File

@ -15,30 +15,6 @@ var logs = "";
function Utils(mainWindow) {
_mainWindow = mainWindow;
//Let's inject a decent alert box
var bdAlert = '\
<div id=\'bdAlert\' class=\'modal\'>\
<div class=\'modal-inner\'>\
<div class=\'markdown-modal\'>\
<div class=\'markdown-modal-header\'>\
<strong style=\'float:left\'><span>BetterDiscord - </span><span id=\'bdAlertTitle\'></span></strong>\
<span></span>\
<button class=\'markdown-modal-close\' onclick=document.getElementById(\'bdAlert\').remove();></button>\
</div>\
<div class=\'scroller-wrap fade\'>\
<div style=\'font-weight:700\' class=\'scroller\' id=\'bdAlertScroller\'></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.injectVar("BdAlert", bdAlert);
}
//Get browser mainwindow
@ -179,12 +155,34 @@ Utils.prototype.injectVarRaw = function(variable, data) {
this.execJs('var ' + variable + ' = ' + data + ';');
}
var alertCount = 0;
//Alert
Utils.prototype.alert = function(title, message) {
this.execJs("document.body.insertAdjacentHTML('afterbegin', BdAlert);");
this.execJs("document.getElementById('bdAlertTitle').innerHTML = \""+title+"\";");
this.execJs("document.getElementById('bdAlertScroller').innerHTML = \""+message+"\";");
this.execJs("document.getElementById('bdAlert').style.opacity = 1;");
alertCount++;
var id = 'bdalert-' + alertCount;
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+"\");");
}
//Css internal style injector
@ -215,7 +213,13 @@ Utils.prototype.injectJavaScript = function(url, jquery) {
}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(); })(); ');
}
}
Utils.prototype.mkdirSync = function(path) {
if(!_fs.existsSync(path)) {
this.log("Directory " + path + " does not exist. Creating");
_fs.mkdirSync(path);
}
};
exports.Utils = Utils;