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
* Version: 2.0
* Version: 2.1
* Author: Jiiks | http://jiiks.net
* Date: 27/08/2015 - 15:51
* Last Update: 30/11/2015 - 21:41
@ -12,45 +12,16 @@ var _config = require("./config.json");
var _utils = require("./utils");
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";
//Beta flag
var _beta = false;
var _alerts = true;
//Local flag
var _local = false;
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
var _version
var _version;
var _mainWindow;
var _updater;
var _hash;
@ -62,6 +33,13 @@ var _cacheDays = 0;
var _dataPath;
//IDE
_config = {
"Core": {
"Version": "0.2.5"
}
};
//noinspection JSUnresolvedVariable;
var _os = process.platform;
var _userFile;
@ -70,7 +48,6 @@ var _this;
function BetterDiscord(mainWindow) {
_this = this;
_mainWindow = mainWindow;
this.initLoaders();
_version = _config.Core.Version;
_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';
_dataPath += "/BetterDiscord/";
_mainWindow.webContents.on('dom-ready', function() {
if (!_fs.existsSync(_dataPath)) {
console.log('BetterDiscord: Creating BD Dir');
_fs.mkdirSync(_dataPath);
}
if (!_fs.existsSync(_dataPath)) {
console.log('BetterDiscord: Creating BD Dir');
_fs.mkdirSync(_dataPath);
}
if (!_fs.existsSync(_dataPath + "plugins/")) {
console.log('BetterDiscord: Creating Plugins Dir');
_fs.mkdirSync(_dataPath + "plugins/");
}
if (!_fs.existsSync(_dataPath + "plugins/")) {
console.log('BetterDiscord: Creating Plugins Dir');
_fs.mkdirSync(_dataPath + "plugins/");
}
if (!_fs.existsSync(_dataPath + "themes/")) {
console.log('BetterDiscord: Creating Themes Dir');
_fs.mkdirSync(_dataPath + "themes/");
}
_mainWindow.webContents.executeJavaScript('var themesupport2 = true');
if (!_fs.existsSync(_dataPath + "themes/")) {
console.log('BetterDiscord: Creating Themes Dir');
_fs.mkdirSync(_dataPath + "themes/");
}
_mainWindow.webContents.executeJavaScript('var themesupport2 = true');
_fs.readdir(_dataPath + "plugins/", function(err, files) {
if (err) {
console.log(err);
_fs.readdir(_dataPath + "plugins/", function(err, files) {
if (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;
}
_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;
}
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 } })();')
});
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) {
if (err) {
console.log(err);
_fs.readdir(_dataPath + 'themes/', function(err, files) {
if (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;
}
_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;
}
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var parse = JSON.parse(themeVar);
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 + '" } })();');
});
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var parse = JSON.parse(themeVar);
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() {
return _utils;
}
};
BetterDiscord.prototype.createAndCheckData = function(callback) {
@ -258,7 +233,9 @@ BetterDiscord.prototype.init = function() {
BetterDiscord.prototype.start = function() {
_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) {
@ -267,8 +244,12 @@ BetterDiscord.prototype.quit = function(reason) {
var ipcHooked = false;
var _domHooked = false;
BetterDiscord.prototype.domReady = function() {
_domHooked = true;
if(ipcHooked) {
_this.load(true);
return;
@ -281,6 +262,8 @@ BetterDiscord.prototype.domReady = function() {
BetterDiscord.prototype.load = function(reload) {
_this.getUtils().log("Hooked dom-ready");
_this.initLoaders();
if(reload) {
_this.getUtils().log("Reloading");
}