diff --git a/lib/BetterDiscord.devp.js b/lib/BetterDiscord.devp.js index 3ff24cd..fe913f5 100644 --- a/lib/BetterDiscord.devp.js +++ b/lib/BetterDiscord.devp.js @@ -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("
")); + getUtils().alert("The following plugin(s) could not be loaded", pluginErrors.join("
")); } }); -}; +} 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("
")); + } + }); +} 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; \ No newline at end of file diff --git a/lib/Utils.js b/lib/Utils.js index b402380..4956d29 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -15,30 +15,6 @@ var logs = ""; function Utils(mainWindow) { _mainWindow = mainWindow; - - //Let's inject a decent alert box - var bdAlert = '\ -
\ -
\ -
\ -
\ - BetterDiscord - \ - \ - \ -
\ -
\ -
\ -
\ -
\ - for support.\ - #support\ - Join \ -
\ -
\ -
\ -
\ - '; - 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 = '\ +
\ +
\ +
\ +
\ + BetterDiscord - '+title+'\ + \ + \ +
\ +
\ +
'+message+'
\ +
\ +
\ + for support.\ + #support\ + Join \ +
\ +
\ +
\ +
\ + '; + + 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; \ No newline at end of file