From 056e68da66f112f47d0b272df55de8afd0c159d6 Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 10 Apr 2015 10:45:40 -0400 Subject: [PATCH] this fixes #42 --- src/js/async.js | 15 ++++----------- src/js/messaging-handlers.js | 11 ++++++++--- src/js/messaging.js | 2 +- src/js/pagestats.js | 13 +------------ src/js/popup.js | 4 ++-- src/js/tab.js | 20 ++++---------------- src/js/traffic.js | 28 ++++++++++------------------ 7 files changed, 30 insertions(+), 63 deletions(-) diff --git a/src/js/async.js b/src/js/async.js index f9b065f..b6cc7ae 100644 --- a/src/js/async.js +++ b/src/js/async.js @@ -138,15 +138,8 @@ return asyncJobManager; // Cache callback definition, it was a bad idea to define this one inside // updateBadgeAsync - var updateBadge = function(pageUrl) { + var updateBadge = function(tabId) { var µm = µMatrix; - if ( pageUrl === µm.behindTheSceneURL ) { - return; - } - var tabId = µm.tabIdFromPageUrl(pageUrl); - if ( !tabId ) { - return; - } var pageStore = µm.pageStatsFromTabId(tabId); if ( pageStore ) { pageStore.updateBadge(tabId); @@ -159,11 +152,11 @@ return asyncJobManager; ); }; - var updateBadgeAsync = function(pageUrl) { - if ( typeof pageUrl !== 'string' || pageUrl === '' ) { + var updateBadgeAsync = function(tabId) { + if ( typeof tabId !== 'number' || tabId <= 0 ) { return; } - µm.asyncJobs.add('updateBadge-' + pageUrl, pageUrl, updateBadge, 250); + µm.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250); }; return updateBadgeAsync; diff --git a/src/js/messaging-handlers.js b/src/js/messaging-handlers.js index 7244469..876871b 100644 --- a/src/js/messaging-handlers.js +++ b/src/js/messaging-handlers.js @@ -20,6 +20,7 @@ */ /* global chrome, µMatrix */ +/* jshint boss: true */ /******************************************************************************/ /******************************************************************************/ @@ -282,14 +283,14 @@ var µm = µMatrix; /******************************************************************************/ -var contentScriptSummaryHandler = function(details, sender) { +var contentScriptSummaryHandler = function(tabId, details) { // TODO: Investigate "Error in response to tabs.executeScript: TypeError: // Cannot read property 'locationURL' of null" (2013-11-12). When can this // happens? if ( !details || !details.locationURL ) { return; } - var pageURL = µm.pageUrlFromTabId(sender.tab.id); + var pageURL = µm.pageUrlFromTabId(tabId); var pageStats = µm.pageStatsFromPageUrl(pageURL); var µmuri = µm.URI.set(details.locationURL); var frameURL = µmuri.normalizedURI(); @@ -363,16 +364,20 @@ var onMessage = function(request, sender, callback) { break; } + var tabId = sender.tab.id; + // Sync var response; switch ( request.what ) { case 'contentScriptHasLocalStorage': response = contentScriptLocalStorageHandler(request.url); + µm.updateBadgeAsync(tabId); break; case 'contentScriptSummary': - contentScriptSummaryHandler(request, sender); + contentScriptSummaryHandler(tabId, request); + µm.updateBadgeAsync(tabId); break; case 'checkScriptBlacklisted': diff --git a/src/js/messaging.js b/src/js/messaging.js index d9d13c6..2becf2c 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -150,7 +150,7 @@ function defaultHandler(request, sender, callback) { switch ( request.what ) { case 'forceReloadTab': - µm.forceReload(request.pageURL); + µm.forceReload(request.tabId); break; case 'getUserSettings': diff --git a/src/js/pagestats.js b/src/js/pagestats.js index 4aaa54f..4adb1ea 100644 --- a/src/js/pagestats.js +++ b/src/js/pagestats.js @@ -20,7 +20,7 @@ */ /* global chrome, µMatrix */ -/* jshint bitwise: false */ +/* jshint bitwise: false, boss: true */ /******************************************************************************* @@ -498,17 +498,6 @@ PageStore.prototype.dispose = function() { /******************************************************************************/ PageStore.prototype.recordRequest = function(type, url, block) { - // TODO: this makes no sense, I forgot why I put this here. - if ( !this ) { - // console.error('pagestats.js > PageStore.recordRequest(): no pageStats'); - return; - } - - // rhill 2013-10-26: This needs to be called even if the request is - // already logged, since the request stats are cached for a while after - // the page is no longer visible in a browser tab. - µm.updateBadgeAsync(this.pageUrl); - // Count blocked/allowed requests this.requestStats.record(type, block); diff --git a/src/js/popup.js b/src/js/popup.js index 66c0f15..4fd42d5 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -649,7 +649,7 @@ function makeMatrixMetaRow(totals) { function computeMatrixGroupMetaStats(group) { var headers = matrixSnapshot.headers; - var n = Object.keys(headers).length + var n = Object.keys(headers).length; var totals = new Array(n); var i = n; while ( i-- ) { @@ -1108,7 +1108,7 @@ function revertAll() { function buttonReloadHandler() { messaging.tell({ what: 'forceReloadTab', - pageURL: matrixSnapshot.url + tabId: targetTabId }); } diff --git a/src/js/tab.js b/src/js/tab.js index fd3ff25..d178a61 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -139,7 +139,7 @@ } // https://github.com/gorhill/uMatrix/issues/37 - this.updateBadgeAsync(pageURL); + this.updateBadgeAsync(tabId); this.unbindTabFromPageStats(tabId); @@ -183,6 +183,7 @@ var pageStats = this.pageStatsFromTabId(tabId); if ( pageStats ) { pageStats.recordRequest(type, url, blocked); + this.updateBadgeAsync(tabId); } }; @@ -357,16 +358,6 @@ /******************************************************************************/ -µMatrix.tabIdFromPageUrl = function(pageURL) { - // https://github.com/gorhill/httpswitchboard/issues/303 - // Normalize to a page-URL. - return this.pageUrlToTabId[this.normalizePageURL(pageURL)]; -}; - -µMatrix.tabIdFromPageStats = function(pageStats) { - return this.tabIdFromPageUrl(pageStats.pageUrl); -}; - µMatrix.pageUrlFromTabId = function(tabId) { return this.tabIdToPageUrl[tabId]; }; @@ -406,11 +397,8 @@ /******************************************************************************/ -µMatrix.forceReload = function(pageURL) { - var tabId = this.tabIdFromPageUrl(pageURL); - if ( tabId ) { - chrome.tabs.reload(tabId, { bypassCache: true }); - } +µMatrix.forceReload = function(tabId) { + chrome.tabs.reload(tabId, { bypassCache: true }); }; /******************************************************************************/ diff --git a/src/js/traffic.js b/src/js/traffic.js index 6e63981..a00b506 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -20,6 +20,7 @@ */ /* global chrome, µMatrix */ +/* jshint boss: true */ /******************************************************************************/ @@ -239,6 +240,8 @@ var onBeforeRootFrameRequestHandler = function(details) { // https://github.com/gorhill/httpswitchboard/issues/112 pageStore.recordRequest('doc', requestURL, block); + µm.updateBadgeAsync(tabId); + // If it's a blacklisted frame, redirect to frame.html // rhill 2013-11-05: The root frame contains a link to noop.css, this // allows to later check whether the root frame has been unblocked by the @@ -313,7 +316,8 @@ var onBeforeRequestHandler = function(details) { // to scope on unknown scheme? Etc. // https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 - var pageStore = µm.pageStatsFromTabId(details.tabId); + var tabId = details.tabId; + var pageStore = µm.pageStatsFromTabId(tabId); if ( !pageStore ) { pageStore = µm.pageStatsFromTabId(µm.behindTheSceneTabId); } @@ -329,6 +333,8 @@ var onBeforeRequestHandler = function(details) { // it is available. pageStore.recordRequest(requestType, requestURL, block); + µm.updateBadgeAsync(tabId); + // whitelisted? if ( !block ) { // console.debug('onBeforeRequestHandler()> ALLOW "%s": %o', details.url, details); @@ -394,6 +400,7 @@ var onBeforeSendHeadersHandler = function(details) { if ( linkAuditor ) { var block = µm.userSettings.processHyperlinkAuditing; pageStore.recordRequest('other', requestURL + '{Ping-To:' + linkAuditor + '}', block); + µm.updateBadgeAsync(tabId); if ( block ) { µm.hyperlinkAuditingFoiledCounter += 1; return { 'cancel': true }; @@ -461,23 +468,6 @@ var hyperlinkAuditorFromHeaders = function(headers) { /******************************************************************************/ -var tabIdFromHeaders = function(µm, headers) { - var header; - var i = headers.length; - while ( i-- ) { - header = headers[i]; - if ( header.name.toLowerCase() === 'referer' ) { - return µm.tabIdFromPageUrl(header.value); - } - if ( header.name.toLowerCase() === 'ping-from' ) { - return µm.tabIdFromPageUrl(header.value); - } - } - return -1; -}; - -/******************************************************************************/ - var foilCookieHeaders = function(µm, details) { var changed = false; var headers = details.requestHeaders; @@ -647,6 +637,7 @@ var onMainDocHeadersReceived = function(details) { while ( destinationURL = mainFrameStack.pop() ) { pageStats.recordRequest('doc', destinationURL, false); } + µm.updateBadgeAsync(tabId); } // Maybe modify inbound headers @@ -775,6 +766,7 @@ var onErrorOccurredHandler = function(details) { while ( destinationURL = mainFrameStack.pop() ) { pageStats.recordRequest('doc', destinationURL, false); } + µm.updateBadgeAsync(details.tabId); }; /******************************************************************************/