diff options
author | Jesús <heckyel@hyperbola.info> | 2019-09-21 20:55:00 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-09-21 20:55:00 -0500 |
commit | d610c87a53736071e1d7c8c7252268b6ceeaae4f (patch) | |
tree | fca8396a97237e15d64e2ac74096550e8262ffc7 | |
parent | 0bbba59da4099bbbbe77ddbc20a0caebbf28fe80 (diff) | |
download | ematrix-d610c87a53736071e1d7c8c7252268b6ceeaae4f.tar.lz ematrix-d610c87a53736071e1d7c8c7252268b6ceeaae4f.tar.xz ematrix-d610c87a53736071e1d7c8c7252268b6ceeaae4f.zip |
Improve syntax
-rw-r--r-- | js/logger-ui.js | 371 | ||||
-rw-r--r-- | js/pagestats.js | 2 | ||||
-rw-r--r-- | js/polyfill.js | 1 | ||||
-rw-r--r-- | js/storage.js | 333 | ||||
-rw-r--r-- | js/tab.js | 10 |
5 files changed, 329 insertions, 388 deletions
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; }; diff --git a/js/pagestats.js b/js/pagestats.js index 276f099..d5c2cdd 100644 --- a/js/pagestats.js +++ b/js/pagestats.js @@ -206,7 +206,7 @@ }, recordRequest: function(type, url, block) { - if ( block !== false ) { + if ( block !== false ) { this.perLoadBlockedRequestCount++; } else { this.perLoadAllowedRequestCount++; diff --git a/js/polyfill.js b/js/polyfill.js index b0ff617..65ba414 100644 --- a/js/polyfill.js +++ b/js/polyfill.js @@ -94,4 +94,3 @@ if ( self.Map.prototype.iterator instanceof Function ) { return this; }; } - diff --git a/js/storage.js b/js/storage.js index a96caf3..b3a5e7b 100644 --- a/js/storage.js +++ b/js/storage.js @@ -27,13 +27,13 @@ /******************************************************************************/ -ηMatrix.getBytesInUse = function () { +ηMatrix.getBytesInUse = function() { var ηm = this; - var getBytesInUseHandler = function (bytesInUse) { + var getBytesInUseHandler = function(bytesInUse) { ηm.storageUsed = bytesInUse; }; // Not all WebExtension implementations support getBytesInUse(). - if (typeof vAPI.storage.getBytesInUse === 'function') { + if ( typeof vAPI.storage.getBytesInUse === 'function' ) { vAPI.storage.getBytesInUse(null, getBytesInUseHandler); } else { ηm.storageUsed = undefined; @@ -42,21 +42,21 @@ /******************************************************************************/ -ηMatrix.saveUserSettings = function () { +ηMatrix.saveUserSettings = function() { this.XAL.keyvalSetMany( this.userSettings, this.getBytesInUse.bind(this) ); }; -ηMatrix.loadUserSettings = function (callback) { +ηMatrix.loadUserSettings = function(callback) { var ηm = this; - if (typeof callback !== 'function') { + if ( typeof callback !== 'function' ) { callback = this.noopFunc; } - var settingsLoaded = function (store) { + var settingsLoaded = function(store) { // console.log('storage.js > loaded user settings'); ηm.userSettings = store; @@ -69,17 +69,15 @@ /******************************************************************************/ -ηMatrix.loadRawSettings = function () { +ηMatrix.loadRawSettings = function() { var ηm = this; - var onLoaded = function (bin) { - if (!bin || bin.rawSettings instanceof Object === false) { - return; - } - for (var key of Object.keys(bin.rawSettings)) { + var onLoaded = function(bin) { + if ( !bin || bin.rawSettings instanceof Object === false ) { return; } + for ( var key of Object.keys(bin.rawSettings) ) { if ( ηm.rawSettings.hasOwnProperty(key) === false || - typeof bin.rawSettings[key] !== typeof ηm.rawSettings[key] + typeof bin.rawSettings[key] !== typeof ηm.rawSettings[key] ) { continue; } @@ -91,77 +89,73 @@ vAPI.storage.get('rawSettings', onLoaded); }; -ηMatrix.saveRawSettings = function (rawSettings, callback) { +ηMatrix.saveRawSettings = function(rawSettings, callback) { var keys = Object.keys(rawSettings); - if (keys.length === 0) { - if (typeof callback === 'function') { + if ( keys.length === 0 ) { + if ( typeof callback === 'function' ) { callback(); } return; } - for (var key of keys) { + for ( var key of keys ) { if ( this.rawSettingsDefault.hasOwnProperty(key) && - typeof rawSettings[key] === typeof this.rawSettingsDefault[key] + typeof rawSettings[key] === typeof this.rawSettingsDefault[key] ) { this.rawSettings[key] = rawSettings[key]; } } - vAPI.storage.set({ - rawSettings: this.rawSettings - }, callback); + vAPI.storage.set({ rawSettings: this.rawSettings }, callback); this.rawSettingsWriteTime = Date.now(); }; -ηMatrix.rawSettingsFromString = function (raw) { +ηMatrix.rawSettingsFromString = function(raw) { var result = {}, lineIter = new this.LineIterator(raw), line, matches, name, value; - while (lineIter.eot() === false) { + while ( lineIter.eot() === false ) { line = lineIter.next().trim(); matches = /^(\S+)(\s+(.+))?$/.exec(line); - if (matches === null) { - continue; - } + if ( matches === null ) { continue; } name = matches[1]; - if (this.rawSettingsDefault.hasOwnProperty(name) === false) { + if ( this.rawSettingsDefault.hasOwnProperty(name) === false ) { continue; } value = (matches[2] || '').trim(); - switch (typeof this.rawSettingsDefault[name]) { - case 'boolean': - if (value === 'true') { - value = true; - } else if (value === 'false') { - value = false; - } else { - value = this.rawSettingsDefault[name]; - } - break; - case 'string': - if (value === '') { - value = this.rawSettingsDefault[name]; - } - break; - case 'number': - value = parseInt(value, 10); - if (isNaN(value)) { - value = this.rawSettingsDefault[name]; - } - break; - default: - break; + switch ( typeof this.rawSettingsDefault[name] ) { + case 'boolean': + if ( value === 'true' ) { + value = true; + } else if ( value === 'false' ) { + value = false; + } else { + value = this.rawSettingsDefault[name]; + } + break; + case 'string': + if ( value === '' ) { + value = this.rawSettingsDefault[name]; + } + break; + case 'number': + value = parseInt(value, 10); + if ( isNaN(value) ) { + value = this.rawSettingsDefault[name]; + } + break; + default: + break; } - if (this.rawSettings[name] !== value) { + if ( this.rawSettings[name] !== value ) { result[name] = value; } } this.saveRawSettings(result); }; -ηMatrix.stringFromRawSettings = function () { +ηMatrix.stringFromRawSettings = function() { var out = []; - for (var key of Object.keys(this.rawSettings).sort()) { + for ( var key of Object.keys(this.rawSettings).sort() ) { out.push(key + ' ' + this.rawSettings[key]); } return out.join('\n'); @@ -170,19 +164,19 @@ /******************************************************************************/ // save white/blacklist -ηMatrix.saveMatrix = function () { +ηMatrix.saveMatrix = function() { ηMatrix.XAL.keyvalSetOne('userMatrix', this.pMatrix.toString()); }; /******************************************************************************/ -ηMatrix.loadMatrix = function (callback) { - if (typeof callback !== 'function') { +ηMatrix.loadMatrix = function(callback) { + if ( typeof callback !== 'function' ) { callback = this.noopFunc; } var ηm = this; - var onLoaded = function (bin) { - if (bin.hasOwnProperty('userMatrix')) { + var onLoaded = function(bin) { + if ( bin.hasOwnProperty('userMatrix') ) { ηm.pMatrix.fromString(bin.userMatrix); ηm.tMatrix.assign(ηm.pMatrix); callback(); @@ -193,17 +187,15 @@ /******************************************************************************/ -ηMatrix.listKeysFromCustomHostsFiles = function (raw) { +ηMatrix.listKeysFromCustomHostsFiles = function(raw) { var out = new Set(), reIgnore = /^[!#]/, reValid = /^[a-z-]+:\/\/\S+/, lineIter = new this.LineIterator(raw), location; - while (lineIter.eot() === false) { + while ( lineIter.eot() === false ) { location = lineIter.next().trim(); - if (reIgnore.test(location) || !reValid.test(location)) { - continue; - } + if ( reIgnore.test(location) || !reValid.test(location) ) { continue; } out.add(location); } return this.setToArray(out); @@ -211,7 +203,7 @@ /******************************************************************************/ -ηMatrix.getAvailableHostsFiles = function (callback) { +ηMatrix.getAvailableHostsFiles = function(callback) { var ηm = this, availableHostsFiles = {}; @@ -219,7 +211,7 @@ var importedListKeys = this.listKeysFromCustomHostsFiles(ηm.userSettings.externalHostsFiles), i = importedListKeys.length, listKey, entry; - while (i--) { + while ( i-- ) { listKey = importedListKeys[i]; entry = { content: 'filters', @@ -233,37 +225,31 @@ } // selected lists - var onSelectedHostsFilesLoaded = function (bin) { + var onSelectedHostsFilesLoaded = function(bin) { // Now get user's selection of lists - for (var assetKey in bin.liveHostsFiles) { + for ( var assetKey in bin.liveHostsFiles ) { var availableEntry = availableHostsFiles[assetKey]; - if (availableEntry === undefined) { - continue; - } + if ( availableEntry === undefined ) { continue; } var liveEntry = bin.liveHostsFiles[assetKey]; availableEntry.off = liveEntry.off || false; - if (liveEntry.entryCount !== undefined) { + if ( liveEntry.entryCount !== undefined ) { availableEntry.entryCount = liveEntry.entryCount; } - if (liveEntry.entryUsedCount !== undefined) { + if ( liveEntry.entryUsedCount !== undefined ) { availableEntry.entryUsedCount = liveEntry.entryUsedCount; } // This may happen if the list name was pulled from the list content - if (availableEntry.title === '' && liveEntry.title !== undefined) { + if ( availableEntry.title === '' && liveEntry.title !== undefined ) { availableEntry.title = liveEntry.title; } } // Remove unreferenced imported filter lists. var dict = new Set(importedListKeys); - for (assetKey in availableHostsFiles) { + for ( assetKey in availableHostsFiles ) { var entry = availableHostsFiles[assetKey]; - if (entry.submitter !== 'user') { - continue; - } - if (dict.has(assetKey)) { - continue; - } + if ( entry.submitter !== 'user' ) { continue; } + if ( dict.has(assetKey) ) { continue; } delete availableHostsFiles[assetKey]; ηm.assets.unregisterAssetSource(assetKey); ηm.assets.remove(assetKey); @@ -273,22 +259,17 @@ }; // built-in lists - var onBuiltinHostsFilesLoaded = function (entries) { - for (var assetKey in entries) { - if (entries.hasOwnProperty(assetKey) === false) { - continue; - } + var onBuiltinHostsFilesLoaded = function(entries) { + for ( var assetKey in entries ) { + if ( entries.hasOwnProperty(assetKey) === false ) { continue; } entry = entries[assetKey]; - if (entry.content !== 'filters') { - continue; - } + if ( entry.content !== 'filters' ) { continue; } availableHostsFiles[assetKey] = objectAssign({}, entry); } // Now get user's selection of lists - vAPI.storage.get({ - 'liveHostsFiles': availableHostsFiles - }, + vAPI.storage.get( + { 'liveHostsFiles': availableHostsFiles }, onSelectedHostsFilesLoaded ); }; @@ -298,35 +279,31 @@ /******************************************************************************/ -ηMatrix.loadHostsFiles = function (callback) { +ηMatrix.loadHostsFiles = function(callback) { var ηm = ηMatrix; var hostsFileLoadCount; - if (typeof callback !== 'function') { + if ( typeof callback !== 'function' ) { callback = this.noopFunc; } - var loadHostsFilesEnd = function () { + var loadHostsFilesEnd = function() { ηm.ubiquitousBlacklist.freeze(); - vAPI.storage.set({ - 'liveHostsFiles': ηm.liveHostsFiles - }); - vAPI.messaging.broadcast({ - what: 'loadHostsFilesCompleted' - }); + vAPI.storage.set({ 'liveHostsFiles': ηm.liveHostsFiles }); + vAPI.messaging.broadcast({ what: 'loadHostsFilesCompleted' }); ηm.getBytesInUse(); callback(); }; - var mergeHostsFile = function (details) { + var mergeHostsFile = function(details) { ηm.mergeHostsFile(details); hostsFileLoadCount -= 1; - if (hostsFileLoadCount === 0) { + if ( hostsFileLoadCount === 0 ) { loadHostsFilesEnd(); } }; - var loadHostsFilesStart = function (hostsFiles) { + var loadHostsFilesStart = function(hostsFiles) { ηm.liveHostsFiles = hostsFiles; ηm.ubiquitousBlacklist.reset(); var locations = Object.keys(hostsFiles); @@ -334,8 +311,8 @@ // Load all hosts file which are not disabled. var location; - while ((location = locations.pop())) { - if (hostsFiles[location].off) { + while ( (location = locations.pop()) ) { + if ( hostsFiles[location].off ) { hostsFileLoadCount -= 1; continue; } @@ -343,7 +320,7 @@ } // https://github.com/gorhill/uMatrix/issues/2 - if (hostsFileLoadCount === 0) { + if ( hostsFileLoadCount === 0 ) { loadHostsFilesEnd(); return; } @@ -354,7 +331,7 @@ /******************************************************************************/ -ηMatrix.mergeHostsFile = function (details) { +ηMatrix.mergeHostsFile = function(details) { var usedCount = this.ubiquitousBlacklist.count; var duplicateCount = this.ubiquitousBlacklist.duplicateCount; @@ -370,21 +347,20 @@ /******************************************************************************/ -ηMatrix.mergeHostsFileContent = function (rawText) { +ηMatrix.mergeHostsFileContent = function(rawText) { var rawEnd = rawText.length; var ubiquitousBlacklist = this.ubiquitousBlacklist; var reLocalhost = /(^|\s)(localhost\.localdomain|localhost|local|broadcasthost|0\.0\.0\.0|127\.0\.0\.1|::1|fe80::1%lo0)(?=\s|$)/g; var reAsciiSegment = /^[\x21-\x7e]+$/; var matches; - var lineBeg = 0, - lineEnd; + var lineBeg = 0, lineEnd; var line; - while (lineBeg < rawEnd) { + while ( lineBeg < rawEnd ) { lineEnd = rawText.indexOf('\n', lineBeg); - if (lineEnd < 0) { + if ( lineEnd < 0 ) { lineEnd = rawText.indexOf('\r', lineBeg); - if (lineEnd < 0) { + if ( lineEnd < 0 ) { lineEnd = rawEnd; } } @@ -406,19 +382,19 @@ // The filter is whatever sequence of printable ascii character without // whitespaces matches = reAsciiSegment.exec(line); - if (!matches || matches.length === 0) { + if ( !matches || matches.length === 0 ) { continue; } // Bypass anomalies // For example, when a filter contains whitespace characters, or // whatever else outside the range of printable ascii characters. - if (matches[0] !== line) { + if ( matches[0] !== line ) { continue; } line = matches[0]; - if (line === '') { + if ( line === '' ) { continue; } @@ -430,37 +406,37 @@ // `switches` contains the filter lists for which the switch must be revisited. -ηMatrix.selectHostsFiles = function (details, callback) { +ηMatrix.selectHostsFiles = function(details, callback) { var ηm = this, externalHostsFiles = this.userSettings.externalHostsFiles, i, n, assetKey; // Hosts file to select - if (Array.isArray(details.toSelect)) { - for (assetKey in this.liveHostsFiles) { - if (this.liveHostsFiles.hasOwnProperty(assetKey) === false) { + if ( Array.isArray(details.toSelect) ) { + for ( assetKey in this.liveHostsFiles ) { + if ( this.liveHostsFiles.hasOwnProperty(assetKey) === false ) { continue; } - if (details.toSelect.indexOf(assetKey) !== -1) { + if ( details.toSelect.indexOf(assetKey) !== -1 ) { this.liveHostsFiles[assetKey].off = false; - } else if (details.merge !== true) { + } else if ( details.merge !== true ) { this.liveHostsFiles[assetKey].off = true; } } } // Imported hosts files to remove - if (Array.isArray(details.toRemove)) { - var removeURLFromHaystack = function (haystack, needle) { + if ( Array.isArray(details.toRemove) ) { + var removeURLFromHaystack = function(haystack, needle) { return haystack.replace( new RegExp( '(^|\\n)' + - needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + - '(\\n|$)', 'g'), + needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + + '(\\n|$)', 'g'), '\n' ).trim(); }; - for (i = 0, n = details.toRemove.length; i < n; i++) { + for ( i = 0, n = details.toRemove.length; i < n; i++ ) { assetKey = details.toRemove[i]; delete this.liveHostsFiles[assetKey]; externalHostsFiles = removeURLFromHaystack(externalHostsFiles, assetKey); @@ -469,30 +445,23 @@ } // Hosts file to import - if (typeof details.toImport === 'string') { + if ( typeof details.toImport === 'string' ) { // https://github.com/gorhill/uBlock/issues/1181 // Try mapping the URL of an imported filter list to the assetKey of an // existing stock list. - var assetKeyFromURL = function (url) { + var assetKeyFromURL = function(url) { var needle = url.replace(/^https?:/, ''); - var assets = ηm.liveHostsFiles, - asset; - for (var assetKey in assets) { + var assets = ηm.liveHostsFiles, asset; + for ( var assetKey in assets ) { asset = assets[assetKey]; - if (asset.content !== 'filters') { + if ( asset.content !== 'filters' ) { continue; } + if ( typeof asset.contentURL === 'string' ) { + if ( asset.contentURL.endsWith(needle) ) { return assetKey; } continue; } - if (typeof asset.contentURL === 'string') { - if (asset.contentURL.endsWith(needle)) { - return assetKey; - } - continue; - } - if (Array.isArray(asset.contentURL) === false) { - continue; - } - for (i = 0, n = asset.contentURL.length; i < n; i++) { - if (asset.contentURL[i].endsWith(needle)) { + if ( Array.isArray(asset.contentURL) === false ) { continue; } + for ( i = 0, n = asset.contentURL.length; i < n; i++ ) { + if ( asset.contentURL[i].endsWith(needle) ) { return assetKey; } } @@ -504,36 +473,28 @@ iter = toImportSet.values(); for (;;) { var entry = iter.next(); - if (entry.done) { - break; - } - if (importedSet.has(entry.value)) { - continue; - } + if ( entry.done ) { break; } + if ( importedSet.has(entry.value) ) { continue; } assetKey = assetKeyFromURL(entry.value); - if (assetKey === entry.value) { + if ( assetKey === entry.value ) { importedSet.add(entry.value); } this.liveHostsFiles[assetKey] = { content: 'filters', - contentURL: [assetKey], + contentURL: [ assetKey ], title: assetKey }; } externalHostsFiles = this.setToArray(importedSet).sort().join('\n'); } - if (externalHostsFiles !== this.userSettings.externalHostsFiles) { + if ( externalHostsFiles !== this.userSettings.externalHostsFiles ) { this.userSettings.externalHostsFiles = externalHostsFiles; - vAPI.storage.set({ - externalHostsFiles: externalHostsFiles - }); + vAPI.storage.set({ externalHostsFiles: externalHostsFiles }); } - vAPI.storage.set({ - 'liveHostsFiles': this.liveHostsFiles - }); + vAPI.storage.set({ 'liveHostsFiles': this.liveHostsFiles }); - if (typeof callback === 'function') { + if ( typeof callback === 'function' ) { callback(); } }; @@ -543,19 +504,19 @@ // `switches` contains the preset blacklists for which the switch must be // revisited. -ηMatrix.reloadHostsFiles = function () { +ηMatrix.reloadHostsFiles = function() { this.loadHostsFiles(); }; /******************************************************************************/ -ηMatrix.loadPublicSuffixList = function (callback) { - if (typeof callback !== 'function') { +ηMatrix.loadPublicSuffixList = function(callback) { + if ( typeof callback !== 'function' ) { callback = this.noopFunc; } - var applyPublicSuffixList = function (details) { - if (!details.error) { + var applyPublicSuffixList = function(details) { + if ( !details.error ) { publicSuffixList.parse(details.content, punycode.toASCII); } callback(); @@ -566,49 +527,47 @@ /******************************************************************************/ -ηMatrix.scheduleAssetUpdater = (function () { +ηMatrix.scheduleAssetUpdater = (function() { var timer, next = 0; - return function (updateDelay) { - if (timer) { + return function(updateDelay) { + if ( timer ) { clearTimeout(timer); timer = undefined; } - if (updateDelay === 0) { + if ( updateDelay === 0 ) { next = 0; return; } var now = Date.now(); // Use the new schedule if and only if it is earlier than the previous // one. - if (next !== 0) { + if ( next !== 0 ) { updateDelay = Math.min(updateDelay, Math.max(next - now, 0)); } next = now + updateDelay; - timer = vAPI.setTimeout(function () { + timer = vAPI.setTimeout(function() { timer = undefined; next = 0; - ηMatrix.assets.updateStart({ - delay: 120000 - }); + ηMatrix.assets.updateStart({ delay: 120000 }); }, updateDelay); }; })(); /******************************************************************************/ -ηMatrix.assetObserver = function (topic, details) { +ηMatrix.assetObserver = function(topic, details) { // Do not update filter list if not in use. - if (topic === 'before-asset-updated') { + if ( topic === 'before-asset-updated' ) { if ( this.liveHostsFiles.hasOwnProperty(details.assetKey) === false || - this.liveHostsFiles[details.assetKey].off === true + this.liveHostsFiles[details.assetKey].off === true ) { return false; } return true; } - if (topic === 'after-asset-updated') { + if ( topic === 'after-asset-updated' ) { vAPI.messaging.broadcast({ what: 'assetUpdated', key: details.assetKey, @@ -618,7 +577,7 @@ } // Update failed. - if (topic === 'asset-update-failed') { + if ( topic === 'asset-update-failed' ) { vAPI.messaging.broadcast({ what: 'assetUpdated', key: details.assetKey, @@ -628,11 +587,11 @@ } // Reload all filter lists if needed. - if (topic === 'after-assets-updated') { - if (details.assetKeys.length !== 0) { + if ( topic === 'after-assets-updated' ) { + if ( details.assetKeys.length !== 0 ) { this.loadHostsFiles(); } - if (this.userSettings.autoUpdate) { + if ( this.userSettings.autoUpdate ) { this.scheduleAssetUpdater(25200000); } else { this.scheduleAssetUpdater(0); @@ -646,10 +605,10 @@ // New asset source became available, if it's a filter list, should we // auto-select it? - if (topic === 'builtin-asset-source-added') { - if (details.entry.content === 'filters') { - if (details.entry.off !== true) { - this.saveSelectedFilterLists([details.assetKey], true); + if ( topic === 'builtin-asset-source-added' ) { + if ( details.entry.content === 'filters' ) { + if ( details.entry.off !== true ) { + this.saveSelectedFilterLists([ details.assetKey ], true); } } return; @@ -52,11 +52,11 @@ // https://github.com/gorhill/uMatrix/issues/992 if (pageURL.startsWith('wyciwyg:')) { - // Matches strings like 'wyciwyg://101/' - let filter = /^wyciwyg:\/\/\d+\//.exec(pageURL); - if (filter) { - pageURL = pageURL.slice(filter[0].length); - } + // Matches strings like 'wyciwyg://101/' + let filter = /^wyciwyg:\/\/\d+\//.exec(pageURL); + if (filter) { + pageURL = pageURL.slice(filter[0].length); + } } // If the URL is that of our "blocked page" document, return the URL of |