From c6cab029996249a38b109517190cb154f0d6b32d Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 27 May 2018 08:31:17 -0400 Subject: [PATCH] fine-tune logger-related code - Default to being detached - Default to "Current tab" - Append current tab title to "Current tab" entry - Avoid iterating through all tabs when no change --- src/js/background.js | 2 +- src/js/logger-ui.js | 117 +++++++++++++++++++++++++++---------------- src/js/messaging.js | 39 +++++++++------ src/js/popup.js | 23 ++++----- src/js/tab.js | 2 +- src/popup.html | 2 +- 6 files changed, 112 insertions(+), 73 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index a9e99f636..9b76d11bd 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -73,7 +73,7 @@ var µBlock = (function() { // jshint ignore:line userSettings: { advancedUserEnabled: false, - alwaysDetachLogger: false, + alwaysDetachLogger: true, autoUpdate: true, cloudStorageEnabled: false, collapseBlocked: true, diff --git a/src/js/logger-ui.js b/src/js/logger-ui.js index bae91a0f9..b8329788f 100644 --- a/src/js/logger-ui.js +++ b/src/js/logger-ui.js @@ -376,20 +376,16 @@ var logDate = new Date(), var renderLogEntries = function(response) { document.body.classList.toggle('colorBlind', response.colorBlind); - var entries = response.entries; - if ( entries.length === 0 ) { - return; - } + let entries = response.entries; + if ( entries.length === 0 ) { return; } // Preserve scroll position - var height = tbody.offsetHeight; + let height = tbody.offsetHeight; - var tabIds = response.tabIds; - var n = entries.length; - var entry, tr; - for ( var i = 0; i < n; i++ ) { - entry = entries[i]; - tr = renderLogEntry(entries[i]); + let tabIds = allTabIds; + for ( let i = 0, n = entries.length; i < n; i++ ) { + let entry = entries[i]; + let tr = renderLogEntry(entries[i]); // https://github.com/gorhill/uBlock/issues/1613#issuecomment-217637122 // Unlikely, but it may happen: mark as void if associated tab no // longer exist. @@ -404,11 +400,9 @@ var renderLogEntries = function(response) { truncateLog(maxEntries); // Follow waterfall if not observing top of waterfall. - var yDelta = tbody.offsetHeight - height; - if ( yDelta === 0 ) { - return; - } - var container = uDom.nodeFromSelector('#netInspector .vscrollable'); + let yDelta = tbody.offsetHeight - height; + if ( yDelta === 0 ) { return; } + let container = uDom.nodeFromSelector('#netInspector .vscrollable'); if ( container.scrollTop !== 0 ) { container.scrollTop += yDelta; } @@ -416,6 +410,24 @@ var renderLogEntries = function(response) { /******************************************************************************/ +let updateCurrentTabTitle = (function() { + let i18nCurrentTab = vAPI.i18n('loggerCurrentTab'); + + return function() { + let select = uDom.nodeFromId('pageSelector'); + if ( select.value !== 'tab_active' ) { return; } + let opt0 = select.querySelector('[value="tab_active"]'); + let opt1 = select.querySelector('[value="tab_' + activeTabId + '"]'); + let text = i18nCurrentTab; + if ( opt1 !== null ) { + text += ' / ' + opt1.textContent; + } + opt0.textContent = text; + }; +})(); + +/******************************************************************************/ + var synchronizeTabIds = function(newTabIds) { var select = uDom.nodeFromId('pageSelector'); var selectValue = select.value; @@ -474,6 +486,8 @@ var synchronizeTabIds = function(newTabIds) { allTabIds = newTabIds; + updateCurrentTabTitle(); + return rowVoided; }; @@ -501,30 +515,32 @@ var onLogBufferRead = function(response) { } // Tab id of currently active tab + let activeTabIdChanged = false; if ( response.activeTabId ) { + activeTabIdChanged = response.activeTabId !== activeTabId; activeTabId = response.activeTabId; } - if ( Array.isArray(response.tabIds) ) { - response.tabIds = new Map(response.tabIds); - } - // This may have changed meanwhile if ( response.maxEntries !== maxEntries ) { maxEntries = response.maxEntries; uDom('#maxEntries').val(maxEntries || ''); } + if ( Array.isArray(response.tabIds) ) { + response.tabIds = new Map(response.tabIds); + } + // Neuter rows for which a tab does not exist anymore - var rowVoided = false; - if ( response.tabIdsToken !== allTabIdsToken ) { + let rowVoided = false; + if ( response.tabIds !== undefined ) { rowVoided = synchronizeTabIds(response.tabIds); allTabIdsToken = response.tabIdsToken; } - // https://github.com/gorhill/uBlock/issues/507 - // Ensure tab selector is in sync with URL hash - pageSelectorFromURLHash(); + if ( activeTabIdChanged ) { + pageSelectorFromURLHash(); + } renderLogEntries(response); @@ -554,7 +570,11 @@ var readLogBuffer = function() { if ( logger.ownerId === undefined ) { return; } vAPI.messaging.send( 'loggerUI', - { what: 'readAll', ownerId: logger.ownerId }, + { + what: 'readAll', + ownerId: logger.ownerId, + tabIdsToken: allTabIdsToken + }, onLogBufferRead ); }; @@ -566,47 +586,57 @@ var readLogBufferAsync = function() { /******************************************************************************/ -var pageSelectorChanged = function() { - var select = uDom.nodeFromId('pageSelector'); +let pageSelectorChanged = function() { + let select = uDom.nodeFromId('pageSelector'); window.location.replace('#' + select.value); pageSelectorFromURLHash(); }; -var pageSelectorFromURLHash = (function() { - var lastTabClass = ''; - var lastEffectiveTabClass = ''; +let pageSelectorFromURLHash = (function() { + let lastTabClass = ''; + let lastEffectiveTabClass = ''; + let reActiveTabId = /^(tab_[^+]+)\+(.+)$/; - var selectRows = function(tabClass) { + let selectRows = function(tabClass) { + let effectiveTabClass = tabClass; if ( tabClass === 'tab_active' ) { if ( activeTabId === undefined ) { return; } - tabClass = 'tab_' + activeTabId; + effectiveTabClass = 'tab_' + activeTabId; } - if ( tabClass === lastEffectiveTabClass ) { return; } - lastEffectiveTabClass = tabClass; + if ( effectiveTabClass === lastEffectiveTabClass ) { return; } + lastEffectiveTabClass = effectiveTabClass; document.dispatchEvent(new Event('tabIdChanged')); - var style = uDom.nodeFromId('tabFilterer'); - var sheet = style.sheet; + let style = uDom.nodeFromId('tabFilterer'); + let sheet = style.sheet; while ( sheet.cssRules.length !== 0 ) { sheet.deleteRule(0); } - if ( tabClass === '' ) { return; } + if ( effectiveTabClass === '' ) { return; } sheet.insertRule( - '#netInspector tr:not(.' + tabClass + '):not(.tab_bts) ' + + '#netInspector tr:not(.' + effectiveTabClass + '):not(.tab_bts) ' + '{display:none;}', 0 ); + + updateCurrentTabTitle(); }; return function() { - var tabClass = window.location.hash.slice(1); + let tabClass = window.location.hash.slice(1); + let match = reActiveTabId.exec(tabClass); + if ( match !== null ) { + tabClass = match[1]; + activeTabId = parseInt(match[2], 10) || undefined; + window.location.hash = '#' + match[1]; + } selectRows(tabClass); if ( tabClass === lastTabClass ) { return; } lastTabClass = tabClass; - var select = uDom.nodeFromId('pageSelector'); - var option = select.querySelector('option[value="' + tabClass + '"]'); + let select = uDom.nodeFromId('pageSelector'); + let option = select.querySelector('option[value="' + tabClass + '"]'); if ( option === null ) { window.location.hash = ''; tabClass = ''; @@ -1716,6 +1746,9 @@ uDom('#netInspector table').on('click', 'tr.canMtx > td:nth-of-type(2)', popupMa uDom('#netInspector').on('click', 'tr.canLookup > td:nth-of-type(3)', reverseLookupManager.toggleOn); uDom('#netInspector').on('click', 'tr.cat_net > td:nth-of-type(4)', netFilteringManager.toggleOn); +// https://github.com/gorhill/uBlock/issues/507 +// Ensure tab selector is in sync with URL hash +pageSelectorFromURLHash(); window.addEventListener('hashchange', pageSelectorFromURLHash); /******************************************************************************/ diff --git a/src/js/messaging.js b/src/js/messaging.js index f138d30a0..93d471d09 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -1016,24 +1016,33 @@ var µb = µBlock, /******************************************************************************/ -var getLoggerData = function(ownerId, activeTabId, callback) { - var tabIds = new Map(); - for ( var entry of µb.pageStores ) { - var pageStore = entry[1]; - if ( pageStore.rawURL.startsWith(extensionOriginURL) ) { continue; } - tabIds.set(entry[0], pageStore.title); - } - if ( activeTabId && tabIds.has(activeTabId) === false ) { - activeTabId = undefined; - } - callback({ +var getLoggerData = function(details, activeTabId, callback) { + let response = { colorBlind: µb.userSettings.colorBlindFriendly, - entries: µb.logger.readAll(ownerId), + entries: µb.logger.readAll(details.ownerId), maxEntries: µb.userSettings.requestLogMaxEntries, activeTabId: activeTabId, - tabIds: Array.from(tabIds), tabIdsToken: µb.pageStoresToken - }); + }; + if ( µb.pageStoresToken !== details.tabIdsToken ) { + let tabIds = new Map(); + for ( let entry of µb.pageStores ) { + let pageStore = entry[1]; + if ( pageStore.rawURL.startsWith(extensionOriginURL) ) { continue; } + tabIds.set(entry[0], pageStore.title); + } + response.tabIds = Array.from(tabIds); + } + if ( activeTabId ) { + let pageStore = µb.pageStoreFromTabId(activeTabId); + if ( + pageStore === null || + pageStore.rawURL.startsWith(extensionOriginURL) + ) { + response.activeTabId = undefined; + } + } + callback(response); }; /******************************************************************************/ @@ -1081,7 +1090,7 @@ var onMessage = function(request, sender, callback) { return; } vAPI.tabs.get(null, function(tab) { - getLoggerData(request.ownerId, tab && tab.id, callback); + getLoggerData(request, tab && tab.id, callback); }); return; diff --git a/src/js/popup.js b/src/js/popup.js index 5e16d3183..35fc66432 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -430,15 +430,6 @@ var renderPopup = function() { } uDom.nodeFromId('total-blocked').textContent = text; - // https://github.com/gorhill/uBlock/issues/507 - // Convenience: open the logger with current tab automatically selected - if ( popupData.tabId ) { - uDom.nodeFromSelector('#basicTools > a[href^="logger-ui.html"]').setAttribute( - 'href', - 'logger-ui.html#tab_' + popupData.tabId - ); - } - // This will collate all domains, touched or not renderPrivacyExposure(); @@ -694,18 +685,24 @@ var gotoPick = function() { /******************************************************************************/ var gotoURL = function(ev) { - if ( this.hasAttribute('href') === false ) { - return; - } + if ( this.hasAttribute('href') === false ) { return; } ev.preventDefault(); + let url = this.getAttribute('href'); + if ( + url === 'logger-ui.html#tab_active' && + typeof popupData.tabId === 'number' + ) { + url += '+' + popupData.tabId; + } + messaging.send( 'popupPanel', { what: 'gotoURL', details: { - url: this.getAttribute('href'), + url: url, select: true, index: -1, shiftKey: ev.shiftKey diff --git a/src/js/tab.js b/src/js/tab.js index 177697c0e..81368053f 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -854,9 +854,9 @@ vAPI.tabs.registerListeners(); // Tab is not bound if ( pageStore === undefined ) { this.updateTitle(tabId); - this.pageStoresToken = Date.now(); pageStore = this.PageStore.factory(tabId, context); this.pageStores.set(tabId, pageStore); + this.pageStoresToken = Date.now(); return pageStore; } diff --git a/src/popup.html b/src/popup.html index dfb8089b2..872bf1d3a 100644 --- a/src/popup.html +++ b/src/popup.html @@ -18,7 +18,7 @@

- +