From f0840da456c0cdb031972cc0f8869928ab3559ec Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Mon, 27 Nov 2017 19:29:09 -0800 Subject: [PATCH] Create a flag which will output to the console a line for each event all tabs emit. Command line flag is '--debug-tab-events', e.g. npm start -- --debug-tab-events Console ouptut contains the tabId and the event name Fix #12115 --- app/browser/tabs.js | 29 +++++++++++++++++++++++++++++ app/cmdLine.js | 3 +++ 2 files changed, 32 insertions(+) diff --git a/app/browser/tabs.js b/app/browser/tabs.js index 710618210e9..a4bb4392a0d 100644 --- a/app/browser/tabs.js +++ b/app/browser/tabs.js @@ -7,6 +7,7 @@ const windowActions = require('../../js/actions/windowActions') const tabActions = require('../common/actions/tabActions') const config = require('../../js/constants/config') const Immutable = require('immutable') +const { shouldDebugTabEvents } = require('../cmdLine') const tabState = require('../common/state/tabState') const {app, BrowserWindow, extensions, session, ipcMain} = require('electron') const {makeImmutable, makeJS} = require('../common/state/immutableUtil') @@ -71,6 +72,9 @@ const getTabValue = function (tabId) { const updateTab = (tabId, changeInfo = {}) => { let tabValue = getTabValue(tabId) + if (shouldDebugTabEvents) { + console.log('tab updated from muon', { tabId, changeIndex: changeInfo.index, changeActive: changeInfo.active, newIndex: tabValue && tabValue.get('index'), newActive: tabValue && tabValue.get('active') }) + } if (tabValue) { appActions.tabUpdated(tabValue, makeImmutable(changeInfo)) } @@ -505,18 +509,43 @@ const api = { }) process.on('chrome-tabs-created', (tabId) => { + if (shouldDebugTabEvents) { + console.log(`tab [${tabId} via process] chrome-tabs-created`) + } updateTab(tabId) }) process.on('chrome-tabs-updated', (tabId, changeInfo) => { + if (shouldDebugTabEvents) { + console.log(`tab [${tabId} via process] chrome-tabs-updated`) + } updateTab(tabId, changeInfo) }) + process.on('chrome-tabs-removed', (tabId, changeInfo) => { + if (shouldDebugTabEvents) { + console.log(`tab [${tabId} via process] - chrome-tabs-removed`) + } + }) + app.on('web-contents-created', function (event, tab) { if (tab.isBackgroundPage() || !tab.isGuest()) { return } const tabId = tab.getId() + + // command-line flag --debug-tab-events + if (shouldDebugTabEvents) { + console.log(`Tab [${tabId}] created in window ${tab.tabValue().windowId}`) + // output console log for each event the tab receives + const oldEmit = tab.emit + tab.emit = function () { + const eventTabId = tab && !tab.isDestroyed() ? tab.getId() : `probably ${tabId}` + console.log(`Tab [${eventTabId}] event '${arguments[0]}'`) + oldEmit.apply(tab, arguments) + } + } + tab.on('did-start-navigation', (e, navigationHandle) => { if (!tab.isDestroyed() && navigationHandle.isValid() && navigationHandle.isInMainFrame()) { const controller = tab.controller() diff --git a/app/cmdLine.js b/app/cmdLine.js index ea67d5aec6a..ba5af12e193 100644 --- a/app/cmdLine.js +++ b/app/cmdLine.js @@ -18,6 +18,7 @@ const fs = require('fs') const path = require('path') let appInitialized = false let newWindowURL +const debugTabEventsFlagName = '--debug-tab-events' const focusOrOpenWindow = function (url) { // don't try to do anything if the app hasn't been initialized @@ -136,3 +137,5 @@ module.exports.newWindowURL = () => { } return newWindowURL } + +module.exports.shouldDebugTabEvents = process.argv.includes(debugTabEventsFlagName)