BetterDiscordApp-v1/dev/js/07utils.js

175 lines
5.7 KiB
JavaScript
Raw Normal View History

2015-08-26 18:41:18 +02:00
/* BetterDiscordApp Utilities JavaScript
* Version: 1.0
* Author: Jiiks | http://jiiks.net
* Date: 26/08/2015 - 15:54
* https://github.com/Jiiks/BetterDiscordApp
*/
2015-11-01 12:37:04 +01:00
var _hash;
2016-04-10 14:43:27 +02:00
2015-08-26 18:41:18 +02:00
function Utils() {
}
2016-04-10 14:43:27 +02:00
Utils.prototype.getTextArea = function () {
2015-08-26 18:41:18 +02:00
return $(".channel-textarea-inner textarea");
};
2015-08-26 18:41:18 +02:00
2016-04-10 14:43:27 +02:00
Utils.prototype.jqDefer = function (fnc) {
if (window.jQuery) {
fnc();
} else {
setTimeout(function () {
this.jqDefer(fnc);
}, 100);
}
2015-11-01 12:37:04 +01:00
};
2016-04-10 14:43:27 +02:00
Utils.prototype.getHash = function () {
$.getJSON("https://api.github.com/repos/Jiiks/BetterDiscordApp/commits/master", function (data) {
2015-11-01 12:37:04 +01:00
_hash = data.sha;
2015-12-16 12:21:46 +01:00
emoteModule.getBlacklist();
2015-11-01 12:37:04 +01:00
});
2015-12-16 12:21:46 +01:00
};
2016-04-10 14:43:27 +02:00
Utils.prototype.loadHtml = function (html, callback) {
var container = $("<div/>", {
class: "bd-container"
}).appendTo("body");
//TODO Inject these in next core update
html = '//cdn.rawgit.com/Jiiks/BetterDiscordApp/' + _hash + '/html/' + html + '.html';
2015-12-16 12:21:46 +01:00
2016-04-10 14:43:27 +02:00
container.load(html, callback());
2015-12-16 12:21:46 +01:00
};
2016-04-10 14:43:27 +02:00
Utils.prototype.injectJs = function (uri) {
2015-12-16 12:21:46 +01:00
$("<script/>", {
type: "text/javascript",
src: uri
}).appendTo($("body"));
};
2015-11-01 12:37:04 +01:00
2016-04-10 14:43:27 +02:00
Utils.prototype.injectCss = function (uri) {
2015-12-16 12:21:46 +01:00
$("<link/>", {
type: "text/css",
rel: "stylesheet",
href: uri
}).appendTo($("head"));
2016-04-10 14:43:27 +02:00
};
Utils.prototype.log = function (message) {
2016-05-22 18:40:07 +02:00
console.log('%c[%cBetterDiscord%c] %c'+message+'', 'color: red;', 'color: #303030; font-weight:700;', 'color:red;', '');
2016-04-10 14:43:27 +02:00
};
Utils.prototype.err = function (message) {
2016-05-22 18:40:07 +02:00
console.log('%c[%cBetterDiscord%c] %c'+message+'', 'color: red;', 'color: red; font-weight:700;', 'color:red;', '');
};
Utils.prototype.importSettings = function() {
mainCore.alert("Import Settings", '<div class="form" style="width:100%;"><div class="control-group"><textarea id="bda-import-textarea" style="min-height:150px;"></textarea></div><button id="bda-import-settings" class="btn btn-primary">Import</button></div>');
$("#bda-import-settings").off("click").on("click", function() {
var obj;
try {
obj = JSON.parse($("#bda-import-textarea").val());
}catch(err) {
mainCore.alert("Invalid Data", err);
return false;
}
try {
for(key in obj.settings) {
var val = obj.settings[key];
if(settingsCookie.hasOwnProperty(key)) {
settingsCookie[key] = val;
var cb = $("#" + key);
cb.prop("checked", val);
settingsPanel.updateSettings();
}
}
localStorage["bdcustomcss"] = obj.customCss;
var ccss = atob(localStorage.getItem("bdcustomcss"));
if (!customCssInitialized) {
customCssEditor.init();
customCssInitialized = true;
}
customCssEditor.applyCustomCss(ccss, settingsCookie["bda-css-0"], false);
customCssEditor.editor.setValue(ccss);
}catch(err) {
mainCore.alert("Invalid Data", err);
return false;
}
try {
$.each(obj.plugins, function(plugin) {
var enabled = obj.plugins[plugin];
if(bdplugins.hasOwnProperty(plugin)) {
pluginCookie[plugin] = enabled;
var cb = $("#"+plugin.replace(" ", "__"));
if(cb.is(":checked") && !enabled) {
bdplugins[plugin]["plugin"].stop();
cb.prop("checked", false);
}
if(!cb.is(":checked") && enabled) {
bdplugins[plugin]["plugin"].start();
cb.prop("checked", true);
}
}
});
pluginModule.savePluginData();
}catch(err) {
mainCore.alert("Failed to load plugin data", err);
return false;
}
try {
themeCookie = obj.themes;
$.each(themeCookie, function(theme) {
var enabled = themeCookie[theme];
var id = "#ti" + theme;
if(bdthemes.hasOwnProperty(theme)) {
if($(id).is(":checked") && !enabled) {
$(id).prop("checked", false);
$("#"+theme).remove();
}
if(!$(id).is(":checked") && enabled) {
$(id).prop("checked", true);
$("head").append('<style id="' + theme + '">' + unescape(bdthemes[theme]["css"]) + '</style>');
}
}
});
themeModule.saveThemeData();
}catch(err) {
mainCore.alert("Failed to load theme data", err);
return false;
}
return false;
});
};
Utils.prototype.exportSettings = function() {
var obj = {
settings: settingsCookie,
customCss: localStorage["bdcustomcss"],
plugins: pluginCookie,
themes: themeCookie,
favEmotes: window.localStorage["bdfavemotes"]
};
mainCore.alert("Export Settings", '<div class="form" style="width:100%;"><div class="control-group"><textarea style="min-height:150px;">'+JSON.stringify(obj)+'</textarea></div></div>');
};
Utils.prototype.addBackdrop = function(target) {
var backDrop = $("<div/>", {
class: "bda-backdrop",
"data-bdbackdrop": target,
mouseup: function() {
$('[data-bdalert="'+target+'"]').remove();
$(this).remove();
}
});
$("#app-mount").append(backDrop)
};
Utils.prototype.removeBackdrop = function(target) {
$('[data-bdbackdrop="'+target+'"]').remove();
};