diff --git a/.gitignore b/.gitignore index b7f2ac85..0a886e03 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ user.config.json /.vs /npm-debug.log /tests/ext/extensions +/tests/userdata diff --git a/client/src/index.js b/client/src/index.js index c64134a0..de27de87 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -10,7 +10,7 @@ import { DOM, BdUI, BdMenu, Modals, Toasts, Notifications, BdContextMenu, DiscordContextMenu } from 'ui'; import BdCss from './styles/index.scss'; -import { Events, CssEditor, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, Vendor, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi, BdWebApi, Connectivity, Cache, Reflection, PackageInstaller } from 'modules'; +import { Events, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, Vendor, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi, BdWebApi, Connectivity, Cache, Reflection, PackageInstaller } from 'modules'; import { ClientLogger as Logger, ClientIPC, Utils } from 'common'; import { BuiltinManager, EmoteModule, ReactDevtoolsModule, VueDevtoolsModule, TrackingProtection, E2EE } from 'builtin'; import electron from 'electron'; @@ -18,7 +18,7 @@ import path from 'path'; import { setTimeout } from 'timers'; const tests = typeof PRODUCTION === 'undefined'; -const ignoreExternal = false; +const ignoreExternal = true; class BetterDiscord { @@ -30,7 +30,7 @@ class BetterDiscord { this._bd = { DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu, DiscordContextMenu, - Events, CssEditor, Globals, Settings, Database, Updater, + Events, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, PackageInstaller, Vendor, diff --git a/client/src/modules/csseditor.js b/client/src/modules/csseditor.js index 6534532b..873a4943 100644 --- a/client/src/modules/csseditor.js +++ b/client/src/modules/csseditor.js @@ -38,6 +38,14 @@ export default new class { ClientIPC.on('bd-get-scss', () => this.scss, true); ClientIPC.on('bd-update-scss', (e, scss) => this.updateScss(scss)); ClientIPC.on('bd-save-csseditor-bounds', (e, bounds) => this.saveEditorBounds(bounds)); + ClientIPC.on('bd-editor-runScript', (e, script) => { + try { + new Function(script)(); + e.reply('ok'); + } catch (err) { + e.reply({ err: err.stack || err }); + } + }); ClientIPC.on('bd-save-scss', async (e, scss) => { await this.updateScss(scss); diff --git a/client/src/modules/editor.js b/client/src/modules/editor.js new file mode 100644 index 00000000..34f9fdf3 --- /dev/null +++ b/client/src/modules/editor.js @@ -0,0 +1,66 @@ +/** + * BetterDiscord Editor Module + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * https://betterdiscord.net + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. +*/ + +import Module from './imodule'; +import { DOM } from 'ui'; + +export default new class extends Module { + + get name() { return 'Editor' } + get delay() { return false; } + + setInitialState(state) { + return { + editorBounds: undefined + }; + } + + initialize() { + super.initialize(); + (async () => { + try { + // TODO this is temporary + const userScss = await this.send('readDataFile', 'user.scss'); + const compiled = await this.send('compileSass', { data: userScss }); + this.injectStyle('customcss', compiled.css.toString()); + } catch (err) { + console.warn('SCSS Compilation error', err); + } + })(); + } + + events(ipc) { + ipc.on('editor-runScript', (e, script) => { + try { + new Function(script)(); + e.reply('ok'); + } catch (err) { + e.reply({ err: err.stack || err }); + } + }); + + ipc.on('editor-injectStyle', (e, { id, style }) => { + this.injectStyle(id, style); + e.reply('ok'); + }); + } + + injectStyle(id, style) { + return DOM.injectStyle(style, `userstyle-${id}`); + } + + /** + * Show editor, flashes if already visible. + */ + async show() { + await this.send('editor-open', this.state.editorBounds); + } + +} diff --git a/client/src/modules/imodule.js b/client/src/modules/imodule.js new file mode 100644 index 00000000..b16b9ee8 --- /dev/null +++ b/client/src/modules/imodule.js @@ -0,0 +1,73 @@ +/** + * BetterDiscord Module Base + * 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 + * LICENSE file in the root directory of this source tree. +*/ + +/** + * Base Module that every non-static module should extend + */ + +import { ClientLogger as Logger, ClientIPC } from 'common'; + +export default class Module { + + constructor(args) { + this.__ = { + state: args || {}, + args + }; + this.setState = this.setState.bind(this); + + if (this.delay) { // If delay is set then module is set to load delayed from modulemanager + this.initialize = this.initialize.bind(this); + this.init = this.initialize; + } else { + this.initialize(); + this.init = () => { }; + } + } + + initialize() { + if (this.bindings) this.bindings(); + if (this.setInitialState) this.setState(this.setInitialState(this.state)); + if (this.events) this.events(ClientIPC); + } + + setState(newState) { + const oldState = this.state; + Object.assign(this.state, newState); + if (this.stateChanged) this.stateChanged(oldState, newState); + } + + set args(t) { } + get args() { return this.__.args; } + + set state(state) { return this.__.state = state; } + get state() { return this.__.state; } + + async send(channel, message) { + return ClientIPC.send(channel, message); + } + + log(msg) { + Logger.log(this.name, msg); + } + + warn(msg) { + Logger.log(this.name, msg); + } + + err(msg) { + Logger.log(this.name, msg); + } + + info(msg) { + Logger.log(this.name, msg); + } + +} diff --git a/client/src/modules/modulemanager.js b/client/src/modules/modulemanager.js index 962b7e93..13b94871 100644 --- a/client/src/modules/modulemanager.js +++ b/client/src/modules/modulemanager.js @@ -9,7 +9,7 @@ */ import { ClientLogger as Logger } from 'common'; -import { SocketProxy, EventHook, CssEditor } from 'modules'; +import { SocketProxy, EventHook } from 'modules'; import { ProfileBadges, ClassNormaliser } from 'ui'; import Updater from './updater'; @@ -27,7 +27,6 @@ export default class { new ClassNormaliser(), new SocketProxy(), new EventHook(), - CssEditor, Updater ]); } diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index 239e89f8..437f54fc 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -1,5 +1,6 @@ export { default as Events } from './events'; export { default as CssEditor } from './csseditor'; +export { default as Editor } from './editor'; export { default as Globals } from './globals'; export { default as Settings } from './settings'; export { default as Database } from './database'; diff --git a/client/src/ui/components/bd/CssEditor.vue b/client/src/ui/components/bd/CssEditor.vue index 18a39f57..a5639641 100644 --- a/client/src/ui/components/bd/CssEditor.vue +++ b/client/src/ui/components/bd/CssEditor.vue @@ -8,6 +8,8 @@ * LICENSE file in the root directory of this source tree. */ +// TODO this should be remade as editor instead of css editor +