v0.2.0 - Improved resource loading and stability/bug fixes.

Tons of refactoring
This commit is contained in:
Jiiks 2015-11-30 21:46:20 +02:00
parent 00b9433717
commit d919a539b0
2 changed files with 377 additions and 280 deletions

View File

@ -1,8 +1,8 @@
/* BetterDiscordApp Entry /* BetterDiscordApp Entry
* Version: 1.4 * Version: 2.0
* Author: Jiiks | http://jiiks.net * Author: Jiiks | http://jiiks.net
* Date: 27/08/2015 - 15:51 * Date: 27/08/2015 - 15:51
* Last Update: 03/11/2015 - 18:35 * Last Update: 30/11/2015 - 21:41
* https://github.com/Jiiks/BetterDiscordApp * https://github.com/Jiiks/BetterDiscordApp
*/ */
@ -12,351 +12,448 @@ 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";
//Beta flag //Beta flag
var _beta = false; var _beta = false;
var _alerts = true; 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
var _version; //Variables
var _version
var _mainWindow; var _mainWindow;
var _updater; var _updater;
var _hash; var _hash;
var _userDefault = {"cache": null}; var _userDefault = { "cache": null };
var _userConfig = {"cache": null}; var _userConfig = _userDefault;
var _cacheExpired = false; var _cacheExpired = true;
var _cacheDays = 0; var _cacheDays = 0;
var _dataPath; var _dataPath;
//noinspection JSUnresolvedVariable;
var _os = process.platform; var _os = process.platform;
var _userFile; var _userFile;
var _this;
function BetterDiscord(mainWindow) { function BetterDiscord(mainWindow) {
_this = this;
_mainWindow = mainWindow; _mainWindow = mainWindow;
_version = _config.Core.Version; _version = _config.Core.Version;
_utils = new _utils.Utils(mainWindow); _utils = new _utils.Utils(mainWindow);
this.createAndCheckData(this.init);
} }
BetterDiscord.prototype.getUtils = function() { BetterDiscord.prototype.getUtils = function() {
return _utils; return _utils;
}; }
BetterDiscord.prototype.init = function() { BetterDiscord.prototype.createAndCheckData = function(callback) {
var self = this; this.getUtils().log("Checking Cache");
//OS specific _dataPath //New data path
if (_os == "win32") { //noinspection JSUnresolvedVariable
_dataPath = "../BetterDiscordData"; _dataPath = _os == "win32" ? process.env.APPDATA : _os == 'darwin' ? process.env.HOME + 'Library/Preference' : '/var/local';
} else if (_os == "darwin") { _dataPath += "/BetterDiscord";
_dataPath = "~/Library/Application Support/discord/BetterDiscordData";
}
_userFile = _dataPath + "/user.json"; _userFile = _dataPath + "/user.json";
//Check emotedata cache
this.getUtils().log("Checking cache");
try { try {
if(!_fs.existsSync(_dataPath)) { //Create data path folder if it doesn't exist
if (!_fs.existsSync(_dataPath)) {
this.getUtils().log("Creating Data Folder @" + _dataPath);
_fs.mkdirSync(_dataPath); _fs.mkdirSync(_dataPath);
} }
if(!_fs.existsSync(_userFile)) { //Read user config if it exists
_fs.writeFileSync(_userFile, JSON.stringify(_userDefault));
}
if(_fs.existsSync(_userFile)) { if(_fs.existsSync(_userFile)) {
_userConfig = JSON.parse(_fs.readFileSync(_userFile)); _userConfig = JSON.parse(_fs.readFileSync(_userFile));
} }
//Userfile doesn't exist
if(_userConfig.cache == null) { if(_userConfig.cache == null) {
_userDefault.cache = new Date(); _userConfig.cache = new Date();
_userConfig = _userDefault;
_fs.writeFileSync(_userFile, JSON.stringify(_userConfig));
_cacheExpired = true;
} else { } else {
var curDate = new Date(); var currentDate = new Date();
var cacheDate = new Date(_userConfig.cache); var cacheDate = new Date(_userConfig.cache);
if(Math.abs(curDate.getDate() - cacheDate.getDate()) > _cacheDays) { //Check if cache is expired
_cacheExpired = true; if(Math.abs(currentDate.getDate() - cacheDate.getDate()) > _cacheDays) {
_userConfig.cache = curDate; _userConfig.cache = currentDate;
_fs.writeFileSync(_userFile, JSON.stringify(_userConfig)); } else {
_cacheExpired = false;
} }
} }
}catch(err) { _cacheExpired = true; console.log(err); } //Write new cache date if expired
if(_cacheExpired) {
this.getUtils().log("Cache Expired or NULL");
_fs.writeFileSync(_userFile, JSON.stringify(_userConfig));
}
this.getUtils().log("Cache expired: " + _cacheExpired); callback();
}catch(err) {
//Exit BD
}
};
BetterDiscord.prototype.init = function() {
var self = this;
this.getUtils().log("Initializing");
this.getUtils().log("Getting latest hash");
//Get latest commit hash
var branch = _beta ? "beta" : "master"; var branch = _beta ? "beta" : "master";
this.getUtils().download("api.github.com", "/repos/Jiiks/BetterDiscordApp/commits/"+branch+"", function(data) {
this.getUtils().log("Using repo: " + _repo + " and branch: " + branch);
//Get the latest commit hash
this.getUtils().download("api.github.com", "/repos/" + _repo + "/BetterDiscordApp/commits/" + branch, function(data) {
try { try {
_hash = JSON.parse(data).sha; _hash = JSON.parse(data).sha;
self.getUtils().execJs("var _hash = " + _hash + ";"); self.getUtils().execJs("var _hash = " + _hash + ";");
}catch(err) { self.quit("Failed to load hash : " + err); } }catch(err) {
self.quit("Failed to load hash");
return;
}
if(typeof(_hash) == "undefined") { if(typeof(_hash) == "undefined") {
self.quit("Failed to load hash"); self.quit("Failed to load hash");
} else { return;
self.getUtils().log("Hash: "+ _hash);
//Get updater
self.getUtils().download("raw.githubusercontent.com", "/Jiiks/BetterDiscordApp/"+_hash+"/updater.json", function(data) {
try {
_updater = JSON.parse(data);
}catch(err) { self.quit("Failed to load updater : " + err); }
if(typeof(_updater) == "undefined") {
self.quit("Failed to load updater");
} else {
self.getUtils().log("Latest Version: " + _updater.LatestVersion);
self.getUtils().log("Using CDN: " + _updater.CDN);
self.getUtils().log("Starting up");
self.start();
}
});
} }
self.getUtils().log("Hash: "+ _hash);
//Download latest updater
self.getUtils().download("raw.githubusercontent.com", "/" + _repo + "/BetterDiscordApp/" + _hash + "/updater.json", function(data) {
try {
_updater = JSON.parse(data);
}catch(err) {
self.quit("Failed to load updater data");
return;
}
if(typeof(_updater) == "undefined") {
self.quit("Failed to load updater data");
return;
}
self.getUtils().log("Latest Versions: " + _updater.LatestVersion);
self.getUtils().log("Using CDN: " + _updater.CDN);
self.getUtils().log("Starting up");
self.start();
});
}); });
}; };
BetterDiscord.prototype.start = function() {
_this.getUtils().log("Hooking dom-ready");
_this.getUtils().getWebContents().on('dom-ready', function() { _this.domReady(); });
};
BetterDiscord.prototype.quit = function(reason) { BetterDiscord.prototype.quit = function(reason) {
this.getUtils().log("Quitting: " + reason); console.log("BetterDiscord ERR: " + reason);
if(_alerts) this.getUtils().alert("Quitting: " + reason); };
}
var ipcHooked = false;
BetterDiscord.prototype.start = function() { BetterDiscord.prototype.domReady = function() {
_this.getUtils().log("Hooked dom-ready");
if(_updater.LatestVersion > _version) {
_this.getUtils().execJs('alert("An update for BetterDiscord is available(v'+ _updater.LatestVersion +')! Download the latest version from GitHub!")');
}
var self = this; //Create loading element
_this.getUtils().execJs('var loadingNode = document.createElement("DIV");');
_this.getUtils().execJs('loadingNode.innerHTML = \' <div style="height:30px;width:100%;background:#282B30;"><div style="padding-right:10px; float:right"> <span id="bd-status" style="line-height:30px;color:#E8E8E8;">BetterDiscord - Loading Libraries : </span><progress id="bd-pbar" value="10" max="100"></progress></div></div> \'');
_this.getUtils().execJs('var flex = document.getElementsByClassName("flex-vertical flex-spacer")[0]; flex.appendChild(loadingNode);');
console.log("BetterDiscord: init"); //Create ipc
_this.getUtils().execJs("var betterDiscordIPC = require('ipc');");
self.getUtils().getWebContents().on('dom-ready', function() { _this.getUtils().log("Hooking ipc async");
if(_updater.LatestVersion > _version) { _ipc.on('asynchronous-message', function(event, arg) { _this.ipcAsyncMessage(event, arg); });
self.getUtils().execJs('alert("An update for BetterDiscord is available(v'+ _updater.LatestVersion +')! Download the latest version from GitHub!")');
_this.getUtils().log("Hooked ipc async");
//Inject version
_this.getUtils().execJs('var version = "'+_version+'"');
//Load jQuery
_this.getUtils().updateLoading("Loading Resources(jQuery)", 0, 100);
_this.getUtils().injectJavaScriptSync("//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", "load-jQueryUI");
};
BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) {
var libCount = 9;
var loadUs = {
'load-jQueryUI': {
'type': 'javascript',
'resource': 'jQueryUI',
'domain': 'cdnjs.cloudflare.com',
'url': '//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js',
'localurl': null,
'message': 'load-mainCSS',
'cacheable': false,
'variable': null
},
'load-mainCSS': {
'type': 'css',
'resource': 'Main CSS',
'domain': _updater.CDN,
'url': '//' + _updater.CDN + '//' + _repo + '/BetterDiscordApp/' + _hash + '/css/main.min.css',
'localurl': '',
'message': 'load-mainJS',
'cacheable': false,
'variable': null
},
'load-mainJS': {
'type': 'javascript',
'resource': 'Main JS',
'domain': _updater.CDN,
'url': '//' + _updater.CDN + '//' + _repo + '/BetterDiscordApp/' + _hash + '/js/main.min.js',
'localurl': '',
'message': 'load-publicServers',
'cacheable': false,
'variable': null
},
'load-publicServers': {
'type': 'json',
'resource': 'Public Servers',
'domain': _updater.CDN,
'url': '//' + _repo + '/BetterDiscordApp/' + _hash + 'serverlist.json',
'localurl': null,
'message': 'load-emoteData-twitchGlobal',
'cacheable': false,
'variable': 'publicServers'
},
'load-emoteData-twitchGlobal': {
'type': 'emotedata',
'resource': 'Twitch Global Emotedata',
'domain': 'http://twitchemotes.com',
'url': 'http://twitchemotes.com/api_cache/v2/global.json',
'localurl': null,
'message': 'load-emoteData-twitchSub',
'cacheable': true,
'variable': 'emotesTwitch',
'localpath': _dataPath + "/emotes_twitch_global.json",
'encoding': "utf8",
'https': false,
'parse': true,
'specialparser': 0
},
'load-emoteData-twitchSub': {
'type': 'emotedata',
'resource': 'Twitch Subscriber Emotedata',
'domain': 'http://twitchemotes.com',
'url': 'http://twitchemotes.com/api_cache/v2/subscriber.json',
'localurl': null,
'message': 'load-emoteData-ffz',
'cacheable': true,
'variable': 'subEmotesTwitch',
'localpath': _dataPath + "/emotes_twitch_subscriber.json",
'encoding': "utf8",
'https': false,
'parse': true,
'specialparser': 1
},
'load-emoteData-ffz': {
'type': 'emotedata',
'resource': 'FrankerFaceZ Emotedata',
'domain': _updater.CDN,
'url': '/' + _repo + '/BetterDiscordApp/' + _hash + '/emotedata_ffz.json',
'localurl': null,
'message': 'load-emoteData-bttv',
'cacheable': true,
'variable': 'emotesFfz',
'localpath': _dataPath + "/emotes_ffz.json",
'encoding': "utf8",
'https': true,
'parse': true,
'specialparser': 2
},
'load-emoteData-bttv': {
'type': 'emotedata',
'resource': 'BTTV Emotedata',
'domain': 'api.betterttv.net',
'url': '/emotes',
'localurl': null,
'message': 'start-bd',
'cacheable': true,
'variable': 'emotesBTTV',
'localpath': _dataPath + "/emotes_bttv.json",
'encoding': "utf8",
'https': true,
'parse': false,
'specialparser': 3
}
};
var loadCount = 0;
if(loadUs.hasOwnProperty(arg)) {
loadCount++;
var loadMe = loadUs[arg];
_this.getUtils().updateLoading("Loading Resources (" + loadMe.resource + ")", loadCount / libCount * 100, 100);
var url = loadMe.url;
if(_local && loadMe.localurl != null) {
url = loadMe.localurl;
} }
//Create loading element if(loadMe.type == 'javascript') {
self.getUtils().execJs('var loadingNode = document.createElement("DIV");'); _this.getUtils().injectJavaScriptSync(url, loadMe.message);
self.getUtils().execJs('loadingNode.innerHTML = \' <div style="height:30px;width:100%;background:#282B30;"><div style="padding-right:10px; float:right"> <span id="bd-status" style="line-height:30px;color:#E8E8E8;">BetterDiscord - Loading Libraries : </span><progress id="bd-pbar" value="10" max="100"></progress></div></div> \''); }else if(loadMe.type == 'css') {
self.getUtils().execJs('var flex = document.getElementsByClassName("flex-vertical flex-spacer")[0]; flex.appendChild(loadingNode);'); _this.getUtils().injectStylesheetSync(url, loadMe.message);
}else if(loadMe.type == 'json') {
_this.getUtils().download(loadMe.domain, loadMe.url, function(data) {
_this.getUtils().execJs('var ' + loadMe.variable + ' = ' + data + ';');
_this.getUtils().sendIcpAsync(loadMe.message);
});
}else if(loadMe.type == 'emotedata') {
//Create ipc var exists = _fs.existsSync(loadMe.localpath);
self.getUtils().execJs("var betterDiscordIPC = require('ipc');");
//ipc listener if(exists && !_cacheExpired && loadMe.cacheable) {
_ipc.on('asynchronous-message', function(event, arg){ _this.getUtils().log("Reading " + loadMe.resource + " from file");
switch(arg) { _this.injectEmoteData(loadMe, _fs.readFileSync(loadMe.localpath, loadMe.encoding));
//jQuery loaded, load jQuery cookie } else {
case 'loaded-jquery': _this.getUtils().log("Downloading " + loadMe.resource);
self.getUtils().updateLoading("Loading Resources(jQuery Cookie)", 10, 100);
self.getUtils().injectJavaScriptSync("//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js", "loaded-jquery-cookie");
break;
//jQuery cookie loaded, load css
case 'loaded-jquery-cookie':
self.getUtils().updateLoading("Loading Resources(css)", 20, 100);
//TODO make this nicer
// self.getUtils().injectStylesheetSync(_updater.CDN + "/Jiiks/BetterDiscordApp/" + _hash + "/css/main.min.css", "loaded-css");
self.getUtils().execJs('function injectMainCss() { $(\'head\').append( \'<link rel=\"stylesheet\" type=\"text\/css\" href=\"\/\/'+_updater.CDN+'\/Jiiks\/BetterDiscordApp\/'+_hash+'\/css\/main.min.css\">\' ) }');
self.getUtils().execJs('injectMainCss();');
self.getUtils().sendIcpAsync("loaded-css");
break;
//Css loaded, load main javascript
case 'loaded-css':
self.getUtils().updateLoading("Loading Resources(js)", 30, 100);
self.getUtils().injectJavaScriptSync("//" + _updater.CDN + "/Jiiks/BetterDiscordApp/"+_hash+"/js/main.min.js", "loaded-js");
break;
//Main javascript loaded, load public servers
case 'loaded-js':
self.getUtils().updateLoading("Loading Resources(Public Servers)", 40, 100);
self.getUtils().download("raw.githubusercontent.com", "/Jiiks/BetterDiscordApp/"+_hash+"/serverlist.json", function(data) {
self.getUtils().execJs('var publicServers = '+data+';');
self.getUtils().execJs('var ps = '+data+';');
self.getUtils().sendIcpAsync("loaded-publicservers");
});
break;
//Public Servers loaded, load global twitch emotes
case 'loaded-publicservers':
self.getUtils().updateLoading("Loading Resources(Twitch Global Emotedata)", 50, 100);
var tgExists = false; if(loadMe.https) {
try { _this.getUtils().download(loadMe.domain, loadMe.url, function(data) {
tgExists = _fs.existsSync(_dataPath + "/emotes_twitch_global.json"); var parsedEmoteData = _this.parseEmoteData(loadMe, data);
}catch(err) { console.log(err); } _this.saveEmoteData(loadMe, parsedEmoteData);
_this.injectEmoteData(loadMe, parsedEmoteData);
if(tgExists && !_cacheExpired) {
try {
self.getUtils().log("Reading Twitch global emotes from file");
var emotedata = _fs.readFileSync(_dataPath + "/emotes_twitch_global.json", "utf8");
JSON.parse(emotedata);
emotedata = emotedata.replace(/\$/g, "\\$").replace(/'/g, "\\'").replace(/"/g, "\\\"");
self.getUtils().execJs('var emotesTwitch = JSON.parse(\''+emotedata+'\');');
self.getUtils().sendIcpAsync("loaded-emotedata-twitchglobal");
break;
}catch(err) { console.log(err); }
}
self.getUtils().log("Downloading Twitch global emotes");
self.getUtils().downloadHttp("http://twitchemotes.com/api_cache/v2/global.json", function(emotedata) {
try {
self.getUtils().log("Writing Twitch global emotes to file");
_fs.writeFileSync(_dataPath + "/emotes_twitch_global.json", emotedata, "utf8");
}catch(err) {}
emotedata = emotedata.replace(/\$/g, "\\$").replace(/'/g, "\\'").replace(/"/g, "\\\"");
self.getUtils().execJs('var emotesTwitch = JSON.parse(\''+emotedata+'\');');
self.getUtils().sendIcpAsync("loaded-emotedata-twitchglobal");
}); });
break; } else {
//Global twitch emotes loaded, load sub emotes _this.getUtils().downloadHttp(loadMe.url, function(data) {
case 'loaded-emotedata-twitchglobal': var parsedEmoteData = _this.parseEmoteData(loadMe, data);
self.getUtils().updateLoading("Loading Resources(Twitch Subscriber Emotedata)", 60, 100); _this.saveEmoteData(loadMe, parsedEmoteData);
_this.injectEmoteData(loadMe, parsedEmoteData);
var tsExists = false;
try {
tsExists = _fs.existsSync(_dataPath + "/emotes_twitch_sub.json");
}catch(err) { console.log(err); }
if(tsExists && !_cacheExpired) {
try {
self.getUtils().log("Reading Twitch sub emotes from file");
var tsEmotedata = _fs.readFileSync(_dataPath + "/emotes_twitch_sub.json", "utf8");
JSON.parse(tsEmotedata);
self.getUtils().execJs('var subEmotesTwitch = '+tsEmotedata+';');
self.getUtils().sendIcpAsync("loaded-emotedata-twitchsubs");
break;
}catch(err) { console.log(err); }
}
self.getUtils().log("Downloading Twitch sub emotes");
self.getUtils().downloadHttp("http://twitchemotes.com/api_cache/v2/subscriber.json", function(emotedata) {
self.getUtils().updateLoading("Parsing Resources(Twitch Subscriber Emotedata)", 70, 100);
//Since there's so many sub emotes, parse them to a simple object.
//Input: {"channel":{"emotes":["emote":emoteid]}}
//Output: {"emote":emoteid}
emotedata = JSON.parse(emotedata);
var emoteData = {};
var channels = emotedata["channels"];
for(var channel in channels) {
var emotes = channels[channel]["emotes"];
for(var i = 0 ; i < emotes.length ; i++) {
var code = emotes[i]["code"];
var id = emotes[i]["image_id"]
emoteData[code] = id;
}
}
try {
self.getUtils().log("Writing Twitch sub emotes to file");
_fs.writeFile(_dataPath + "/emotes_twitch_sub.json", JSON.stringify(emoteData), "utf8");
}catch(err) { console.log(err); }
self.getUtils().execJs('var subEmotesTwitch = '+JSON.stringify(emoteData)+';');
self.getUtils().sendIcpAsync("loaded-emotedata-twitchsubs");
}); });
break; }
//Twitch sub emotes loaded, load ffz emotes
case 'loaded-emotedata-twitchsubs':
var ffzExists = false;
try {
ffzExists = _fs.existsSync(_dataPath + "/emotes_ffz.json");
}catch(err) { console.log(err); }
if(ffzExists && !_cacheExpired) {
try {
self.getUtils().log("Reading FFZ emotes from file");
var ffzEmotedata = _fs.readFileSync(_dataPath + "/emotes_ffz.json", "utf8");
JSON.parse(ffzEmotedata);
self.getUtils().execJs('var emotesFfz = JSON.parse(\''+ffzEmotedata+'\');');
self.getUtils().sendIcpAsync("loaded-emotedata-ffz");
break;
}catch(err) { console.log(err); }
}
self.getUtils().log("Downloading FFZ emotes");
self.getUtils().updateLoading("Loading Resources(FFZ Emotedata)", 80, 100);
self.getUtils().download(_updater.CDN , "/Jiiks/BetterDiscordApp/"+_hash+"/emotedata_ffz.json", function(emotedata) {
try {
self.getUtils().log("Writing FFZ emotes to file");
_fs.writeFileSync(_dataPath + "/emotes_ffz.json", emotedata, "utf8");
}catch(err) { console.log(err); }
self.getUtils().execJs('var emotesFfz = JSON.parse(\''+emotedata+'\');');
self.getUtils().sendIcpAsync("loaded-emotedata-ffz");
});
break;
//Ffz emotes loaded, load bttv emotes
case 'loaded-emotedata-ffz':
self.getUtils().updateLoading("Loading Resources(BTTV Emotedata)", 85, 100);
var bttvExists = false;
try {
bttvExists = _fs.existsSync(_dataPath + "/emotes_bttv.json");
}catch(err) { console.log(err); }
if(bttvExists && !_cacheExpired) {
try {
self.getUtils().log("Loading BTTV emotes from file");
var bttvEmotedata = _fs.readFileSync(_dataPath + "/emotes_bttv.json", "utf8");
JSON.parse(bttvEmotedata);
self.getUtils().execJs("var emotesBTTV = " + bttvEmotedata + ";");
self.getUtils().sendIcpAsync("loaded-emotedata-bttv");
break;
}catch(err) { console.log(err); }
}
self.getUtils().log("Downloading BTTV emotes");
self.getUtils().download("api.betterttv.net", "/emotes", function(emotedata) {
self.getUtils().updateLoading("Parsing Resource(BTTV Emotedata)", 90, 100);
var emoteData = {};
emotedata = JSON.parse(emotedata);
for(var emote in emotedata.emotes) {
var e = emotedata.emotes[emote];
var url = e.url;
var code = e.regex;
emoteData[code] = url;
}
try {
self.getUtils().log("Writing BTTV emotes to file");
_fs.writeFileSync(_dataPath + "/emotes_bttv.json", JSON.stringify(emoteData), "utf8");
}catch(err) { console.log(err); }
self.getUtils().execJs('var emotesBTTV = '+JSON.stringify(emoteData) +';');
self.getUtils().sendIcpAsync("loaded-emotedata-bttv");
});
break;
//Bttv emotes loaded
case 'loaded-emotedata-bttv':
self.getUtils().sendIcpAsync("start-betterdiscord");
break;
//Start BetterDiscord
case 'start-betterdiscord':
self.getUtils().updateLoading("Starting Up", 100, 100);
self.getUtils().execJs('var mainCore; var startBda = function() { mainCore = new Core(); mainCore.init(); }; startBda();');
//Remove loading node
setTimeout(function() {
self.getUtils().execJs('$("#bd-status").parent().parent().hide();');
}, 2000);
break;
} }
});
self.getUtils().execJs('var version = "'+_version+'"');
//Load jQuery }
self.getUtils().updateLoading("Loading Resources(jQuery)", 0, 100); }
self.getUtils().injectJavaScriptSync("//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", "loaded-jquery");
}); if(arg == "start-bd") {
_this.getUtils().updateLoading("Starting Up", 100, 100);
_this.getUtils().execJs('var mainCore; var startBda = function() { mainCore = new Core(); mainCore.init(); }; startBda();');
//Remove loading node
setTimeout(function() {
_this.getUtils().execJs('$("#bd-status").parent().parent().hide();');
}, 2000);
}
};
BetterDiscord.prototype.parseEmoteData = function(loadMe, emoteData) {
_this.getUtils().log("Parsing: " + loadMe.resource);
var returnData;
switch(loadMe.specialparser) {
case 0: //Twitch Global Emotes
returnData = emoteData.replace(/\$/g, "\\$").replace(/'/g, "\\'").replace(/"/g, "\\\"");
break;
case 1: //Twitch Subscriber Emotes
returnData = {};
emoteData = JSON.parse(emoteData);
var channels = emoteData["channels"];
for(var channel in channels) {
var emotes = channels[channel]["emotes"];
for(var i = 0 ; i < emotes.length ; i++) {
var code = emotes[i]["code"];
var id = emotes[i]["image_id"];
returnData[code] = id;
}
}
returnData = JSON.stringify(returnData);
break;
case 2: //FFZ Emotes
returnData = emoteData;
break;
case 3: //BTTV Emotes
returnData = {};
emoteData = JSON.parse(emoteData);
for(var emote in emoteData.emotes) {
emote = emoteData.emotes[emote];
var url = emote.url;
var code = emote.regex;
returnData[code] = url;
}
returnData = JSON.stringify(returnData);
break;
}
return returnData;
};
BetterDiscord.prototype.saveEmoteData = function(loadMe, emoteData) {
_fs.writeFileSync(loadMe.localpath, emoteData, loadMe.encoding);
};
BetterDiscord.prototype.injectEmoteData = function(loadMe, emoteData) {
if(loadMe.parse) {
_this.getUtils().execJs('var ' + loadMe.variable + ' = JSON.parse(\'' + emoteData + '\');');
} else {
_this.getUtils().execJs('var ' + loadMe.variable + ' = ' + emoteData + ';');
}
_this.getUtils().sendIcpAsync(loadMe.message);
}; };
exports.BetterDiscord = BetterDiscord; exports.BetterDiscord = BetterDiscord;

View File

@ -118,10 +118,10 @@ Utils.prototype.injectStylesheet = function(url) {
}); });
} }
//Css jquery injector
Utils.prototype.injectStylesheetSync = function(url, callbackMessage) { Utils.prototype.injectStylesheetSync = function(url, callbackMessage) {
this.execJs('$("head").append(\' <link rel="stylesheet" type="text/css" href="'+url+'"> \'); betterDiscordIPC.send("asynchronous-message", "'+callbackMessage+'"); '); this.execJs('$("head").append(" <link rel=\'stylesheet\' href=\''+url+'\'> ");');
} this.sendIcpAsync(callbackMessage);
};
Utils.prototype.headStyleSheet = function(url) { 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+'" })();') this.execJs('(function() { var stylesheet = document.createElement("link"); stylesheet.type = "text/css"; document.getElementsByTagName("head")[0].appendChild(stylesheet); stylesheet.href = "'+url+'" })();')