diff --git a/client/dist/betterdiscord.client.js b/client/dist/betterdiscord.client.js index 011d7e9f..bf5954ac 100644 --- a/client/dist/betterdiscord.client.js +++ b/client/dist/betterdiscord.client.js @@ -27094,7 +27094,7 @@ module.exports = window.require("electron"); /***/ }), /* 131 */ -/***/ (function(module, exports, __webpack_require__) { +/***/ (function(module, exports) { /** * BetterDiscord Client WebpackModules Module @@ -27111,6 +27111,7 @@ class WebpackModules { static async getModuleByProps(props) { const modules = await this.getAllModules(); return new Promise((resolve, reject) => { + const rm = []; for (let index in modules) { if (!modules.hasOwnProperty(index)) continue; const module = modules[index]; @@ -27118,11 +27119,11 @@ class WebpackModules { if (!exports || typeof exports !== 'object') continue; if (!(props in exports)) continue; - - resolve(module.exports); - break; + rm.push(module); + // resolve(module); + // break; } - + resolve(rm); reject(null); }); } @@ -27133,10 +27134,14 @@ class WebpackModules { return new Promise((resolve, reject) => { for (let index in modules) { if (!modules.hasOwnProperty(index)) continue; - if (typeof exports === 'object' && name in exports) { + const module = modules[index]; + const { exports } = module; + if (!exports) continue; + + if (typeof exports === 'object' && (name in exports || exports.name === name)) { resolve(module.exports); break; - } else if (false) { + } else if (typeof exports === 'function' && exports.name === name) { resolve(module.exports); break; } diff --git a/client/src/modules/webpackmodules.js b/client/src/modules/webpackmodules.js index f29e8726..3c220def 100644 --- a/client/src/modules/webpackmodules.js +++ b/client/src/modules/webpackmodules.js @@ -13,6 +13,7 @@ class WebpackModules { static async getModuleByProps(props) { const modules = await this.getAllModules(); return new Promise((resolve, reject) => { + const rm = []; for (let index in modules) { if (!modules.hasOwnProperty(index)) continue; const module = modules[index]; @@ -20,11 +21,11 @@ class WebpackModules { if (!exports || typeof exports !== 'object') continue; if (!(props in exports)) continue; - - resolve(module.exports); - break; + rm.push(module); + // resolve(module); + // break; } - + resolve(rm); reject(null); }); } @@ -35,7 +36,11 @@ class WebpackModules { return new Promise((resolve, reject) => { for (let index in modules) { if (!modules.hasOwnProperty(index)) continue; - if (typeof exports === 'object' && name in exports) { + const module = modules[index]; + const { exports } = module; + if (!exports) continue; + + if (typeof exports === 'object' && (name in exports || exports.name === name)) { resolve(module.exports); break; } else if (typeof exports === 'function' && exports.name === name) { diff --git a/core/dist/main.js b/core/dist/main.js index 9bdc9047..f2a60580 100644 --- a/core/dist/main.js +++ b/core/dist/main.js @@ -24,8 +24,6 @@ const __DEV = { clientScriptPath: `${clientScriptPath}/betterdiscord.client.js` }; -console.log(__DEV); - const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins'); const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes'); @@ -74,6 +72,7 @@ class BetterDiscord { constructor(args) { this.injectScripts = this.injectScripts.bind(this); + this.ignite = this.ignite.bind(this); Common.Config = new Config(args || dummyArgs); this.comms = new Comms(); this.init(); @@ -86,6 +85,28 @@ class BetterDiscord { const window = yield _this.waitForWindow(); _this.windowUtils = new WindowUtils({ window }); + //Log some events for now + _this.windowUtils.webContents.on('did-start-loading', function (e) { + return _this.windowUtils.executeJavascript(`console.info('did-start-loading');`); + }); + _this.windowUtils.webContents.on('did-stop-loading', function (e) { + return _this.windowUtils.executeJavascript(`console.info('did-stop-loading');`); + }); + _this.windowUtils.webContents.on('did-get-response-details', function (e) { + return _this.ignite(_this.windowUtils.window); + }); + _this.windowUtils.webContents.on('page-favicon-updated', function (e) { + return _this.windowUtils.executeJavascript(`console.info('page-favicon-updated');`); + }); + _this.windowUtils.webContents.on('will-navigate', function (e) { + return _this.windowUtils.executeJavascript(`console.info('will-navigate');`); + }); + _this.windowUtils.webContents.on('did-navigate', function (e) { + return _this.windowUtils.executeJavascript(`console.info('did-navigate');`); + }); + _this.windowUtils.webContents.on('did-navigate-in-page', function (e) { + return _this.windowUtils.executeJavascript(`console.info('did-navigate-in-page');`); + }); _this.windowUtils.webContents.on('did-finish-load', function (e) { return _this.injectScripts(true); }); @@ -99,10 +120,20 @@ class BetterDiscord { } waitForWindow() { + var _this2 = this; + return _asyncToGenerator(function* () { + const self = _this2; return new Promise(function (resolve, reject) { const defer = setInterval(function () { const windows = BrowserWindow.getAllWindows(); + + if (windows.length > 0) { + windows.forEach(function (window) { + self.ignite(window); + }); + } + if (__DEV && __DEV.TESTING && windows.length > 0) { resolve(windows[0]); clearInterval(defer); @@ -113,12 +144,19 @@ class BetterDiscord { resolve(windows[0]); clearInterval(defer); } - }, 100); + }, 10); }); })(); } + ignite(window) { + //Hook things that Discord removes from global. These will be removed again in the client script + const sp = path.resolve(__dirname, 'sparkplug.js').replace(/\\/g, '/'); + window.webContents.executeJavaScript(`require("${sp}");`); + } + injectScripts(reload = false) { + console.log(`RELOAD? ${reload}`); if (__DEV) { this.windowUtils.injectScript(__DEV.clientScriptPath); } diff --git a/core/dist/sparkplug.js b/core/dist/sparkplug.js new file mode 100644 index 00000000..cc130328 --- /dev/null +++ b/core/dist/sparkplug.js @@ -0,0 +1,32 @@ +'use strict'; + +(() => { + if (window.__bd && window.__bd.ignited) return; + + console.log('[BetterDiscord|Sparkplug]'); + + const ls = window.localStorage; + if (!ls) console.warn('[BetterDiscord|Sparkplug] Failed to hook localStorage :('); + const wsOrig = window.WebSocket; + + window.__bd = { + localStorage: ls, + wsHook: null, + wsOrig, + ignited: true + }; + + window.WebSocket = function (endpoint, protocols) { + console.info(`[BetterDiscord|WebSocket Proxy] new WebSocket detected, endpoint: ${endpoint}`); + const wsHook = new wsOrig(endpoint, protocols); + if (window.__bd.setWS) { + window.__bd.setWS(wsHook); + console.info(`[BetterDiscord|WebSocket Proxy] WebSocket sent to instance`); + } else { + window.__bd['wsHook'] = wsHook; + console.info(`[BetterDiscord|WebSocket Proxy] WebSocket stored to __bd['wsHook']`); + } + + return wsHook; + }; +})(); \ No newline at end of file diff --git a/core/src/main.js b/core/src/main.js index f42de01f..3af4481a 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -71,6 +71,7 @@ class BetterDiscord { constructor(args) { this.injectScripts = this.injectScripts.bind(this); + this.ignite = this.ignite.bind(this); Common.Config = new Config(args || dummyArgs); this.comms = new Comms(); this.init(); @@ -80,19 +81,33 @@ class BetterDiscord { const window = await this.waitForWindow(); this.windowUtils = new WindowUtils({ window }); + //Log some events for now + this.windowUtils.webContents.on('did-start-loading', e => this.windowUtils.executeJavascript(`console.info('did-start-loading');`)); + this.windowUtils.webContents.on('did-stop-loading', e => this.windowUtils.executeJavascript(`console.info('did-stop-loading');`)); + this.windowUtils.webContents.on('did-get-response-details', e => this.ignite(this.windowUtils.window)); + this.windowUtils.webContents.on('page-favicon-updated', e => this.windowUtils.executeJavascript(`console.info('page-favicon-updated');`)); + this.windowUtils.webContents.on('will-navigate', e => this.windowUtils.executeJavascript(`console.info('will-navigate');`)); + this.windowUtils.webContents.on('did-navigate', e => this.windowUtils.executeJavascript(`console.info('did-navigate');`)); + this.windowUtils.webContents.on('did-navigate-in-page', e => this.windowUtils.executeJavascript(`console.info('did-navigate-in-page');`)); this.windowUtils.webContents.on('did-finish-load', e => this.injectScripts(true)); setTimeout(() => { - if (__DEV) { - this.injectScripts(); - } + if (__DEV) { this.injectScripts(); } }, 500); } async waitForWindow() { + const self = this; return new Promise((resolve, reject) => { const defer = setInterval(() => { const windows = BrowserWindow.getAllWindows(); + + if (windows.length > 0) { + windows.forEach(window => { + self.ignite(window); + }); + } + if (__DEV && __DEV.TESTING && windows.length > 0) { resolve(windows[0]); clearInterval(defer); @@ -103,11 +118,18 @@ class BetterDiscord { resolve(windows[0]); clearInterval(defer); } - }, 100); + }, 10); }); } + ignite(window) { + //Hook things that Discord removes from global. These will be removed again in the client script + const sp = path.resolve(__dirname, 'sparkplug.js').replace(/\\/g, '/'); + window.webContents.executeJavaScript(`require("${sp}");`); + } + injectScripts(reload = false) { + console.log(`RELOAD? ${reload}`); if (__DEV) { this.windowUtils.injectScript(__DEV.clientScriptPath); } diff --git a/core/src/sparkplug.js b/core/src/sparkplug.js new file mode 100644 index 00000000..350d91f8 --- /dev/null +++ b/core/src/sparkplug.js @@ -0,0 +1,31 @@ +(() => { + if (window.__bd && window.__bd.ignited) return; + + console.log('[BetterDiscord|Sparkplug]'); + + const ls = window.localStorage; + if (!ls) console.warn('[BetterDiscord|Sparkplug] Failed to hook localStorage :('); + const wsOrig = window.WebSocket; + + window.__bd = { + localStorage: ls, + wsHook: null, + wsOrig, + ignited: true + } + + window.WebSocket = function (endpoint, protocols) { + console.info(`[BetterDiscord|WebSocket Proxy] new WebSocket detected, endpoint: ${endpoint}`); + const wsHook = new wsOrig(endpoint, protocols); + if (window.__bd.setWS) { + window.__bd.setWS(wsHook); + console.info(`[BetterDiscord|WebSocket Proxy] WebSocket sent to instance`); + } else { + window.__bd['wsHook'] = wsHook; + console.info(`[BetterDiscord|WebSocket Proxy] WebSocket stored to __bd['wsHook']`); + } + + return wsHook; + } + +})(); \ No newline at end of file