2018-01-10 20:19:34 +01:00
|
|
|
/**
|
|
|
|
* BetterDiscord Utils Module
|
|
|
|
* Copyright (c) 2015-present JsSucks - https://github.com/JsSucks
|
|
|
|
* All rights reserved.
|
|
|
|
* https://github.com/JsSucks - https://betterdiscord.net
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-01-27 15:51:23 +01:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-01-10 20:19:34 +01:00
|
|
|
*/
|
|
|
|
|
2018-01-27 15:51:23 +01:00
|
|
|
const
|
2018-01-10 17:02:29 +01:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs');
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
const { Module } = require('./modulebase');
|
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
class Utils {
|
|
|
|
|
|
|
|
static async tryParseJson(jsonString) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
}catch(err) {
|
|
|
|
throw ({
|
|
|
|
'message': 'Failed to parse json',
|
|
|
|
err
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get timestamp() {
|
|
|
|
return 'Timestamp';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileUtils {
|
|
|
|
|
|
|
|
static async fileExists(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if(err) return reject({
|
|
|
|
'message': `No such file or directory: ${err.path}`,
|
|
|
|
err
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!stats.isFile()) return reject({
|
|
|
|
'message': `Not a file: ${path}`,
|
|
|
|
stats
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async directoryExists(path) {
|
2018-01-27 15:51:23 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-10 17:02:29 +01:00
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if(err) return reject({
|
|
|
|
'message': `Directory does not exist: ${path}`,
|
|
|
|
err
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!stats.isDirectory()) return reject({
|
|
|
|
'message': `Not a directory: ${path}`,
|
|
|
|
stats
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async readFile(path) {
|
|
|
|
try {
|
|
|
|
await this.fileExists(path);
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
|
2018-01-27 15:51:23 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-10 17:02:29 +01:00
|
|
|
fs.readFile(path, 'utf-8', (err, data) => {
|
|
|
|
if(err) reject({
|
|
|
|
'message': `Could not read file: ${path}`,
|
|
|
|
err
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async readJsonFromFile(path) {
|
|
|
|
let readFile;
|
|
|
|
try {
|
|
|
|
readFile = await this.readFile(path);
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parsed = await Utils.tryParseJson(readFile);
|
|
|
|
return parsed;
|
|
|
|
} catch(err) {
|
|
|
|
throw(Object.assign(err, { path }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
class WindowUtils extends Module {
|
|
|
|
|
|
|
|
bindings() {
|
|
|
|
this.openDevTools = this.openDevTools.bind(this);
|
|
|
|
this.executeJavascript = this.executeJavascript.bind(this);
|
|
|
|
this.injectScript = this.injectScript.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
get window() {
|
|
|
|
return this.state.window;
|
|
|
|
}
|
|
|
|
|
|
|
|
get webContents() {
|
|
|
|
return this.window.webContents;
|
|
|
|
}
|
|
|
|
|
|
|
|
openDevTools() {
|
|
|
|
this.webContents.openDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
executeJavascript(script) {
|
|
|
|
this.webContents.executeJavaScript(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
injectScript(fpath, variable) {
|
|
|
|
console.log(`Injecting: ${fpath}`);
|
|
|
|
if (variable) this.executeJavascript(`${variable} = require("${fpath}");`);
|
|
|
|
else this.executeJavascript(`require("${fpath}");`);
|
|
|
|
}
|
2018-01-14 07:00:21 +01:00
|
|
|
|
|
|
|
events(event, callback) {
|
|
|
|
this.webContents.on(event, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
send(channel, message) {
|
|
|
|
channel = channel.startsWith('bd-') ? channel : `bd-${channel}`;
|
|
|
|
this.webContents.send(channel, message);
|
|
|
|
}
|
2018-01-27 15:51:23 +01:00
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
module.exports = {
|
|
|
|
Utils,
|
2018-01-10 23:15:31 +01:00
|
|
|
FileUtils,
|
|
|
|
WindowUtils
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|