From 976ea796f8eba3cdff9c192956b0bf55818871c5 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Thu, 8 Mar 2018 23:40:38 +0200 Subject: [PATCH] Reliable switch events --- client/src/ui/bdui.js | 93 +++++++++++++++++++++++++------------ client/src/ui/reflection.js | 1 + 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/client/src/ui/bdui.js b/client/src/ui/bdui.js index b8cacb09..fb69f361 100644 --- a/client/src/ui/bdui.js +++ b/client/src/ui/bdui.js @@ -15,10 +15,42 @@ import BdModals from './components/bd/BdModals.vue'; import { Events, WebpackModules } from 'modules'; import { Utils } from 'common'; import AutoManip from './automanip'; +import { remote } from 'electron'; + +class TempApi { + static get currentGuild() { + try { + const currentGuildId = WebpackModules.getModuleByName('SelectedGuildStore').getGuildId(); + return WebpackModules.getModuleByName('GuildStore').getGuild(currentGuildId); + } catch (err) { + return null; + } + } + static get currentChannel() { + try { + const currentChannelId = WebpackModules.getModuleByName('SelectedChannelStore').getChannelId(); + return WebpackModules.getModuleByName('ChannelStore').getChannel(currentChannelId); + } catch (err) { + return 0; + } + } + static get currentUserId() { + try { + return WebpackModules.getModuleByName('UserStore').getCurrentUser().id; + } catch (err) { + return 0; + } + } +} export default class { static initUiEvents() { + this.pathCache = { + isDm: null, + server: TempApi.currentGuild, + channel: TempApi.currentChannel + }; this.autoManip = new AutoManip(); const defer = setInterval(() => { if (!this.profilePopupModule) return; @@ -29,39 +61,42 @@ export default class { data: { userid } })); }, 100); - // This is temporary - const locationInterval = setInterval(() => { - try { - const path = window.location.pathname.match(/\d+/g); - if (!path) { - this.prevLocation = null; - Events.emit('server-switch'); + + const ehookInterval = setInterval(() => { + if (!remote.BrowserWindow.getFocusedWindow()) return; + clearInterval(ehookInterval); + remote.BrowserWindow.getFocusedWindow().webContents.on('did-navigate-in-page', (e, url, isMainFrame) => { + const { currentGuild, currentChannel } = TempApi; + if (!this.pathCache.server) { + Events.emit('server-switch', { 'server': currentGuild, 'channel': currentChannel }); + this.pathCache.server = currentGuild; + this.pathCache.channel = currentChannel; return; } - const ids = path.reduce((obj, el, index) => { - obj[index === 0 ? 'guildId' : 'channelId'] = el; - return obj; - }, - {}); - - if (Object.keys(ids).length === 1) { - ids.isDm = true; - ids.dmId = ids.guildId + if (!this.pathCache.channel) { + Events.emit('channel-switch', currentChannel); + this.pathCache.server = currentGuild; + this.pathCache.channel = currentChannel; + return; } - - if (!this.prevLocation) { - Events.emit('server-switch'); - } else { - if (this.prevLocation.channelId !== ids.channelId) { - Events.emit('channel-switch'); - } else if (this.prevLocation.guildId !== ids.guildId) { - Events.emit('server-switch'); - } + if (currentGuild && + currentGuild.id && + this.pathCache.server && + this.pathCache.server.id !== currentGuild.id) { + Events.emit('server-switch', { 'server': currentGuild, 'channel': currentChannel }); + this.pathCache.server = currentGuild; + this.pathCache.channel = currentChannel; + return; } - this.prevLocation = ids; - } catch (err) { - console.log(err); - } + if (currentChannel && + currentChannel.id && + this.pathCache.channel && + this.pathCache.channel.id !== currentChannel.id) Events.emit('channel-switch', currentChannel); + + + this.pathCache.server = currentGuild; + this.pathCache.channel = currentChannel; + }); }, 100); } diff --git a/client/src/ui/reflection.js b/client/src/ui/reflection.js index 5e28e84a..9abcdd7c 100644 --- a/client/src/ui/reflection.js +++ b/client/src/ui/reflection.js @@ -10,6 +10,7 @@ class Reflection { static reactInternalInstance(node) { + if (!node) return null; if (!Object.keys(node) || !Object.keys(node).length) return null; const riiKey = Object.keys(node).find(k => k.startsWith('__reactInternalInstance')); return riiKey ? node[riiKey] : null;