From d610c87a53736071e1d7c8c7252268b6ceeaae4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs?= Date: Sat, 21 Sep 2019 20:55:00 -0500 Subject: Improve syntax --- js/logger-ui.js | 371 +++++++++++++++++++++++++++----------------------------- 1 file changed, 177 insertions(+), 194 deletions(-) (limited to 'js/logger-ui.js') diff --git a/js/logger-ui.js b/js/logger-ui.js index cb8e4f1..590e67b 100644 --- a/js/logger-ui.js +++ b/js/logger-ui.js @@ -27,14 +27,14 @@ /******************************************************************************/ -(function () { +(function() { /******************************************************************************/ var tbody = document.querySelector('#content tbody'); var trJunkyard = []; var tdJunkyard = []; - var firstVarDataCol = 2; // currently, column 2 (0-based index) + var firstVarDataCol = 2; // currently, column 2 (0-based index) var lastVarDataIndex = 3; // currently, d0-d3 var maxEntries = 0; var noTabId = ''; @@ -69,11 +69,11 @@ /******************************************************************************/ - var classNameFromTabId = function (tabId) { - if (tabId === noTabId) { + var classNameFromTabId = function(tabId) { + if ( tabId === noTabId ) { return 'tab_bts'; } - if (tabId !== '') { + if ( tabId !== '' ) { return 'tab_' + tabId; } return ''; @@ -83,20 +83,20 @@ // Emphasize hostname and cookie name. - var emphasizeCookie = function (s) { + var emphasizeCookie = function(s) { var pnode = emphasizeHostname(s); - if (pnode.childNodes.length !== 3) { + if ( pnode.childNodes.length !== 3 ) { return pnode; } var prefix = '-cookie:'; var text = pnode.childNodes[2].textContent; var beg = text.indexOf(prefix); - if (beg === -1) { + if ( beg === -1 ) { return pnode; } beg += prefix.length; var end = text.indexOf('}', beg); - if (end === -1) { + if ( end === -1 ) { return pnode; } var cnode = emphasizeTemplate.cloneNode(true); @@ -113,17 +113,17 @@ // Emphasize hostname in URL. - var emphasizeHostname = function (url) { + var emphasizeHostname = function(url) { var hnbeg = url.indexOf('://'); - if (hnbeg === -1) { + if ( hnbeg === -1 ) { return document.createTextNode(url); } hnbeg += 3; var hnend = url.indexOf('/', hnbeg); - if (hnend === -1) { + if ( hnend === -1 ) { hnend = url.slice(hnbeg).search(/\?#/); - if (hnend !== -1) { + if ( hnend !== -1 ) { hnend += hnbeg; } else { hnend = url.length; @@ -139,19 +139,19 @@ /******************************************************************************/ - var createCellAt = function (tr, index) { + var createCellAt = function(tr, index) { var td = tr.cells[index]; var mustAppend = !td; - if (mustAppend) { + if ( mustAppend ) { td = tdJunkyard.pop(); } - if (td) { + if ( td ) { td.removeAttribute('colspan'); td.textContent = ''; } else { td = document.createElement('td'); } - if (mustAppend) { + if ( mustAppend ) { tr.appendChild(td); } return td; @@ -159,28 +159,26 @@ /******************************************************************************/ - var createRow = function (layout) { + var createRow = function(layout) { var tr = trJunkyard.pop(); - if (tr) { + if ( tr ) { tr.className = ''; } else { tr = document.createElement('tr'); } - for (var index = 0; index < firstVarDataCol; index++) { + for ( var index = 0; index < firstVarDataCol; index++ ) { createCellAt(tr, index); } - var i = 1, - span = 1, - td; + var i = 1, span = 1, td; for (;;) { td = createCellAt(tr, index); - if (i === lastVarDataIndex) { + if ( i === lastVarDataIndex ) { break; } - if (layout.charAt(i) !== '1') { + if ( layout.charAt(i) !== '1' ) { span += 1; } else { - if (span !== 1) { + if ( span !== 1 ) { td.setAttribute('colspan', span); } index += 1; @@ -188,11 +186,11 @@ } i += 1; } - if (span !== 1) { + if ( span !== 1 ) { td.setAttribute('colspan', span); } index += 1; - while ((td = tr.cells[index])) { + while ( (td = tr.cells[index]) ) { tdJunkyard.push(tr.removeChild(td)); } return tr; @@ -200,7 +198,7 @@ /******************************************************************************/ - var createHiddenTextNode = function (text) { + var createHiddenTextNode = function(text) { var node = hiddenTemplate.cloneNode(true); node.textContent = text; return node; @@ -208,13 +206,13 @@ /******************************************************************************/ - var padTo2 = function (v) { + var padTo2 = function(v) { return v < 10 ? '0' + v : v; }; /******************************************************************************/ - var createGap = function (tabId, url) { + var createGap = function(tabId, url) { var tr = createRow('1'); tr.classList.add('doc'); tr.classList.add('tab'); @@ -226,49 +224,49 @@ /******************************************************************************/ - var renderLogEntry = function (entry) { + var renderLogEntry = function(entry) { var tr; var fvdc = firstVarDataCol; - switch (entry.cat) { - case 'error': - case 'info': - tr = createRow('1'); - if (entry.d0 === 'cookie') { - tr.cells[fvdc].appendChild(emphasizeCookie(entry.d1)); - } else { - tr.cells[fvdc].textContent = entry.d0; - } - break; + switch ( entry.cat ) { + case 'error': + case 'info': + tr = createRow('1'); + if ( entry.d0 === 'cookie' ) { + tr.cells[fvdc].appendChild(emphasizeCookie(entry.d1)); + } else { + tr.cells[fvdc].textContent = entry.d0; + } + break; - case 'net': - tr = createRow('111'); - tr.classList.add('canMtx'); - // If the request is that of a root frame, insert a gap in the table - // in order to visually separate entries for different documents. - if (entry.d2 === 'doc' && entry.tab !== noTabId) { - createGap(entry.tab, entry.d1); - } - if (entry.d3) { - tr.classList.add('blocked'); - tr.cells[fvdc].textContent = '--'; - } else { - tr.cells[fvdc].textContent = ''; - } - tr.cells[fvdc + 1].textContent = (prettyRequestTypes[entry.d2] || entry.d2); - if (dontEmphasizeSet.has(entry.d2)) { - tr.cells[fvdc + 2].textContent = entry.d1; - } else if (entry.d2 === 'cookie') { - tr.cells[fvdc + 2].appendChild(emphasizeCookie(entry.d1)); - } else { - tr.cells[fvdc + 2].appendChild(emphasizeHostname(entry.d1)); - } - break; + case 'net': + tr = createRow('111'); + tr.classList.add('canMtx'); + // If the request is that of a root frame, insert a gap in the table + // in order to visually separate entries for different documents. + if ( entry.d2 === 'doc' && entry.tab !== noTabId ) { + createGap(entry.tab, entry.d1); + } + if ( entry.d3 ) { + tr.classList.add('blocked'); + tr.cells[fvdc].textContent = '--'; + } else { + tr.cells[fvdc].textContent = ''; + } + tr.cells[fvdc+1].textContent = (prettyRequestTypes[entry.d2] || entry.d2); + if ( dontEmphasizeSet.has(entry.d2) ) { + tr.cells[fvdc+2].textContent = entry.d1; + } else if ( entry.d2 === 'cookie' ) { + tr.cells[fvdc+2].appendChild(emphasizeCookie(entry.d1)); + } else { + tr.cells[fvdc+2].appendChild(emphasizeHostname(entry.d1)); + } + break; - default: - tr = createRow('1'); - tr.cells[fvdc].textContent = entry.d0; - break; + default: + tr = createRow('1'); + tr.cells[fvdc].textContent = entry.d0; + break; } // Fields common to all rows. @@ -278,14 +276,14 @@ padTo2(time.getUTCMinutes()) + ':' + padTo2(time.getSeconds()); - if (entry.tab) { + if ( entry.tab ) { tr.classList.add('tab'); tr.classList.add(classNameFromTabId(entry.tab)); - if (entry.tab === noTabId) { + if ( entry.tab === noTabId ) { tr.cells[1].appendChild(createHiddenTextNode('bts')); } } - if (entry.cat !== '') { + if ( entry.cat !== '' ) { tr.classList.add('cat_' + entry.cat); } @@ -300,9 +298,9 @@ /******************************************************************************/ - var renderLogEntries = function (response) { + var renderLogEntries = function(response) { var entries = response.entries; - if (entries.length === 0) { + if ( entries.length === 0 ) { return; } @@ -312,10 +310,10 @@ var tabIds = response.tabIds; var n = entries.length; var entry; - for (var i = 0; i < n; i++) { + for ( var i = 0; i < n; i++ ) { entry = entries[i]; // Unlikely, but it may happen - if (entry.tab && tabIds.hasOwnProperty(entry.tab) === false) { + if ( entry.tab && tabIds.hasOwnProperty(entry.tab) === false ) { continue; } renderLogEntry(entries[i]); @@ -327,14 +325,14 @@ truncateLog(maxEntries); var yDelta = tbody.offsetHeight - height; - if (yDelta === 0) { + if ( yDelta === 0 ) { return; } // Chromium: // body.scrollTop = good value // body.parentNode.scrollTop = 0 - if (document.body.scrollTop !== 0) { + if ( document.body.scrollTop !== 0 ) { document.body.scrollTop += yDelta; return; } @@ -343,68 +341,68 @@ // body.scrollTop = 0 // body.parentNode.scrollTop = good value var parentNode = document.body.parentNode; - if (parentNode && parentNode.scrollTop !== 0) { + if ( parentNode && parentNode.scrollTop !== 0 ) { parentNode.scrollTop += yDelta; } }; /******************************************************************************/ - var synchronizeTabIds = function (newTabIds) { + var synchronizeTabIds = function(newTabIds) { var oldTabIds = allTabIds; var autoDeleteVoidRows = !!vAPI.localStorage.getItem('loggerAutoDeleteVoidRows'); var rowVoided = false; var trs; - for (var tabId in oldTabIds) { - if (oldTabIds.hasOwnProperty(tabId) === false) { + for ( var tabId in oldTabIds ) { + if ( oldTabIds.hasOwnProperty(tabId) === false ) { continue; } - if (newTabIds.hasOwnProperty(tabId)) { + if ( newTabIds.hasOwnProperty(tabId) ) { continue; } // Mark or remove voided rows trs = uDom('.tab_' + tabId); - if (autoDeleteVoidRows) { + 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) { + if ( tabId === popupManager.tabId ) { popupManager.toggleOff(); } } var select = document.getElementById('pageSelector'); var selectValue = select.value; - var tabIds = Object.keys(newTabIds).sort(function (a, b) { + 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++) { + for ( var i = 0, j = 2; i < tabIds.length; i++ ) { tabId = tabIds[i]; - if (tabId === noTabId) { + if ( tabId === noTabId ) { continue; } option = select.options[j]; j += 1; - if (!option) { + if ( !option ) { option = document.createElement('option'); select.appendChild(option); } option.textContent = newTabIds[tabId]; option.value = classNameFromTabId(tabId); - if (option.value === selectValue) { + if ( option.value === selectValue ) { option.setAttribute('selected', ''); } else { option.removeAttribute('selected'); } } - while (j < select.options.length) { + while ( j < select.options.length ) { select.removeChild(select.options[j]); } - if (select.value !== selectValue) { + if ( select.value !== selectValue ) { select.selectedIndex = 0; select.value = ''; select.options[0].setAttribute('selected', ''); @@ -418,14 +416,14 @@ /******************************************************************************/ - var truncateLog = function (size) { - if (size === 0) { + var truncateLog = function(size) { + if ( size === 0 ) { size = 5000; } var tbody = document.querySelector('#content tbody'); size = Math.min(size, 10000); var tr; - while (tbody.childElementCount > size) { + while ( tbody.childElementCount > size ) { tr = tbody.lastElementChild; trJunkyard.push(tbody.removeChild(tr)); } @@ -433,8 +431,8 @@ /******************************************************************************/ - var onLogBufferRead = function (response) { - if (!response || response.unavailable) { + var onLogBufferRead = function(response) { + if ( !response || response.unavailable ) { readLogBufferAsync(); return; } @@ -443,21 +441,21 @@ noTabId = response.noTabId; // This may have changed meanwhile - if (response.maxLoggedRequests !== maxEntries) { + if ( response.maxLoggedRequests !== maxEntries ) { maxEntries = response.maxLoggedRequests; uDom('#maxEntries').val(maxEntries || ''); } // Neuter rows for which a tab does not exist anymore var rowVoided = false; - if (response.tabIdsToken !== allTabIdsToken) { + if ( response.tabIdsToken !== allTabIdsToken ) { rowVoided = synchronizeTabIds(response.tabIds); allTabIdsToken = response.tabIdsToken; } renderLogEntries(response); - if (rowVoided) { + if ( rowVoided ) { uDom('#clean').toggleClass( 'disabled', tbody.querySelector('tr.tab:not(.canMtx)') === null @@ -479,36 +477,30 @@ // automatically. If called after init time, this will be messy, and this would // require a bit more code to ensure no multi time out events. - var readLogBuffer = function () { - if (ownerId === undefined) { - return; - } + var readLogBuffer = function() { + if ( ownerId === undefined ) { return; } vAPI.messaging.send( - 'logger-ui.js', { - what: 'readMany', - ownerId: ownerId - }, + 'logger-ui.js', + { what: 'readMany', ownerId: ownerId }, onLogBufferRead ); }; - var readLogBufferAsync = function () { - if (ownerId === undefined) { - return; - } + var readLogBufferAsync = function() { + if ( ownerId === undefined ) { return; } vAPI.setTimeout(readLogBuffer, 1200); }; /******************************************************************************/ - var pageSelectorChanged = function () { + var pageSelectorChanged = function() { var style = document.getElementById('tabFilterer'); var tabClass = document.getElementById('pageSelector').value; var sheet = style.sheet; - while (sheet.cssRules.length !== 0) { + while ( sheet.cssRules.length !== 0 ) { sheet.deleteRule(0); } - if (tabClass !== '') { + if ( tabClass !== '' ) { sheet.insertRule( '#content table tr:not(.' + tabClass + ') { display: none; }', 0 @@ -522,30 +514,28 @@ /******************************************************************************/ - var refreshTab = function () { + var refreshTab = function() { var tabClass = document.getElementById('pageSelector').value; var matches = tabClass.match(/^tab_(.+)$/); - if (matches === null) { + if ( matches === null ) { return; } - if (matches[1] === 'bts') { + if ( matches[1] === 'bts' ) { return; } vAPI.messaging.send( - 'logger-ui.js', { - what: 'forceReloadTab', - tabId: matches[1] - } + 'logger-ui.js', + { what: 'forceReloadTab', tabId: matches[1] } ); }; /******************************************************************************/ - var onMaxEntriesChanged = function () { + var onMaxEntriesChanged = function() { var raw = uDom(this).val(); try { maxEntries = parseInt(raw, 10); - if (isNaN(maxEntries)) { + if ( isNaN(maxEntries) ) { maxEntries = 0; } } catch (e) { @@ -563,47 +553,46 @@ /******************************************************************************/ - var rowFilterer = (function () { + var rowFilterer = (function() { var filters = []; - var parseInput = function () { + var parseInput = function() { filters = []; var rawPart, hardBeg, hardEnd; var raw = uDom('#filterInput').val().trim(); var rawParts = raw.split(/\s+/); - var reStr, reStrs = [], - not = false; + var reStr, reStrs = [], not = false; var n = rawParts.length; - for (var i = 0; i < n; i++) { + for ( var i = 0; i < n; i++ ) { rawPart = rawParts[i]; - if (rawPart.charAt(0) === '!') { - if (reStrs.length === 0) { + if ( rawPart.charAt(0) === '!' ) { + if ( reStrs.length === 0 ) { not = true; } rawPart = rawPart.slice(1); } hardBeg = rawPart.charAt(0) === '|'; - if (hardBeg) { + if ( hardBeg ) { rawPart = rawPart.slice(1); } hardEnd = rawPart.slice(-1) === '|'; - if (hardEnd) { + if ( hardEnd ) { rawPart = rawPart.slice(0, -1); } - if (rawPart === '') { + if ( rawPart === '' ) { continue; } // https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions reStr = rawPart.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - if (hardBeg) { + if ( hardBeg ) { reStr = '(?:^|\\s)' + reStr; } - if (hardEnd) { + if ( hardEnd ) { reStr += '(?:\\s|$)'; } reStrs.push(reStr); - if (i < (n - 1) && rawParts[i + 1] === '||') { + if ( i < (n - 1) && rawParts[i + 1] === '||' ) { i += 1; continue; } @@ -617,19 +606,19 @@ } }; - var filterOne = function (tr, clean) { + var filterOne = function(tr, clean) { var ff = filters; var fcount = ff.length; - if (fcount === 0 && clean === true) { + if ( fcount === 0 && clean === true ) { return; } // do not filter out doc boundaries, they help separate important // section of log. var cl = tr.classList; - if (cl.contains('doc')) { + if ( cl.contains('doc') ) { return; } - if (fcount === 0) { + if ( fcount === 0 ) { cl.remove('f'); return; } @@ -640,16 +629,16 @@ // if... // positive filter expression = there must one hit on any field // negative filter expression = there must be no hit on all fields - for (var i = 0; i < fcount; i++) { + for ( var i = 0; i < fcount; i++ ) { f = ff[i]; hit = !f.r; - for (j = 0; j < ccount; j++) { - if (f.re.test(cc[j].textContent)) { + for ( j = 0; j < ccount; j++ ) { + if ( f.re.test(cc[j].textContent) ) { hit = f.r; break; } } - if (!hit) { + if ( !hit ) { cl.add('f'); return; } @@ -657,36 +646,36 @@ cl.remove('f'); }; - var filterAll = function () { + var filterAll = function() { // Special case: no filter - if (filters.length === 0) { + if ( filters.length === 0 ) { uDom('#content tr').removeClass('f'); return; } var tbody = document.querySelector('#content tbody'); var rows = tbody.rows; var i = rows.length; - while (i--) { + while ( i-- ) { filterOne(rows[i]); } }; - var onFilterChangedAsync = (function () { + var onFilterChangedAsync = (function() { var timer = null; - var commit = function () { + var commit = function() { timer = null; parseInput(); filterAll(); }; - return function () { - if (timer !== null) { + return function() { + if ( timer !== null ) { clearTimeout(timer); } timer = vAPI.setTimeout(commit, 750); }; })(); - var onFilterButton = function () { + var onFilterButton = function() { var cl = document.body.classList; cl.toggle('f', cl.contains('f') === false); }; @@ -702,20 +691,20 @@ /******************************************************************************/ - var toJunkyard = function (trs) { + var toJunkyard = function(trs) { trs.remove(); var i = trs.length; - while (i--) { + while ( i-- ) { trJunkyard.push(trs.nodeAt(i)); } }; /******************************************************************************/ - var clearBuffer = function () { + var clearBuffer = function() { var tbody = document.querySelector('#content tbody'); var tr; - while (tbody.firstChild !== null) { + while ( tbody.firstChild !== null ) { tr = tbody.lastElementChild; trJunkyard.push(tbody.removeChild(tr)); } @@ -725,10 +714,10 @@ /******************************************************************************/ - var cleanBuffer = function () { + var cleanBuffer = function() { var rows = uDom('#content tr.tab:not(.canMtx)').remove(); var i = rows.length; - while (i--) { + while ( i-- ) { trJunkyard.push(rows.nodeAt(i)); } uDom('#clean').addClass('disabled'); @@ -736,18 +725,18 @@ /******************************************************************************/ - var toggleCompactView = function () { + var toggleCompactView = function() { document.body.classList.toggle('compactView'); uDom('#content table .vExpanded').removeClass('vExpanded'); }; - var toggleCompactRow = function (ev) { + var toggleCompactRow = function(ev) { ev.target.parentElement.classList.toggle('vExpanded'); }; /******************************************************************************/ - var popupManager = (function () { + var popupManager = (function() { var realTabId = null; var localTabId = null; var container = null; @@ -761,63 +750,63 @@ '}' ].join('\n'); - var resizePopup = function () { - if (popup === null) { + var resizePopup = function() { + if ( popup === null ) { return; } var popupBody = popup.contentWindow.document.body; - if (popupBody.clientWidth !== 0 && container.clientWidth !== popupBody.clientWidth) { + if ( popupBody.clientWidth !== 0 && container.clientWidth !== popupBody.clientWidth ) { container.style.setProperty('width', popupBody.clientWidth + 'px'); } popup.style.removeProperty('height'); - if (popupBody.clientHeight !== 0 && popup.clientHeight !== popupBody.clientHeight) { + if ( popupBody.clientHeight !== 0 && popup.clientHeight !== popupBody.clientHeight ) { popup.style.setProperty('height', popupBody.clientHeight + 'px'); } var ph = document.documentElement.clientHeight; var crect = container.getBoundingClientRect(); - if (crect.height > ph) { + if ( crect.height > ph ) { popup.style.setProperty('height', 'calc(' + ph + 'px - 1.8em)'); } // Adjust width for presence/absence of vertical scroll bar which may // have appeared as a result of last operation. var cw = container.clientWidth; var dw = popup.contentWindow.document.documentElement.clientWidth; - if (cw !== dw) { + if ( cw !== dw ) { container.style.setProperty('width', (2 * cw - dw) + 'px'); } }; - var toggleSize = function () { + var toggleSize = function() { container.classList.toggle('hide'); }; - var onResizeRequested = function () { + var onResizeRequested = function() { var popupBody = popup.contentWindow.document.body; - if (popupBody.hasAttribute('data-resize-popup') === false) { + if ( popupBody.hasAttribute('data-resize-popup') === false ) { return; } popupBody.removeAttribute('data-resize-popup'); resizePopup(); }; - var onLoad = function () { + var onLoad = function() { resizePopup(); var popupBody = popup.contentDocument.body; popupBody.removeAttribute('data-resize-popup'); popupObserver.observe(popupBody, { attributes: true, - attributesFilter: ['data-resize-popup'] + attributesFilter: [ 'data-resize-popup' ] }); }; - var toggleOn = function (td) { + var toggleOn = function(td) { var tr = td.parentNode; var matches = tr.className.match(/(?:^| )tab_([^ ]+)/); - if (matches === null) { + if ( matches === null ) { return; } realTabId = localTabId = matches[1]; - if (localTabId === 'bts') { + if ( localTabId === 'bts' ) { realTabId = noTabId; } @@ -838,7 +827,7 @@ document.body.classList.add('popupOn'); }; - var toggleOff = function () { + var toggleOff = function() { document.body.classList.remove('popupOn'); container.querySelector('div > span:nth-of-type(1)').removeEventListener('click', toggleSize); @@ -860,22 +849,20 @@ }; var exports = { - toggleOn: function (ev) { - if (realTabId === null) { + toggleOn: function(ev) { + if ( realTabId === null ) { toggleOn(ev.target); } }, - toggleOff: function () { - if (realTabId !== null) { + toggleOff: function() { + if ( realTabId !== null ) { toggleOff(); } } }; Object.defineProperty(exports, 'tabId', { - get: function () { - return realTabId || 0; - } + get: function() { return realTabId || 0; } }); return exports; @@ -883,22 +870,18 @@ /******************************************************************************/ - var grabView = function () { - if (ownerId === undefined) { + var grabView = function() { + if ( ownerId === undefined ) { ownerId = Date.now(); } readLogBufferAsync(); }; - var releaseView = function () { - if (ownerId === undefined) { - return; - } + var releaseView = function() { + if ( ownerId === undefined ) { return; } vAPI.messaging.send( - 'logger-ui.js', { - what: 'releaseView', - ownerId: ownerId - } + 'logger-ui.js', + { what: 'releaseView', ownerId: ownerId } ); ownerId = undefined; }; -- cgit v1.2.3