Skip to content

Commit

Permalink
this fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed May 16, 2015
1 parent 20f5794 commit e0284b8
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@
"message":"No non-blocked requests logged for this page",
"description":"English: No non-blocked requests logged for this page"
},
"logAll":{
"message":"All",
"description":"Appears in the logger's tab selector"
},
"logBehindTheScene":{
"message":"Behind the scene",
"description":"Pretty name for behind-the-scene network requests"
Expand Down
21 changes: 16 additions & 5 deletions src/css/logger-ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ body {
margin: 0;
overflow-x: hidden;
padding: 0;
white-space: nowrap;
width: 100%;
}
#toolbar {
Expand All @@ -16,7 +15,7 @@ body {
box-sizing: border-box;
left: 0;
margin: 0;
padding: 0 1em;
padding: 0.5em 1em;
position: fixed;
top: 0;
width: 100%;
Expand All @@ -28,7 +27,7 @@ body {
box-sizing: border-box;
cursor: pointer;
display: inline-block;
font-size: 20px;
font-size: 150%;
margin: 0;
padding: 8px;
}
Expand All @@ -39,6 +38,19 @@ body {
#toolbar .button:hover {
background-color: #eee;
}
#toolbar > div {
white-space: nowrap;
}
#toolbar > div:first-of-type {
font-size: 120%;
}
#toolbar > div > * {
vertical-align: middle;
}
#pageSelector {
width: 28em;
padding: 0.2em 0;
}
body #compactViewToggler.button:before {
content: '\f102';
}
Expand All @@ -55,14 +67,13 @@ body.f #filterButton {
background-color: #fee;
}
#maxEntries {
margin-left: 3em;
margin: 0 2em;
}
input:focus {
background-color: #ffe;
}
#content {
font: 13px sans-serif;
margin-top: 3.5em;
width: 100%;
}

Expand Down
171 changes: 144 additions & 27 deletions src/js/logger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@

/******************************************************************************/

// Adjust top padding of content table, to match that of toolbar height.

document.getElementById('content').style.setProperty(
'margin-top',
document.getElementById('toolbar').offsetHeight + 'px'
);

/******************************************************************************/

var messager = vAPI.messaging.channel('logger-ui.js');
var tbody = document.querySelector('#content tbody');
var trJunkyard = [];
Expand All @@ -40,6 +49,8 @@ var maxEntries = 5000;
var noTabId = '';
var allTabIds = {};

var hiddenTemplate = document.querySelector('#hiddenTemplate > span');

var prettyRequestTypes = {
'main_frame': 'doc',
'stylesheet': 'css',
Expand All @@ -61,6 +72,18 @@ var dateOptions = {

/******************************************************************************/

var classNameFromTabId = function(tabId) {
if ( tabId === noTabId ) {
return 'tab_bts';
}
if ( tabId !== '' ) {
return 'tab_' + tabId;
}
return '';
};

/******************************************************************************/

// Emphasize hostname in URL, as this is what matters in uMatrix's rules.

var nodeFromURL = function(url, filter) {
Expand Down Expand Up @@ -165,6 +188,14 @@ var createRow = function(layout) {

/******************************************************************************/

var createHiddenTextNode = function(text) {
var node = hiddenTemplate.cloneNode(true);
node.textContent = text;
return node;
};

/******************************************************************************/

var createGap = function(tabId, url) {
var tr = createRow('1');
tr.classList.add('tab');
Expand Down Expand Up @@ -246,11 +277,9 @@ var renderLogEntry = function(entry) {
tr.cells[0].title = time.toLocaleDateString('fullwide', dateOptions);

if ( entry.tab ) {
tr.classList.add('tab');
tr.classList.add('tab', classNameFromTabId(entry.tab));
if ( entry.tab === noTabId ) {
tr.classList.add('tab_bts');
} else if ( entry.tab !== '' ) {
tr.classList.add('tab_' + entry.tab);
tr.cells[1].appendChild(createHiddenTextNode('bts'));
}
}
if ( entry.cat !== '' ) {
Expand Down Expand Up @@ -316,6 +345,78 @@ 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;
for ( var tabId in oldTabIds ) {
if ( oldTabIds.hasOwnProperty(tabId) === false ) {
continue;
}
if ( newTabIds.hasOwnProperty(tabId) ) {
continue;
}
// Mark or remove voided rows
trs = uDom('.tab_' + tabId);
if ( autoDeleteVoidRows ) {
toJunkyard(trs);
} else {
trs.removeClass('canMtx');
rowVoided = true;
}
// Remove popup if it is currently bound to a removed tab.
if ( tabId === popupManager.tabId ) {
popupManager.toggleOff();
}
}

var select = document.getElementById('pageSelector');
var selectValue = select.value;
var tabIds = Object.keys(newTabIds).sort(function(a, b) {
return newTabIds[a].localeCompare(newTabIds[b]);
});
var option;
for ( var i = 0, j = 2; i < tabIds.length; i++ ) {
tabId = tabIds[i];
if ( tabId === noTabId ) {
continue;
}
option = select.options[j];
j += 1;
if ( !option ) {
option = document.createElement('option');
select.appendChild(option);
}
option.textContent = newTabIds[tabId];
option.value = classNameFromTabId(tabId);
if ( option.value === selectValue ) {
option.setAttribute('selected', '');
} else {
option.removeAttribute('selected');
}
}
while ( j < select.options.length ) {
select.removeChild(select.options[j]);
}
if ( select.value !== selectValue ) {
select.selectedIndex = 0;
select.value = '';
select.options[0].setAttribute('selected', '');
pageSelectorChanged();
}

allTabIds = newTabIds;

return rowVoided;
};

/******************************************************************************/

var truncateLog = function(size) {
if ( size === 0 ) {
size = 5000;
Expand Down Expand Up @@ -343,28 +444,7 @@ var onLogBufferRead = function(response) {

// 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, trs;
for ( var tabId in allTabIds ) {
if ( allTabIds.hasOwnProperty(tabId) === false ) {
continue;
}
if ( response.tabIds.hasOwnProperty(tabId) ) {
continue;
}
trs = uDom('.tab_' + tabId);
if ( autoDeleteVoidRows ) {
toJunkyard(trs);
} else {
trs.removeClass('canMtx');
rowVoided = true;
}
if ( tabId === popupManager.tabId ) {
popupManager.toggleOff();
}
}
allTabIds = response.tabIds;

var rowVoided = synchronizeTabIds(response.tabIds);
renderLogEntries(response);

if ( rowVoided ) {
Expand Down Expand Up @@ -395,6 +475,41 @@ var readLogBuffer = function() {

/******************************************************************************/

var pageSelectorChanged = function() {
var style = document.getElementById('tabFilterer');
var tabClass = document.getElementById('pageSelector').value;
var sheet = style.sheet;
while ( sheet.cssRules.length !== 0 ) {
sheet.deleteRule(0);
}
if ( tabClass !== '' ) {
sheet.insertRule(
'#content table tr:not(.' + tabClass + ') { display: none; }',
0
);
}
uDom('#refresh').toggleClass(
'disabled',
tabClass === '' || tabClass === 'tab_bts'
);
};

/******************************************************************************/

var reloadTab = function() {
var tabClass = document.getElementById('pageSelector').value;
var matches = tabClass.match(/^tab_(.+)$/);
if ( matches === null ) {
return;
}
if ( matches[1] === 'bts' ) {
return;
}
messager.send({ what: 'reloadTab', tabId: matches[1] });
};

/******************************************************************************/

var onMaxEntriesChanged = function() {
var raw = uDom(this).val();
try {
Expand Down Expand Up @@ -649,7 +764,7 @@ var popupManager = (function() {
popupObserver = new MutationObserver(resizePopup);
container.appendChild(popup);

style = document.querySelector('#content > style');
style = document.getElementById('popupFilterer');
style.textContent = styleTemplate.replace('{{tabId}}', localTabId);

document.body.classList.add('popupOn');
Expand Down Expand Up @@ -701,6 +816,8 @@ var popupManager = (function() {
uDom.onLoad(function() {
readLogBuffer();

uDom('#pageSelector').on('change', pageSelectorChanged);
uDom('#refresh').on('click', reloadTab);
uDom('#compactViewToggler').on('click', toggleCompactView);
uDom('#clean').on('click', cleanBuffer);
uDom('#clear').on('click', clearBuffer);
Expand Down
12 changes: 9 additions & 3 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,17 @@ var onMessage = function(request, sender, callback) {

switch ( request.what ) {
case 'readAll':
var tabIds = {};
var tabIds = {}, pageStore;
var loggerURL = vAPI.getURL('logger-ui.html');
for ( var tabId in µb.pageStores ) {
if ( µb.pageStores.hasOwnProperty(tabId) ) {
tabIds[tabId] = true;
pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
continue;
}
if ( pageStore.rawURL.lastIndexOf(loggerURL, 0) === 0 ) {
continue;
}
tabIds[tabId] = pageStore.title;
}
response = {
colorBlind: µb.userSettings.colorBlindFriendly,
Expand Down
6 changes: 6 additions & 0 deletions src/js/pagestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ PageStore.prototype.init = function(tabId) {
var tabContext = µb.tabContextManager.lookup(tabId);
this.tabId = tabId;
this.tabHostname = tabContext.rootHostname;
this.title = tabContext.rawURL;
this.rawURL = tabContext.rawURL;
this.hostnameToCountMap = {};
this.contentLastModified = 0;
this.frames = {};
Expand Down Expand Up @@ -334,6 +336,7 @@ PageStore.prototype.reuse = function(context) {
if ( context === 'tabUpdated' ) {
// As part of https://github.com/chrisaljoudi/uBlock/issues/405
// URL changed, force a re-evaluation of filtering switch
this.rawURL = tabContext.rawURL;
this.netFilteringReadTime = 0;
return this;
}
Expand All @@ -354,6 +357,9 @@ PageStore.prototype.dispose = function() {
// need to release the memory taken by these, which can amount to
// sizeable enough chunks (especially requests, through the request URL
// used as a key).
this.tabHostname = '';
this.title = '';
this.rawURL = '';
this.hostnameToCountMap = null;
this.disposeFrameStores();
this.netFilteringCache = this.netFilteringCache.dispose();
Expand Down
Loading

0 comments on commit e0284b8

Please sign in to comment.