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
This commit is contained in:
Raymond Hill 2018-05-27 08:31:17 -04:00
parent 956e2161aa
commit c6cab02999
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
6 changed files with 112 additions and 73 deletions

View File

@ -73,7 +73,7 @@ var µBlock = (function() { // jshint ignore:line
userSettings: {
advancedUserEnabled: false,
alwaysDetachLogger: false,
alwaysDetachLogger: true,
autoUpdate: true,
cloudStorageEnabled: false,
collapseBlocked: true,

View File

@ -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);
/******************************************************************************/

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -18,7 +18,7 @@
<p id="basicTools">
<span id="gotoZap" class="fa tool" data-i18n-tip="popupTipZapper" data-tip-position="under">&#xf0e7;</span>
<span id="gotoPick" class="fa tool" data-i18n-tip="popupTipPicker" data-tip-position="under">&#xf1fb;</span>
<a href="logger-ui.html" class="fa tool enabled" aria-label="data-tip" data-i18n-tip="popupTipLog" data-tip-position="under" target="uBOLogger" tabindex="0">&#xf022;</a>
<a href="logger-ui.html#tab_active" class="fa tool enabled" aria-label="data-tip" data-i18n-tip="popupTipLog" data-tip-position="under" target="uBOLogger" tabindex="0">&#xf022;</a>
<a href="dashboard.html" class="fa tool enabled" aria-label="data-tip" data-i18n-tip="popupTipDashboard" data-tip-position="under" target="uBODashboard" tabindex="0">&#xf1de;</a>
</p>
<h2 id="dfToggler" data-i18n="popupBlockedRequestPrompt">&nbsp;</h2>