Browse Source

this fixes #42

pull/2/head
gorhill 10 years ago
parent
commit
056e68da66
  1. 15
      src/js/async.js
  2. 11
      src/js/messaging-handlers.js
  3. 2
      src/js/messaging.js
  4. 13
      src/js/pagestats.js
  5. 4
      src/js/popup.js
  6. 18
      src/js/tab.js
  7. 28
      src/js/traffic.js

15
src/js/async.js

@ -138,15 +138,8 @@ return asyncJobManager;
// Cache callback definition, it was a bad idea to define this one inside // Cache callback definition, it was a bad idea to define this one inside
// updateBadgeAsync // updateBadgeAsync
var updateBadge = function(pageUrl) {
var updateBadge = function(tabId) {
var µm = µMatrix; var µm = µMatrix;
if ( pageUrl === µm.behindTheSceneURL ) {
return;
}
var tabId = µm.tabIdFromPageUrl(pageUrl);
if ( !tabId ) {
return;
}
var pageStore = µm.pageStatsFromTabId(tabId); var pageStore = µm.pageStatsFromTabId(tabId);
if ( pageStore ) { if ( pageStore ) {
pageStore.updateBadge(tabId); 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; return;
} }
µm.asyncJobs.add('updateBadge-' + pageUrl, pageUrl, updateBadge, 250);
µm.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250);
}; };
return updateBadgeAsync; return updateBadgeAsync;

11
src/js/messaging-handlers.js

@ -20,6 +20,7 @@
*/ */
/* global chrome, µMatrix */ /* 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: // TODO: Investigate "Error in response to tabs.executeScript: TypeError:
// Cannot read property 'locationURL' of null" (2013-11-12). When can this // Cannot read property 'locationURL' of null" (2013-11-12). When can this
// happens? // happens?
if ( !details || !details.locationURL ) { if ( !details || !details.locationURL ) {
return; return;
} }
var pageURL = µm.pageUrlFromTabId(sender.tab.id);
var pageURL = µm.pageUrlFromTabId(tabId);
var pageStats = µm.pageStatsFromPageUrl(pageURL); var pageStats = µm.pageStatsFromPageUrl(pageURL);
var µmuri = µm.URI.set(details.locationURL); var µmuri = µm.URI.set(details.locationURL);
var frameURL = µmuri.normalizedURI(); var frameURL = µmuri.normalizedURI();
@ -363,16 +364,20 @@ var onMessage = function(request, sender, callback) {
break; break;
} }
var tabId = sender.tab.id;
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'contentScriptHasLocalStorage': case 'contentScriptHasLocalStorage':
response = contentScriptLocalStorageHandler(request.url); response = contentScriptLocalStorageHandler(request.url);
µm.updateBadgeAsync(tabId);
break; break;
case 'contentScriptSummary': case 'contentScriptSummary':
contentScriptSummaryHandler(request, sender);
contentScriptSummaryHandler(tabId, request);
µm.updateBadgeAsync(tabId);
break; break;
case 'checkScriptBlacklisted': case 'checkScriptBlacklisted':

2
src/js/messaging.js

@ -150,7 +150,7 @@ function defaultHandler(request, sender, callback) {
switch ( request.what ) { switch ( request.what ) {
case 'forceReloadTab': case 'forceReloadTab':
µm.forceReload(request.pageURL);
µm.forceReload(request.tabId);
break; break;
case 'getUserSettings': case 'getUserSettings':

13
src/js/pagestats.js

@ -20,7 +20,7 @@
*/ */
/* global chrome, µMatrix */ /* 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) { 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 // Count blocked/allowed requests
this.requestStats.record(type, block); this.requestStats.record(type, block);

4
src/js/popup.js

@ -649,7 +649,7 @@ function makeMatrixMetaRow(totals) {
function computeMatrixGroupMetaStats(group) { function computeMatrixGroupMetaStats(group) {
var headers = matrixSnapshot.headers; var headers = matrixSnapshot.headers;
var n = Object.keys(headers).length
var n = Object.keys(headers).length;
var totals = new Array(n); var totals = new Array(n);
var i = n; var i = n;
while ( i-- ) { while ( i-- ) {
@ -1108,7 +1108,7 @@ function revertAll() {
function buttonReloadHandler() { function buttonReloadHandler() {
messaging.tell({ messaging.tell({
what: 'forceReloadTab', what: 'forceReloadTab',
pageURL: matrixSnapshot.url
tabId: targetTabId
}); });
} }

18
src/js/tab.js

@ -139,7 +139,7 @@
} }
// https://github.com/gorhill/uMatrix/issues/37 // https://github.com/gorhill/uMatrix/issues/37
this.updateBadgeAsync(pageURL);
this.updateBadgeAsync(tabId);
this.unbindTabFromPageStats(tabId); this.unbindTabFromPageStats(tabId);
@ -183,6 +183,7 @@
var pageStats = this.pageStatsFromTabId(tabId); var pageStats = this.pageStatsFromTabId(tabId);
if ( pageStats ) { if ( pageStats ) {
pageStats.recordRequest(type, url, blocked); 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) { µMatrix.pageUrlFromTabId = function(tabId) {
return this.tabIdToPageUrl[tabId]; return this.tabIdToPageUrl[tabId];
}; };
@ -406,11 +397,8 @@
/******************************************************************************/ /******************************************************************************/
µMatrix.forceReload = function(pageURL) {
var tabId = this.tabIdFromPageUrl(pageURL);
if ( tabId ) {
µMatrix.forceReload = function(tabId) {
chrome.tabs.reload(tabId, { bypassCache: true }); chrome.tabs.reload(tabId, { bypassCache: true });
}
}; };
/******************************************************************************/ /******************************************************************************/

28
src/js/traffic.js

@ -20,6 +20,7 @@
*/ */
/* global chrome, µMatrix */ /* global chrome, µMatrix */
/* jshint boss: true */
/******************************************************************************/ /******************************************************************************/
@ -239,6 +240,8 @@ var onBeforeRootFrameRequestHandler = function(details) {
// https://github.com/gorhill/httpswitchboard/issues/112 // https://github.com/gorhill/httpswitchboard/issues/112
pageStore.recordRequest('doc', requestURL, block); pageStore.recordRequest('doc', requestURL, block);
µm.updateBadgeAsync(tabId);
// If it's a blacklisted frame, redirect to frame.html // If it's a blacklisted frame, redirect to frame.html
// rhill 2013-11-05: The root frame contains a link to noop.css, this // 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 // 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. // to scope on unknown scheme? Etc.
// https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/191
// https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 // 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 ) { if ( !pageStore ) {
pageStore = µm.pageStatsFromTabId(µm.behindTheSceneTabId); pageStore = µm.pageStatsFromTabId(µm.behindTheSceneTabId);
} }
@ -329,6 +333,8 @@ var onBeforeRequestHandler = function(details) {
// it is available. // it is available.
pageStore.recordRequest(requestType, requestURL, block); pageStore.recordRequest(requestType, requestURL, block);
µm.updateBadgeAsync(tabId);
// whitelisted? // whitelisted?
if ( !block ) { if ( !block ) {
// console.debug('onBeforeRequestHandler()> ALLOW "%s": %o', details.url, details); // console.debug('onBeforeRequestHandler()> ALLOW "%s": %o', details.url, details);
@ -394,6 +400,7 @@ var onBeforeSendHeadersHandler = function(details) {
if ( linkAuditor ) { if ( linkAuditor ) {
var block = µm.userSettings.processHyperlinkAuditing; var block = µm.userSettings.processHyperlinkAuditing;
pageStore.recordRequest('other', requestURL + '{Ping-To:' + linkAuditor + '}', block); pageStore.recordRequest('other', requestURL + '{Ping-To:' + linkAuditor + '}', block);
µm.updateBadgeAsync(tabId);
if ( block ) { if ( block ) {
µm.hyperlinkAuditingFoiledCounter += 1; µm.hyperlinkAuditingFoiledCounter += 1;
return { 'cancel': true }; 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 foilCookieHeaders = function(µm, details) {
var changed = false; var changed = false;
var headers = details.requestHeaders; var headers = details.requestHeaders;
@ -647,6 +637,7 @@ var onMainDocHeadersReceived = function(details) {
while ( destinationURL = mainFrameStack.pop() ) { while ( destinationURL = mainFrameStack.pop() ) {
pageStats.recordRequest('doc', destinationURL, false); pageStats.recordRequest('doc', destinationURL, false);
} }
µm.updateBadgeAsync(tabId);
} }
// Maybe modify inbound headers // Maybe modify inbound headers
@ -775,6 +766,7 @@ var onErrorOccurredHandler = function(details) {
while ( destinationURL = mainFrameStack.pop() ) { while ( destinationURL = mainFrameStack.pop() ) {
pageStats.recordRequest('doc', destinationURL, false); pageStats.recordRequest('doc', destinationURL, false);
} }
µm.updateBadgeAsync(details.tabId);
}; };
/******************************************************************************/ /******************************************************************************/

Loading…
Cancel
Save