Move loaders to same dom event and added a failsafe dom hook

This commit is contained in:
Jiiks 2015-12-18 17:23:08 +02:00
parent 105a04a000
commit 0fdfcaa3e8
1 changed files with 74 additions and 91 deletions

View File

@ -1,5 +1,5 @@
/* BetterDiscordApp Entry /* BetterDiscordApp Entry
* Version: 2.0 * Version: 2.1
* Author: Jiiks | http://jiiks.net * Author: Jiiks | http://jiiks.net
* Date: 27/08/2015 - 15:51 * Date: 27/08/2015 - 15:51
* Last Update: 30/11/2015 - 21:41 * Last Update: 30/11/2015 - 21:41
@ -12,45 +12,16 @@ var _config = require("./config.json");
var _utils = require("./utils"); var _utils = require("./utils");
var _ipc = require('ipc'); var _ipc = require('ipc');
//For IDE
/*_config = {
"Core": {
"Version": "0.2.0"
},
"EmoteModule": {
"Twitch":{
"EmoteData": "emotedata_twitch.json",
"EmoteUrlStart": "https://static-cdn.jtvnw.net/emoticons/v1/",
"EmoteUrlEnd": "/1.0"
},
"FrankerFaceZ": {
"EmoteData": "emotedata_ffz.json",
"EmoteUrlStart": "https://cdn.frankerfacez.com/emoticon/",
"EmoteUrlEnd": "/1"
},
"BetterTTV": {
"EmoteData": "emotedata_bttv.json",
"EmoteUrlStart": "",
"EmoteUrlEnd": ""
}
}
};*/
var _repo = "Jiiks"; var _repo = "Jiiks";
//Beta flag //Beta flag
var _beta = false; var _beta = false;
var _alerts = true;
//Local flag //Local flag
var _local = false; var _local = false;
var _localServer = "http://localhost"; var _localServer = "http://localhost";
//Please note that you must either use https or set web-security to false when using local server
//Everything is loaded from _localServer/BetterDiscordApp/
//For css use: _localServer/BetterDiscordApp/css/main.css
//For js use: _localServer/BetterDiscordApp/js/main.js
//Variables //Variables
var _version var _version;
var _mainWindow; var _mainWindow;
var _updater; var _updater;
var _hash; var _hash;
@ -62,6 +33,13 @@ var _cacheDays = 0;
var _dataPath; var _dataPath;
//IDE
_config = {
"Core": {
"Version": "0.2.5"
}
};
//noinspection JSUnresolvedVariable; //noinspection JSUnresolvedVariable;
var _os = process.platform; var _os = process.platform;
var _userFile; var _userFile;
@ -70,7 +48,6 @@ var _this;
function BetterDiscord(mainWindow) { function BetterDiscord(mainWindow) {
_this = this; _this = this;
_mainWindow = mainWindow; _mainWindow = mainWindow;
this.initLoaders();
_version = _config.Core.Version; _version = _config.Core.Version;
_utils = new _utils.Utils(mainWindow); _utils = new _utils.Utils(mainWindow);
@ -82,78 +59,76 @@ BetterDiscord.prototype.initLoaders = function(){
var _dataPath = os == "win32" ? process.env.APPDATA : os == 'darwin' ? process.env.HOME + '/Library/Preferences' : '/var/local'; var _dataPath = os == "win32" ? process.env.APPDATA : os == 'darwin' ? process.env.HOME + '/Library/Preferences' : '/var/local';
_dataPath += "/BetterDiscord/"; _dataPath += "/BetterDiscord/";
_mainWindow.webContents.on('dom-ready', function() { if (!_fs.existsSync(_dataPath)) {
if (!_fs.existsSync(_dataPath)) { console.log('BetterDiscord: Creating BD Dir');
console.log('BetterDiscord: Creating BD Dir'); _fs.mkdirSync(_dataPath);
_fs.mkdirSync(_dataPath); }
}
if (!_fs.existsSync(_dataPath + "plugins/")) { if (!_fs.existsSync(_dataPath + "plugins/")) {
console.log('BetterDiscord: Creating Plugins Dir'); console.log('BetterDiscord: Creating Plugins Dir');
_fs.mkdirSync(_dataPath + "plugins/"); _fs.mkdirSync(_dataPath + "plugins/");
} }
if (!_fs.existsSync(_dataPath + "themes/")) { if (!_fs.existsSync(_dataPath + "themes/")) {
console.log('BetterDiscord: Creating Themes Dir'); console.log('BetterDiscord: Creating Themes Dir');
_fs.mkdirSync(_dataPath + "themes/"); _fs.mkdirSync(_dataPath + "themes/");
} }
_mainWindow.webContents.executeJavaScript('var themesupport2 = true'); _mainWindow.webContents.executeJavaScript('var themesupport2 = true');
_fs.readdir(_dataPath + "plugins/", function(err, files) { _fs.readdir(_dataPath + "plugins/", function(err, files) {
if (err) { if (err) {
console.log(err); console.log(err);
return;
}
_mainWindow.webContents.executeJavaScript('var bdplugins = {};');
files.forEach(function(fileName) {
var plugin = _fs.readFileSync(_dataPath + "plugins/" + fileName, 'utf8');
var meta = plugin.split('\n')[0];
if (meta.indexOf('META') < 0) {
console.log('BetterDiscord: ERROR[Plugin META not found in file: ' + fileName + ']');
return; return;
} }
_mainWindow.webContents.executeJavaScript('var bdplugins = {};'); var pluginVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
files.forEach(function(fileName) { var parse = JSON.parse(pluginVar);
var plugin = _fs.readFileSync(_dataPath + "plugins/" + fileName, 'utf8'); var pluginName = parse['name'];
var meta = plugin.split('\n')[0]; console.log('BetterDiscord: Loading Plugin: ' + pluginName);
if (meta.indexOf('META') < 0) { _mainWindow.webContents.executeJavaScript(plugin);
console.log('BetterDiscord: ERROR[Plugin META not found in file: ' + fileName + ']'); _mainWindow.webContents.executeJavaScript('(function() { var plugin = new ' + pluginName + '(); bdplugins[plugin.getName()] = { "plugin": plugin, "enabled": false } })();')
return;
}
var pluginVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var parse = JSON.parse(pluginVar);
var pluginName = parse['name'];
console.log('BetterDiscord: Loading Plugin: ' + pluginName);
_mainWindow.webContents.executeJavaScript(plugin);
_mainWindow.webContents.executeJavaScript('(function() { var plugin = new ' + pluginName + '(); bdplugins[plugin.getName()] = { "plugin": plugin, "enabled": false } })();')
});
}); });
});
_fs.readdir(_dataPath + 'themes/', function(err, files) { _fs.readdir(_dataPath + 'themes/', function(err, files) {
if (err) { if (err) {
console.log(err); console.log(err);
return;
}
_mainWindow.webContents.executeJavaScript('var bdthemes = {};');
files.forEach(function(fileName) {
var theme = _fs.readFileSync(_dataPath + 'themes/' + fileName, 'utf8');
var split = theme.split('\n');
var meta = split[0];
if (meta.indexOf('META') < 0) {
console.log('BetterDiscord: ERROR[Theme META not found in file: ' + fileName + ']');
return; return;
} }
_mainWindow.webContents.executeJavaScript('var bdthemes = {};'); var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
files.forEach(function(fileName) { var parse = JSON.parse(themeVar);
var theme = _fs.readFileSync(_dataPath + 'themes/' + fileName, 'utf8'); var themeName = parse['name'];
var split = theme.split('\n'); var themeAuthor = parse['author'];
var meta = split[0]; var themeDescription = parse['description'];
if (meta.indexOf('META') < 0) { var themeVersion = parse['version'];
console.log('BetterDiscord: ERROR[Theme META not found in file: ' + fileName + ']'); console.log('BetterDiscord: Loading Theme: ' + themeName);
return; split.splice(0, 1);
} theme = split.join('\n');
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//')); theme = theme.replace(/(\r\n|\n|\r)/gm, '');
var parse = JSON.parse(themeVar); _mainWindow.webContents.executeJavaScript('(function() { bdthemes["' + themeName + '"] = { "enabled": false, "name": "' + themeName + '", "css": "' + escape(theme) + '", "description": "' + themeDescription + '", "author":"' + themeAuthor + '", "version":"' + themeVersion + '" } })();');
var themeName = parse['name'];
var themeAuthor = parse['author'];
var themeDescription = parse['description'];
var themeVersion = parse['version'];
console.log('BetterDiscord: Loading Theme: ' + themeName);
split.splice(0, 1);
theme = split.join('\n');
theme = theme.replace(/(\r\n|\n|\r)/gm, '');
_mainWindow.webContents.executeJavaScript('(function() { bdthemes["' + themeName + '"] = { "enabled": false, "name": "' + themeName + '", "css": "' + escape(theme) + '", "description": "' + themeDescription + '", "author":"' + themeAuthor + '", "version":"' + themeVersion + '" } })();');
});
}); });
}); });
}; };
BetterDiscord.prototype.getUtils = function() { BetterDiscord.prototype.getUtils = function() {
return _utils; return _utils;
} };
BetterDiscord.prototype.createAndCheckData = function(callback) { BetterDiscord.prototype.createAndCheckData = function(callback) {
@ -258,7 +233,9 @@ BetterDiscord.prototype.init = function() {
BetterDiscord.prototype.start = function() { BetterDiscord.prototype.start = function() {
_this.getUtils().log("Hooking dom-ready"); _this.getUtils().log("Hooking dom-ready");
_this.getUtils().getWebContents().on('dom-ready', function() { _this.domReady(); }); var webContents = _this.getUtils().getWebContents();
webContents.on('dom-ready', function() { _this.domReady(); });
webContents.on("did-finish-loading", function() { if(!_domHooked) { _this.getUtils().log("Failsafe"); _this.domReady(); } });
}; };
BetterDiscord.prototype.quit = function(reason) { BetterDiscord.prototype.quit = function(reason) {
@ -267,8 +244,12 @@ BetterDiscord.prototype.quit = function(reason) {
var ipcHooked = false; var ipcHooked = false;
var _domHooked = false;
BetterDiscord.prototype.domReady = function() { BetterDiscord.prototype.domReady = function() {
_domHooked = true;
if(ipcHooked) { if(ipcHooked) {
_this.load(true); _this.load(true);
return; return;
@ -280,7 +261,9 @@ BetterDiscord.prototype.domReady = function() {
BetterDiscord.prototype.load = function(reload) { BetterDiscord.prototype.load = function(reload) {
_this.getUtils().log("Hooked dom-ready"); _this.getUtils().log("Hooked dom-ready");
_this.initLoaders();
if(reload) { if(reload) {
_this.getUtils().log("Reloading"); _this.getUtils().log("Reloading");
} }