From 23aa1e79cb9b28df716e00ba793438cad2bc65d9 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 18 May 2015 07:32:25 -0400 Subject: [PATCH] remove overhead of tab selector update when no change --- src/js/background.js | 1 + src/js/contentscript-end.js | 9 +----- src/js/logger-ui.js | 13 ++++---- src/js/messaging.js | 61 +++++++++++++++++------------------ src/js/tab.js | 63 +++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 46 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 15ef63e..b477328 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -140,6 +140,7 @@ return { // urls stats are kept on the back burner while waiting to be reactivated // in a tab or another. pageStores: {}, + pageStoresToken: 0, pageStoreCemetery: {}, // page url => permission scope diff --git a/src/js/contentscript-end.js b/src/js/contentscript-end.js index 3b93419..7345ff5 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -445,8 +445,7 @@ var nodeListsAddedHandler = function(nodeLists) { what: 'contentScriptSummary', locationURL: window.location.href, inlineScript: false, - mustReport: true, - title: '' + mustReport: true }; // https://github.com/gorhill/httpswitchboard/issues/25 // & @@ -454,12 +453,6 @@ var nodeListsAddedHandler = function(nodeLists) { // https://github.com/gorhill/httpswitchboard/issues/131 hasInlineScript(document.querySelectorAll('a[href^="javascript:"],script'), summary); - if ( window === window.top ) { - if ( document.title ) { - summary.title = document.title.trim(); - } - } - //console.debug('contentscript-end.js > firstObservationHandler(): found %d script tags in "%s"', Object.keys(summary.scriptSources).length, window.location.href); localMessager.send(summary); diff --git a/src/js/logger-ui.js b/src/js/logger-ui.js index 80504e2..a6ea58c 100644 --- a/src/js/logger-ui.js +++ b/src/js/logger-ui.js @@ -39,6 +39,7 @@ var lastVarDataIndex = 3; // currently, d0-d3 var maxEntries = 0; var noTabId = ''; var allTabIds = {}; +var allTabIdsToken; var emphasizeTemplate = document.querySelector('#emphasizeTemplate > span'); var hiddenTemplate = document.querySelector('#hiddenTemplate > span'); @@ -349,10 +350,6 @@ var renderLogEntries = function(response) { var synchronizeTabIds = function(newTabIds) { var oldTabIds = allTabIds; - - // Neuter rows for which a tab does not exist anymore - // TODO: sort to avoid using indexOf - var autoDeleteVoidRows = !!vAPI.localStorage.getItem('loggerAutoDeleteVoidRows'); var rowVoided = false; var trs; @@ -445,8 +442,12 @@ var onLogBufferRead = function(response) { } // Neuter rows for which a tab does not exist anymore - // TODO: sort to avoid using indexOf - var rowVoided = synchronizeTabIds(response.tabIds); + var rowVoided = false; + if ( response.tabIdsToken !== allTabIdsToken ) { + rowVoided = synchronizeTabIds(response.tabIds); + allTabIdsToken = response.tabIdsToken; + } + renderLogEntries(response); if ( rowVoided ) { diff --git a/src/js/messaging.js b/src/js/messaging.js index cb69dfb..bf64f6a 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -391,18 +391,14 @@ var contentScriptSummaryHandler = function(tabId, details) { return; } - // https://github.com/gorhill/httpswitchboard/issues/25 - var pageStore = µm.pageStoreFromTabId(tabId); - if ( pageStore === null ) { + // scripts + if ( details.inlineScript !== true ) { return; } - if ( details.title ) { - pageStore.title = details.title; - } - - // scripts - if ( details.inlineScript !== true ) { + // https://github.com/gorhill/httpswitchboard/issues/25 + var pageStore = µm.pageStoreFromTabId(tabId); + if ( pageStore === null ) { return; } @@ -907,31 +903,32 @@ var onMessage = function(request, sender, callback) { var response; switch ( request.what ) { - case 'readMany': - var tabIds = {}; - var loggerURL = vAPI.getURL('logger-ui.html'); - var pageStore; - for ( var tabId in µm.pageStores ) { - pageStore = µm.pageStoreFromTabId(tabId); - if ( pageStore === null ) { - continue; - } - if ( pageStore.rawUrl.lastIndexOf(loggerURL, 0) === 0 ) { - continue; - } - tabIds[tabId] = pageStore.title || pageStore.rawUrl; + case 'readMany': + var tabIds = {}; + var loggerURL = vAPI.getURL('logger-ui.html'); + var pageStore; + for ( var tabId in µm.pageStores ) { + pageStore = µm.pageStoreFromTabId(tabId); + if ( pageStore === null ) { + continue; } - response = { - colorBlind: false, - entries: µm.logger.readAll(), - maxLoggedRequests: µm.userSettings.maxLoggedRequests, - noTabId: vAPI.noTabId, - tabIds: tabIds - }; - break; + if ( pageStore.rawUrl.lastIndexOf(loggerURL, 0) === 0 ) { + continue; + } + tabIds[tabId] = pageStore.title || pageStore.rawUrl; + } + response = { + colorBlind: false, + entries: µm.logger.readAll(), + maxLoggedRequests: µm.userSettings.maxLoggedRequests, + noTabId: vAPI.noTabId, + tabIds: tabIds, + tabIdsToken: µm.pageStoresToken + }; + break; - default: - return vAPI.messaging.UNHANDLED; + default: + return vAPI.messaging.UNHANDLED; } callback(response); diff --git a/src/js/tab.js b/src/js/tab.js index b902c9f..b040d97 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -481,6 +481,8 @@ vAPI.tabs.registerListeners(); if ( context === 'updated' && pageStore.pageHostname === tabContext.rootHostname ) { pageStore.rawURL = tabContext.rawURL; pageStore.normalURL = normalURL; + this.updateTitle(tabId); + this.pageStoresToken = Date.now(); return pageStore; } @@ -494,6 +496,8 @@ vAPI.tabs.registerListeners(); pageStore = this.PageStore.factory(tabContext); } this.pageStores[tabId] = pageStore; + this.updateTitle(tabId); + this.pageStoresToken = Date.now(); // console.debug('tab.js > bindTabToPageStats(): dispatching traffic in tab id %d to page store "%s"', tabId, pageUrl); @@ -513,6 +517,7 @@ vAPI.tabs.registerListeners(); return; } delete this.pageStores[tabId]; + this.pageStoresToken = Date.now(); if ( pageStore.incinerationTimer ) { clearTimeout(pageStore.incinerationTimer); @@ -618,6 +623,64 @@ vAPI.tabs.registerListeners(); /******************************************************************************/ +µm.updateTitle = (function() { + var tabIdToTimer = Object.create(null); + var tabIdToTryCount = Object.create(null); + var delay = 499; + + var tryNoMore = function(tabId) { + delete tabIdToTryCount[tabId]; + }; + + var tryAgain = function(tabId) { + var count = tabIdToTryCount[tabId]; + if ( count === undefined ) { + return false; + } + if ( count === 1 ) { + delete tabIdToTryCount[tabId]; + return false; + } + tabIdToTryCount[tabId] = count - 1; + tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(µb, tabId), delay); + return true; + }; + + var onTabReady = function(tabId, tab) { + if ( !tab ) { + return tryNoMore(tabId); + } + var pageStore = this.pageStoreFromTabId(tabId); + if ( pageStore === null ) { + return tryNoMore(tabId); + } + if ( !tab.title && tryAgain(tabId) ) { + return; + } + tryNoMore(tabId); + pageStore.title = tab.title || tab.url || ''; + this.pageStoresToken = Date.now(); + }; + + var updateTitle = function(tabId) { + delete tabIdToTimer[tabId]; + vAPI.tabs.get(tabId, onTabReady.bind(this, tabId)); + }; + + return function(tabId) { + if ( vAPI.isBehindTheSceneTabId(tabId) ) { + return; + } + if ( tabIdToTimer[tabId] ) { + clearTimeout(tabIdToTimer[tabId]); + } + tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(this, tabId), delay); + tabIdToTryCount[tabId] = 5; + }; +})(); + +/******************************************************************************/ + // Stale page store entries janitor // https://github.com/chrisaljoudi/uBlock/issues/455