BetterDiscordApp-v1/lib/Utils.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-08-27 15:03:39 +02:00
/* BetterDiscordApp Utils and Helper functions
2015-08-29 21:31:24 +02:00
* Version: 1.2
2015-08-27 15:03:39 +02:00
* Author: Jiiks | http://jiiks.net
* Date: 25/08/2015 - 09:19
2015-08-29 21:31:24 +02:00
* Last Updated: 29/08/2015 - 22:31
2015-08-27 15:03:39 +02:00
* https://github.com/Jiiks/BetterDiscordApp
*/
2015-08-29 21:20:11 +02:00
var https = require('https');
2015-08-27 15:03:39 +02:00
var _mainWindow;
function Utils(mainWindow) {
_mainWindow = mainWindow;
}
//Get browser mainwindow
function getMainWindow() {
return _mainWindow;
}
//Download using http
2015-08-29 21:20:11 +02:00
Utils.prototype.download = function(host, path, callback) {
var options = {
host: host,
path: path,
headers: {'user-agent': 'Mozilla/5.0'},
}
https.get(options, function(res) {
2015-08-27 15:03:39 +02:00
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on("end", function() {
callback(data);
});
}).on("error", function() {
callback(null);
});
}
2015-08-29 21:20:11 +02:00
Utils.prototype.getHash = function(callback) {
this.download("api.github.com", "/repos/Jiiks/BetterDiscordApp/commits/master", function(data) {
callback(JSON.parse(data).sha);
});
}
2015-08-27 15:03:39 +02:00
//Get Webcontents
Utils.prototype.getWebContents = function() {
return getMainWindow().webContents;
}
//Js logger
2015-08-29 21:20:11 +02:00
Utils.prototype.jsLog = function(message, type) {
2015-08-27 15:03:39 +02:00
switch(type) {
case "log":
this.execJs('console.log("BetterDiscord: ' + message + '");');
break;
case "warn":
this.execJs('console.warn("BetterDiscord: ' + message + '");');
break;
case "error":
this.execJs('console.error("BetterDiscord: ' + message + '");');
break;
}
}
2015-08-29 21:20:11 +02:00
//Logger
Utils.prototype.log = function(message) {
console.log("BetterDiscrod: " + message);
}
2015-08-27 15:03:39 +02:00
//Execute javascript
Utils.prototype.execJs = function(js) {
this.getWebContents().executeJavaScript(js);
}
//Parse and execute javascript
Utils.prototype.execJsParse = function(js) {
this.execJs(js); //TODO
}
//Css internal style injector
Utils.prototype.injectStylesheet = function(url) {
var self = this;
this.download(url, function(data) {
var js = 'var style = document.createElement("style"); style.type = "text/css"; style.innerHTML = "'+data+'";';
self.injectToElementByTag("head", js, "style");
});
}
2015-08-28 09:52:36 +02:00
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+'" })();')
}
2015-08-29 21:20:11 +02:00
Utils.prototype.injectJavaScript = function(url, jquery) {
if(!jquery) {
this.execJs('(function() { var script = document.createElement("script"); script.type = "text/javascript"; document.getElementsByTagName("body")[0].appendChild(script); script.src = "' + url + '"; })();');
}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(); })(); ');
}
2015-08-28 09:52:36 +02:00
}
2015-08-27 15:03:39 +02:00
exports.Utils = Utils;