uBlock/src/js/logger-ui.js

1858 lines
58 KiB
JavaScript
Raw Normal View History

2015-05-09 00:28:01 +02:00
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
2018-07-22 14:14:02 +02:00
Copyright (C) 2015-present Raymond Hill
2015-05-09 00:28:01 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
2015-05-09 00:28:01 +02:00
*/
2016-03-06 16:51:06 +01:00
/* global uDom */
2015-05-09 00:28:01 +02:00
2018-07-22 14:14:02 +02:00
'use strict';
2015-05-09 00:28:01 +02:00
/******************************************************************************/
(function() {
/******************************************************************************/
const messaging = vAPI.messaging;
const logger = self.logger = { ownerId: Date.now() };
let popupLoggerBox;
let popupLoggerTooltips;
let activeTabId;
let netInspectorPaused = false;
2015-06-28 23:42:08 +02:00
/******************************************************************************/
const removeAllChildren = logger.removeAllChildren = function(node) {
2015-06-28 23:42:08 +02:00
while ( node.firstChild ) {
node.removeChild(node.firstChild);
}
};
/******************************************************************************/
const tabIdFromClassName = function(className) {
const matches = className.match(/\btab_([^ ]+)\b/);
2018-02-26 22:52:16 +01:00
if ( matches === null ) { return 0; }
if ( matches[1] === 'bts' ) { return -1; }
return parseInt(matches[1], 10);
};
const tabIdFromPageSelector = logger.tabIdFromPageSelector = function() {
const tabClass = uDom.nodeFromId('pageSelector').value;
if ( tabClass === 'tab_active' && activeTabId !== undefined ) {
return activeTabId;
}
if ( tabClass === 'tab_bts' ) { return -1; }
2018-02-26 19:59:16 +01:00
return /^tab_\d+$/.test(tabClass) ? parseInt(tabClass.slice(4), 10) : 0;
2015-06-28 23:42:08 +02:00
};
/******************************************************************************/
/******************************************************************************/
const tbody = document.querySelector('#netInspector tbody');
const trJunkyard = [];
const tdJunkyard = [];
const firstVarDataCol = 1;
const lastVarDataIndex = 6;
const reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/;
const netFilteringDialog = uDom.nodeFromId('netFilteringDialog');
2015-05-16 16:15:02 +02:00
const prettyRequestTypes = {
2015-05-09 00:28:01 +02:00
'main_frame': 'doc',
'stylesheet': 'css',
'sub_frame': 'frame',
'xmlhttprequest': 'xhr'
};
const uglyRequestTypes = {
2015-05-21 20:15:17 +02:00
'doc': 'main_frame',
'css': 'stylesheet',
'frame': 'sub_frame',
'xhr': 'xmlhttprequest'
};
const staticFilterTypes = {
'beacon': 'other',
2016-03-07 01:16:46 +01:00
'doc': 'document',
'css': 'stylesheet',
'frame': 'subdocument',
'ping': 'other',
2017-11-02 16:12:17 +01:00
'object_subrequest': 'object',
'xhr': 'xmlhttprequest'
};
let maxEntries = 5000;
let allTabIds = new Map();
let allTabIdsToken;
2015-05-09 00:28:01 +02:00
/******************************************************************************/
2015-05-16 16:15:02 +02:00
var classNameFromTabId = function(tabId) {
2018-02-26 19:59:16 +01:00
if ( tabId < 0 ) {
2015-05-16 16:15:02 +02:00
return 'tab_bts';
}
if ( tabId !== 0 ) {
2015-05-16 16:15:02 +02:00
return 'tab_' + tabId;
}
return '';
};
2015-06-26 06:08:41 +02:00
/******************************************************************************/
/******************************************************************************/
var regexFromURLFilteringResult = function(result) {
2015-05-21 20:15:17 +02:00
var beg = result.indexOf(' ');
var end = result.indexOf(' ', beg + 1);
var url = result.slice(beg + 1, end);
if ( url === '*' ) {
return new RegExp('^.*$', 'gi');
2015-05-21 20:15:17 +02:00
}
return new RegExp('^' + url.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
2015-05-21 20:15:17 +02:00
};
/******************************************************************************/
2015-05-09 00:28:01 +02:00
// Emphasize hostname in URL, as this is what matters in uMatrix's rules.
var nodeFromURL = function(url, re) {
if ( re instanceof RegExp === false ) {
2015-05-09 00:28:01 +02:00
return document.createTextNode(url);
}
var matches = re.exec(url);
if ( matches === null || matches[0].length === 0 ) {
return document.createTextNode(url);
}
var node = renderedURLTemplate.cloneNode(true);
node.childNodes[0].textContent = url.slice(0, matches.index);
node.childNodes[1].textContent = url.slice(matches.index, re.lastIndex);
node.childNodes[2].textContent = url.slice(re.lastIndex);
return node;
};
var renderedURLTemplate = document.querySelector('#renderedURLTemplate > span');
/******************************************************************************/
const createCellAt = function(tr, index) {
let td = tr.cells[index];
const mustAppend = !td;
2015-05-09 00:28:01 +02:00
if ( mustAppend ) {
td = tdJunkyard.pop();
}
if ( td ) {
td.removeAttribute('colspan');
td.removeAttribute('data-parties');
2015-05-09 00:28:01 +02:00
td.textContent = '';
} else {
td = document.createElement('td');
}
if ( mustAppend ) {
tr.appendChild(td);
}
return td;
};
/******************************************************************************/
var createRow = function(layout) {
let tr = trJunkyard.pop();
2015-05-09 00:28:01 +02:00
if ( tr ) {
tr.className = '';
tr.removeAttribute('data-tabhn');
tr.removeAttribute('data-dochn');
tr.removeAttribute('data-filter');
tr.removeAttribute('data-tabid');
2015-05-09 00:28:01 +02:00
} else {
tr = document.createElement('tr');
}
let index = 0;
for ( ; index < firstVarDataCol; index++ ) {
2015-05-09 00:28:01 +02:00
createCellAt(tr, index);
}
let i = 1, span = 1, td;
2015-05-09 00:28:01 +02:00
for (;;) {
td = createCellAt(tr, index);
if ( i === lastVarDataIndex ) { break; }
2015-05-09 00:28:01 +02:00
if ( layout.charAt(i) !== '1' ) {
span += 1;
} else {
if ( span !== 1 ) {
td.setAttribute('colspan', span);
}
index += 1;
span = 1;
}
i += 1;
}
if ( span !== 1 ) {
td.setAttribute('colspan', span);
}
index += 1;
while ( (td = tr.cells[index]) ) {
2015-05-09 00:28:01 +02:00
tdJunkyard.push(tr.removeChild(td));
}
return tr;
};
/******************************************************************************/
var padTo2 = function(v) {
return v < 10 ? '0' + v : v;
};
/******************************************************************************/
const createGap = function(tabId, url) {
const tr = createRow('1');
tr.setAttribute('data-tabid', tabId);
tr.classList.add('tab_' + tabId);
tr.classList.add('maindoc');
2015-05-09 00:28:01 +02:00
tr.cells[firstVarDataCol].textContent = url;
tbody.insertBefore(tr, tbody.firstChild);
};
/******************************************************************************/
var renderNetLogEntry = function(tr, details) {
const trcl = tr.classList;
const type = details.type;
const url = details.url;
let td;
2015-05-09 00:28:01 +02:00
// 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 ( type === 'main_frame' ) {
createGap(details.tabId, url);
2015-05-09 00:28:01 +02:00
}
tr.classList.add('cat_' + details.realm);
2015-05-21 20:15:17 +02:00
let filter = details.filter || undefined;
let filteringType;
if ( filter !== undefined ) {
if ( typeof filter.source === 'string' ) {
filteringType = filter.source;
trcl.add(filteringType);
}
2015-05-09 00:28:01 +02:00
}
if ( filter !== undefined ) {
td = tr.cells[1];
if ( filteringType === 'static' ) {
td.textContent = filter.raw;
trcl.add('canLookup');
tr.setAttribute('data-filter', filter.compiled);
} else if ( filteringType === 'cosmetic' ) {
td.textContent = filter.raw;
trcl.add('canLookup');
} else {
td.textContent = filter.raw;
}
}
if ( filter !== undefined ) {
td = tr.cells[2];
if ( filter.result === 1 ) {
trcl.add('blocked');
td.textContent = '--';
} else if ( filter.result === 2 ) {
trcl.add('allowed');
td.textContent = '++';
} else if ( filter.result === 3 ) {
trcl.add('nooped');
td.textContent = '**';
} else if ( filteringType === 'redirect' ) {
trcl.add('redirect');
td.textContent = '<<';
}
2015-05-09 00:28:01 +02:00
}
if ( details.tabHostname ) {
tr.setAttribute('data-tabhn', details.tabHostname);
}
if ( details.docHostname ) {
tr.setAttribute('data-dochn', details.docHostname);
tr.cells[3].textContent = details.docHostname;
}
// Partyness
if ( details.realm === 'net' && details.domain !== undefined ) {
td = tr.cells[4];
let text = '';
if ( details.tabDomain !== undefined ) {
text += details.domain === details.tabDomain ? '1' : '3';
} else {
text += '?';
}
if ( details.docDomain !== details.tabDomain ) {
text += ',';
if ( details.docDomain !== undefined ) {
text += details.domain === details.docDomain ? '1' : '3';
} else {
text += '?';
}
}
td.textContent = text;
let indent = '\t';
text = details.tabDomain;
if ( details.docDomain !== details.tabDomain ) {
text += ` \u21d2\n\t${details.docDomain}`;
indent = '\t\t';
}
text += ` \u21d2\n${indent}${details.domain}`;
td.setAttribute('data-parties', text);
}
tr.cells[5].textContent = (prettyRequestTypes[type] || type);
let re = null;
if ( filteringType === 'static' ) {
re = new RegExp(filter.regex, 'gi');
} else if ( filteringType === 'dynamicUrl' ) {
re = regexFromURLFilteringResult(filter.rule.join(' '));
}
tr.cells[6].appendChild(nodeFromURL(url, re));
2015-05-09 00:28:01 +02:00
};
/******************************************************************************/
var renderLogEntry = function(details) {
const fvdc = firstVarDataCol;
let tr;
2015-05-09 00:28:01 +02:00
if ( details.error !== undefined ) {
2015-05-09 00:28:01 +02:00
tr = createRow('1');
tr.cells[fvdc].textContent = details.error;
} else if ( details.url !== undefined ) {
tr = createRow('111111');
renderNetLogEntry(tr, details);
} else {
2015-05-09 00:28:01 +02:00
tr = createRow('1');
tr.cells[fvdc].textContent = '???';
2015-05-09 00:28:01 +02:00
}
// Fields common to all rows.
const time = logDate;
time.setTime(details.tstamp - logDateTimezoneOffset);
2016-12-03 15:21:31 +01:00
tr.cells[0].textContent = padTo2(time.getUTCHours()) + ':' +
2017-01-12 14:45:46 +01:00
padTo2(time.getUTCMinutes()) + ':' +
padTo2(time.getSeconds());
2015-05-09 00:28:01 +02:00
if ( details.tabId ) {
tr.setAttribute('data-tabid', details.tabId);
tr.classList.add(classNameFromTabId(details.tabId));
2015-05-09 00:28:01 +02:00
}
rowFilterer.filterOne(tr, true);
tbody.insertBefore(tr, tbody.firstChild);
return tr;
2015-05-09 00:28:01 +02:00
};
2016-12-03 15:21:31 +01:00
// Reuse date objects.
const logDate = new Date();
const logDateTimezoneOffset = logDate.getTimezoneOffset() * 60000;
2016-12-03 15:21:31 +01:00
2015-05-09 00:28:01 +02:00
/******************************************************************************/
const renderLogEntries = function(response) {
2015-05-09 00:28:01 +02:00
document.body.classList.toggle('colorBlind', response.colorBlind);
const entries = response.entries;
if ( entries.length === 0 ) { return; }
2015-05-09 00:28:01 +02:00
// Preserve scroll position
const height = tbody.offsetHeight;
const tabIds = allTabIds;
for ( const entry of entries ) {
const details = JSON.parse(entry.details);
const tr = renderLogEntry(details);
// https://github.com/gorhill/uBlock/issues/1613#issuecomment-217637122
// Unlikely, but it may happen: mark as void if associated tab no
// longer exist.
if ( details.tabId && tabIds.has(details.tabId) === false ) {
tr.classList.add('void');
2015-05-09 00:28:01 +02:00
}
}
// Prevent logger from growing infinitely and eating all memory. For
// instance someone could forget that it is left opened for some
// dynamically refreshed pages.
truncateLog(maxEntries);
2015-06-26 06:08:41 +02:00
// Follow waterfall if not observing top of waterfall.
const yDelta = tbody.offsetHeight - height;
if ( yDelta === 0 ) { return; }
const container = uDom.nodeFromSelector('#netInspector .vscrollable');
2015-06-26 06:08:41 +02:00
if ( container.scrollTop !== 0 ) {
container.scrollTop += yDelta;
2015-05-09 00:28:01 +02:00
}
};
/******************************************************************************/
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;
};
})();
/******************************************************************************/
const synchronizeTabIds = function(newTabIds) {
const select = uDom.nodeFromId('pageSelector');
const selectValue = select.value;
const oldTabIds = allTabIds;
const autoDeleteVoidRows = selectValue === 'tab_active';
let rowVoided = false;
for ( const tabId of oldTabIds.keys() ) {
2018-02-26 19:59:16 +01:00
if ( newTabIds.has(tabId) ) { continue; }
2015-05-16 16:15:02 +02:00
// Mark or remove voided rows
const trs = uDom('.tab_' + tabId);
2015-05-16 16:15:02 +02:00
if ( autoDeleteVoidRows ) {
toJunkyard(trs);
} else {
trs.addClass('void');
2015-05-16 16:15:02 +02:00
rowVoided = true;
}
// Remove popup if it is currently bound to a removed tab.
if ( tabId === popupManager.tabId ) {
popupManager.toggleOff();
}
}
const tabIds = Array.from(newTabIds.keys()).sort(function(a, b) {
2018-02-26 19:59:16 +01:00
return newTabIds.get(a).localeCompare(newTabIds.get(b));
2015-05-16 16:15:02 +02:00
});
let j = 3;
for ( let i = 0; i < tabIds.length; i++ ) {
const tabId = tabIds[i];
2018-02-26 19:59:16 +01:00
if ( tabId < 0 ) { continue; }
let option = select.options[j];
2015-05-16 16:15:02 +02:00
if ( !option ) {
option = document.createElement('option');
select.appendChild(option);
}
// Truncate too long labels.
2018-02-26 19:59:16 +01:00
option.textContent = newTabIds.get(tabId).slice(0, 80);
2015-05-16 16:15:02 +02:00
option.value = classNameFromTabId(tabId);
if ( option.value === selectValue ) {
select.selectedIndex = j;
2015-05-16 16:15:02 +02:00
option.setAttribute('selected', '');
} else {
option.removeAttribute('selected');
}
j += 1;
2015-05-16 16:15:02 +02:00
}
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;
updateCurrentTabTitle();
2015-05-16 16:15:02 +02:00
return rowVoided;
};
/******************************************************************************/
2015-05-09 00:28:01 +02:00
var truncateLog = function(size) {
if ( size === 0 ) {
size = 5000;
}
var tbody = document.querySelector('#netInspector tbody');
2015-05-09 00:28:01 +02:00
size = Math.min(size, 10000);
var tr;
while ( tbody.childElementCount > size ) {
tr = tbody.lastElementChild;
trJunkyard.push(tbody.removeChild(tr));
}
};
/******************************************************************************/
const onLogBufferRead = function(response) {
2018-01-08 20:29:39 +01:00
if ( !response || response.unavailable ) {
readLogBufferAsync();
return;
}
// Disable tooltips?
if (
popupLoggerTooltips === undefined &&
response.tooltips !== undefined
) {
popupLoggerTooltips = response.tooltips;
if ( popupLoggerTooltips === false ) {
uDom('[data-i18n-title]').attr('title', '');
}
}
// Tab id of currently active tab
let activeTabIdChanged = false;
if ( response.activeTabId ) {
activeTabIdChanged = response.activeTabId !== activeTabId;
activeTabId = response.activeTabId;
}
2015-05-09 00:28:01 +02:00
// 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);
}
2015-05-09 00:28:01 +02:00
// Neuter rows for which a tab does not exist anymore
let rowVoided = false;
if ( response.tabIds !== undefined ) {
rowVoided = synchronizeTabIds(response.tabIds);
allTabIdsToken = response.tabIdsToken;
}
if ( activeTabIdChanged ) {
pageSelectorFromURLHash();
}
2015-08-01 17:30:54 +02:00
if ( netInspectorPaused === false ) {
renderLogEntries(response);
}
2015-05-09 00:28:01 +02:00
if ( rowVoided ) {
uDom('#clean').toggleClass(
'disabled',
tbody.querySelector('#netInspector tr[data-tabid].void') === null
);
}
2015-05-09 00:28:01 +02:00
// Synchronize toolbar with content of log
uDom('#clear').toggleClass(
'disabled',
tbody.querySelector('tr') === null
);
2018-01-08 20:29:39 +01:00
readLogBufferAsync();
2015-05-09 00:28:01 +02:00
};
/******************************************************************************/
// This can be called only once, at init time. After that, this will be called
// 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.
const readLogBuffer = function() {
if ( logger.ownerId === undefined ) { return; }
const msg = {
what: 'readAll',
ownerId: logger.ownerId,
tabIdsToken: allTabIdsToken,
};
if (
popupLoggerBox instanceof Object &&
(
self.screenX !== popupLoggerBox.x ||
self.screenY !== popupLoggerBox.y ||
self.outerWidth !== popupLoggerBox.w ||
self.outerHeight !== popupLoggerBox.h
)
) {
popupLoggerBox.x = self.screenX;
popupLoggerBox.y = self.screenY;
popupLoggerBox.w = self.outerWidth;
popupLoggerBox.h = self.outerHeight;
msg.popupLoggerBoxChanged = true;
}
vAPI.messaging.send('loggerUI', msg, onLogBufferRead);
2015-05-09 00:28:01 +02:00
};
const readLogBufferAsync = function() {
if ( logger.ownerId === undefined ) { return; }
2018-01-08 20:29:39 +01:00
vAPI.setTimeout(readLogBuffer, 1200);
};
2015-05-09 00:28:01 +02:00
/******************************************************************************/
let pageSelectorChanged = function() {
let select = uDom.nodeFromId('pageSelector');
window.location.replace('#' + select.value);
2015-08-01 17:30:54 +02:00
pageSelectorFromURLHash();
2015-05-16 16:15:02 +02:00
};
let pageSelectorFromURLHash = (function() {
let lastTabClass = '';
let lastEffectiveTabClass = '';
let reActiveTabId = /^(tab_[^+]+)\+(.+)$/;
2015-08-01 17:30:54 +02:00
let selectRows = function(tabClass) {
let effectiveTabClass = tabClass;
if ( tabClass === 'tab_active' ) {
if ( activeTabId === undefined ) { return; }
effectiveTabClass = 'tab_' + activeTabId;
}
if ( effectiveTabClass === lastEffectiveTabClass ) { return; }
lastEffectiveTabClass = effectiveTabClass;
document.dispatchEvent(new Event('tabIdChanged'));
let style = uDom.nodeFromId('tabFilterer');
let sheet = style.sheet;
while ( sheet.cssRules.length !== 0 ) {
sheet.deleteRule(0);
2015-08-01 17:30:54 +02:00
}
if ( effectiveTabClass === '' ) { return; }
sheet.insertRule(
'#netInspector tr:not(.' + effectiveTabClass + '):not(.tab_bts) ' +
'{display:none;}',
0
);
updateCurrentTabTitle();
};
return function() {
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;
2015-08-01 17:30:54 +02:00
let select = uDom.nodeFromId('pageSelector');
let option = select.querySelector('option[value="' + tabClass + '"]');
2015-08-01 17:30:54 +02:00
if ( option === null ) {
window.location.hash = '';
2015-08-01 17:30:54 +02:00
tabClass = '';
option = select.options[0];
}
select.selectedIndex = option.index;
select.value = option.value;
uDom('.needdom').toggleClass(
2015-08-01 17:30:54 +02:00
'disabled',
tabClass === '' || tabClass === 'tab_bts'
);
uDom('.needscope').toggleClass(
'disabled',
tabClass === ''
);
2015-08-01 17:30:54 +02:00
};
})();
/******************************************************************************/
var reloadTab = function(ev) {
var tabId = tabIdFromPageSelector();
if ( tabId <= 0 ) { return; }
messaging.send('loggerUI', {
what: 'reloadTab',
tabId: tabId,
bypassCache: ev && (ev.ctrlKey || ev.metaKey || ev.shiftKey)
});
2015-05-16 16:15:02 +02:00
};
/******************************************************************************/
2015-05-09 00:28:01 +02:00
var onMaxEntriesChanged = function() {
2015-06-15 01:40:55 +02:00
var input = this;
2015-05-09 00:28:01 +02:00
try {
2015-06-15 01:40:55 +02:00
maxEntries = parseInt(input.value, 10);
if ( maxEntries === 0 || isNaN(maxEntries) ) {
maxEntries = 1000;
2015-05-09 00:28:01 +02:00
}
} catch (e) {
2015-06-15 01:40:55 +02:00
maxEntries = 1000;
2015-05-09 00:28:01 +02:00
}
2015-06-15 01:40:55 +02:00
maxEntries = Math.min(maxEntries, 5000);
maxEntries = Math.max(maxEntries, 10);
input.value = maxEntries.toString(10);
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'userSettings',
name: 'requestLogMaxEntries',
value: maxEntries
}
);
2015-05-09 00:28:01 +02:00
truncateLog(maxEntries);
};
2015-05-21 20:15:17 +02:00
/******************************************************************************/
/******************************************************************************/
var netFilteringManager = (function() {
var targetRow = null;
var dialog = null;
var createdStaticFilters = {};
var targetType;
var targetURLs = [];
var targetFrameHostname;
var targetPageHostname;
var targetTabId;
var targetDomain;
var targetPageDomain;
var targetFrameDomain;
2015-05-21 20:15:17 +02:00
var uglyTypeFromSelector = function(pane) {
var prettyType = selectValue('select.type.' + pane);
if ( pane === 'static' ) {
return staticFilterTypes[prettyType] || prettyType;
}
2015-05-21 20:15:17 +02:00
return uglyRequestTypes[prettyType] || prettyType;
};
var selectNode = function(selector) {
return dialog.querySelector(selector);
};
var selectValue = function(selector) {
return selectNode(selector).value || '';
};
var staticFilterNode = function() {
return dialog.querySelector('div.containers > div.static textarea');
};
2015-05-21 20:15:17 +02:00
var onColorsReady = function(response) {
document.body.classList.toggle('dirty', response.dirty);
var colorEntries = response.colors;
var colorEntry, node;
for ( var url in colorEntries ) {
if ( colorEntries.hasOwnProperty(url) === false ) {
continue;
}
colorEntry = colorEntries[url];
node = dialog.querySelector('.dynamic .entry .action[data-url="' + url + '"]');
2015-05-21 20:15:17 +02:00
if ( node === null ) {
continue;
}
node.classList.toggle('allow', colorEntry.r === 2);
node.classList.toggle('noop', colorEntry.r === 3);
node.classList.toggle('block', colorEntry.r === 1);
node.classList.toggle('own', colorEntry.own);
}
};
var colorize = function() {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'getURLFilteringData',
context: selectValue('select.dynamic.origin'),
urls: targetURLs,
type: uglyTypeFromSelector('dynamic')
},
onColorsReady
);
2015-05-21 20:15:17 +02:00
};
var parseStaticInputs = function() {
2017-07-03 16:20:47 +02:00
var filter = '',
options = [],
block = selectValue('select.static.action') === '';
if ( !block ) {
filter = '@@';
}
var value = selectValue('select.static.url');
if ( value !== '' ) {
2017-07-03 16:20:47 +02:00
if ( value.slice(-1) === '/' ) {
value += '*';
} else if ( /[/?]/.test(value) === false ) {
value += '^';
}
value = '||' + value;
}
2017-07-03 16:20:47 +02:00
filter += value;
value = selectValue('select.static.type');
if ( value !== '' ) {
options.push(uglyTypeFromSelector('static'));
}
value = selectValue('select.static.origin');
if ( value !== '' ) {
if ( value === targetDomain ) {
options.push('first-party');
} else {
options.push('domain=' + value);
}
}
if ( block && selectValue('select.static.importance') !== '' ) {
options.push('important');
}
if ( options.length ) {
filter += '$' + options.join(',');
}
staticFilterNode().value = filter;
updateWidgets();
};
var updateWidgets = function() {
var value = staticFilterNode().value;
dialog.querySelector('#createStaticFilter').classList.toggle(
'disabled',
createdStaticFilters.hasOwnProperty(value) || value === ''
);
};
2015-05-21 20:15:17 +02:00
var onClick = function(ev) {
var target = ev.target;
// click outside the dialog proper
if ( target.classList.contains('modalDialog') ) {
2015-05-21 20:15:17 +02:00
toggleOff();
return;
}
ev.stopPropagation();
var tcl = target.classList;
var value;
// Select a mode
if ( tcl.contains('header') ) {
if ( tcl.contains('selected') ) {
return;
}
uDom('.header').removeClass('selected');
uDom('.container').removeClass('selected');
value = target.getAttribute('data-container');
uDom('.header.' + value).addClass('selected');
uDom('.container.' + value).addClass('selected');
return;
}
// Create static filter
if ( target.id === 'createStaticFilter' ) {
value = staticFilterNode().value;
// Avoid duplicates
if ( createdStaticFilters.hasOwnProperty(value) ) {
return;
}
createdStaticFilters[value] = true;
if ( value !== '' ) {
var d = new Date();
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'createUserFilter',
2016-07-07 22:42:01 +02:00
pageDomain: targetPageDomain,
2016-03-06 16:51:06 +01:00
filters: '! ' + d.toLocaleString() + ' ' + targetPageDomain + '\n' + value
}
);
}
updateWidgets();
return;
}
2015-05-21 20:15:17 +02:00
// Save url filtering rule(s)
if ( target.id === 'saveRules' ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'saveURLFilteringRules',
context: selectValue('select.dynamic.origin'),
urls: targetURLs,
type: uglyTypeFromSelector('dynamic')
},
colorize
);
2015-05-21 20:15:17 +02:00
return;
}
2015-05-22 14:05:55 +02:00
var persist = !!ev.ctrlKey || !!ev.metaKey;
2015-05-21 20:15:17 +02:00
// Remove url filtering rule
if ( tcl.contains('action') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'setURLFilteringRule',
context: selectValue('select.dynamic.origin'),
url: target.getAttribute('data-url'),
type: uglyTypeFromSelector('dynamic'),
action: 0,
persist: persist
},
colorize
);
2015-05-21 20:15:17 +02:00
return;
}
// add "allow" url filtering rule
if ( tcl.contains('allow') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'setURLFilteringRule',
context: selectValue('select.dynamic.origin'),
url: target.parentNode.getAttribute('data-url'),
type: uglyTypeFromSelector('dynamic'),
action: 2,
persist: persist
},
colorize
);
2015-05-21 20:15:17 +02:00
return;
}
// add "block" url filtering rule
if ( tcl.contains('noop') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'setURLFilteringRule',
context: selectValue('select.dynamic.origin'),
url: target.parentNode.getAttribute('data-url'),
type: uglyTypeFromSelector('dynamic'),
action: 3,
persist: persist
},
colorize
);
2015-05-21 20:15:17 +02:00
return;
}
// add "block" url filtering rule
if ( tcl.contains('block') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'setURLFilteringRule',
context: selectValue('select.dynamic.origin'),
url: target.parentNode.getAttribute('data-url'),
type: uglyTypeFromSelector('dynamic'),
action: 1,
persist: persist
},
colorize
);
2015-05-21 20:15:17 +02:00
return;
}
// Force a reload of the tab
if ( tcl.contains('reload') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'reloadTab',
tabId: targetTabId
}
);
return;
}
// Hightlight corresponding element in target web page
if ( tcl.contains('picker') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'launchElementPicker',
tabId: targetTabId,
targetURL: 'img\t' + targetURLs[0],
select: true
}
);
return;
}
};
var onSelectChange = function(ev) {
var target = ev.target;
var tcl = target.classList;
if ( tcl.contains('dynamic') ) {
colorize();
return;
}
if ( tcl.contains('static') ) {
parseStaticInputs();
return;
}
};
var onInputChange = function() {
updateWidgets();
};
var createPreview = function(type, url) {
// First, whether picker can be used
dialog.querySelector('.picker').classList.toggle(
'hide',
2018-02-26 19:59:16 +01:00
targetTabId < 0 ||
targetType !== 'image' ||
/(?:^| )[dlsu]b(?: |$)/.test(targetRow.className)
);
var preview = null;
if ( type === 'image' ) {
preview = document.createElement('img');
preview.setAttribute('src', url);
}
var container = dialog.querySelector('div.preview');
container.classList.toggle('hide', preview === null);
if ( preview === null ) {
return;
}
container.appendChild(preview);
2015-05-21 20:15:17 +02:00
};
2016-03-28 15:31:53 +02:00
// https://github.com/gorhill/uBlock/issues/1511
var shortenLongString = function(url, max) {
var urlLen = url.length;
if ( urlLen <= max ) {
return url;
}
var n = urlLen - max - 1;
var i = (urlLen - n) / 2 | 0;
return url.slice(0, i) + '…' + url.slice(i + n);
};
// Build list of candidate URLs
var createTargetURLs = function(url) {
var urls = [];
var matches = reRFC3986.exec(url);
if ( matches === null || !matches[1] || !matches[2] ) {
return urls;
}
// Shortest URL for a valid URL filtering rule
var rootURL = matches[1] + matches[2];
urls.unshift(rootURL);
var path = matches[3] || '';
var pos = path.charAt(0) === '/' ? 1 : 0;
while ( pos < path.length ) {
pos = path.indexOf('/', pos + 1);
if ( pos === -1 ) {
pos = path.length;
}
2017-07-03 16:20:47 +02:00
urls.unshift(rootURL + path.slice(0, pos + 1));
}
var query = matches[4] || '';
if ( query !== '') {
urls.unshift(rootURL + path + query);
}
return urls;
};
// Fill dynamic URL filtering pane
var fillDynamicPane = function() {
var select;
// Fill context selector
select = selectNode('select.dynamic.origin');
removeAllChildren(select);
fillOriginSelect(select, targetPageHostname, targetPageDomain);
var option = document.createElement('option');
option.textContent = '*';
option.setAttribute('value', '*');
select.appendChild(option);
2015-05-21 20:15:17 +02:00
// Fill type selector
select = selectNode('select.dynamic.type');
select.options[0].textContent = targetType;
select.options[0].setAttribute('value', targetType);
select.selectedIndex = 0;
2015-05-21 20:15:17 +02:00
// Fill entries
var menuEntryTemplate = dialog.querySelector('table.toolbar tr.entry');
var tbody = dialog.querySelector('div.dynamic table.entries tbody');
var url, menuEntry;
for ( var i = 0; i < targetURLs.length; i++ ) {
url = targetURLs[i];
menuEntry = menuEntryTemplate.cloneNode(true);
menuEntry.cells[0].children[0].setAttribute('data-url', url);
2016-03-28 15:31:53 +02:00
menuEntry.cells[1].textContent = shortenLongString(url, 128);
tbody.appendChild(menuEntry);
2015-05-21 20:15:17 +02:00
}
colorize();
};
2015-05-21 20:15:17 +02:00
var fillOriginSelect = function(select, hostname, domain) {
var option, pos;
var template = vAPI.i18n('loggerStaticFilteringSentencePartOrigin');
var value = hostname;
2015-05-21 20:15:17 +02:00
for (;;) {
option = document.createElement('option');
option.setAttribute('value', value);
option.textContent = template.replace('{{origin}}', value);
select.appendChild(option);
if ( value === domain ) {
break;
}
pos = value.indexOf('.');
2015-05-21 20:15:17 +02:00
if ( pos === -1 ) {
break;
}
value = value.slice(pos + 1);
2015-05-21 20:15:17 +02:00
}
};
2015-05-21 20:15:17 +02:00
// Fill static filtering pane
var fillStaticPane = function() {
var template = vAPI.i18n('loggerStaticFilteringSentence');
var rePlaceholder = /\{\{[^}]+?\}\}/g;
var nodes = [];
var match, pos = 0;
2017-07-03 16:20:47 +02:00
var select, option, n, i, value;
for (;;) {
match = rePlaceholder.exec(template);
if ( match === null ) {
break;
}
if ( pos !== match.index ) {
nodes.push(document.createTextNode(template.slice(pos, match.index)));
}
pos = rePlaceholder.lastIndex;
switch ( match[0] ) {
case '{{br}}':
nodes.push(document.createElement('br'));
break;
2015-05-21 20:15:17 +02:00
case '{{action}}':
select = document.createElement('select');
select.className = 'static action';
option = document.createElement('option');
option.setAttribute('value', '');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartBlock');
select.appendChild(option);
option = document.createElement('option');
option.setAttribute('value', '@@');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartAllow');
select.appendChild(option);
nodes.push(select);
break;
2015-05-21 20:15:17 +02:00
case '{{type}}':
select = document.createElement('select');
select.className = 'static type';
option = document.createElement('option');
option.setAttribute('value', targetType);
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartType').replace('{{type}}', targetType);
select.appendChild(option);
option = document.createElement('option');
option.setAttribute('value', '');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartAnyType');
select.appendChild(option);
nodes.push(select);
break;
case '{{url}}':
select = document.createElement('select');
select.className = 'static url';
2017-07-03 16:20:47 +02:00
for ( i = 0, n = targetURLs.length; i < n; i++ ) {
value = targetURLs[i].replace(/^[a-z-]+:\/\//, '');
option = document.createElement('option');
option.setAttribute('value', value);
2016-03-28 15:31:53 +02:00
option.textContent = shortenLongString(value, 128);
select.appendChild(option);
}
nodes.push(select);
break;
2015-05-21 20:15:17 +02:00
case '{{origin}}':
select = document.createElement('select');
select.className = 'static origin';
fillOriginSelect(select, targetFrameHostname, targetFrameDomain);
option = document.createElement('option');
option.setAttribute('value', '');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartAnyOrigin');
select.appendChild(option);
nodes.push(select);
break;
case '{{importance}}':
select = document.createElement('select');
select.className = 'static importance';
option = document.createElement('option');
option.setAttribute('value', '');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartNotImportant');
select.appendChild(option);
option = document.createElement('option');
option.setAttribute('value', 'important');
option.textContent = vAPI.i18n('loggerStaticFilteringSentencePartImportant');
select.appendChild(option);
nodes.push(select);
break;
2015-05-21 20:15:17 +02:00
default:
break;
}
}
if ( pos < template.length ) {
nodes.push(document.createTextNode(template.slice(pos)));
2015-05-21 20:15:17 +02:00
}
var parent = dialog.querySelector('div.containers > div.static > p:first-of-type');
removeAllChildren(parent);
for ( i = 0; i < nodes.length; i++ ) {
parent.appendChild(nodes[i]);
}
parseStaticInputs();
};
2015-05-21 20:15:17 +02:00
var fillDialog = function(domains) {
targetDomain = domains[0];
targetPageDomain = domains[1];
targetFrameDomain = domains[2];
2015-05-21 20:15:17 +02:00
createPreview(targetType, targetURLs[0]);
fillDynamicPane();
fillStaticPane();
document.body.appendChild(netFilteringDialog);
netFilteringDialog.addEventListener('click', onClick, true);
netFilteringDialog.addEventListener('change', onSelectChange, true);
netFilteringDialog.addEventListener('input', onInputChange, true);
};
var toggleOn = function(ev) {
dialog = netFilteringDialog.querySelector('.dialog');
targetRow = ev.target.parentElement;
targetTabId = tabIdFromClassName(targetRow.className);
targetType = targetRow.cells[5].textContent.trim() || '';
targetURLs = createTargetURLs(targetRow.cells[6].textContent);
targetPageHostname = targetRow.getAttribute('data-tabhn') || '';
targetFrameHostname = targetRow.getAttribute('data-dochn') || '';
// We need the root domain names for best user experience.
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'getDomainNames',
targets: [targetURLs[0], targetPageHostname, targetFrameHostname]
},
fillDialog
);
2015-05-21 20:15:17 +02:00
};
var toggleOff = function() {
removeAllChildren(dialog.querySelector('div.preview'));
removeAllChildren(dialog.querySelector('div.dynamic table.entries tbody'));
dialog = null;
targetRow = null;
targetURLs = [];
netFilteringDialog.removeEventListener('click', onClick, true);
netFilteringDialog.removeEventListener('change', onSelectChange, true);
netFilteringDialog.removeEventListener('input', onInputChange, true);
document.body.removeChild(netFilteringDialog);
};
return {
toggleOn: toggleOn
};
})();
// https://www.youtube.com/watch?v=XyNYrmmdUd4
/******************************************************************************/
/******************************************************************************/
var reverseLookupManager = (function() {
2018-07-22 14:14:02 +02:00
let filterFinderDialog = uDom.nodeFromId('filterFinderDialog');
let rawFilter = '';
2018-07-22 14:14:02 +02:00
let removeAllChildren = function(node) {
while ( node.firstChild ) {
node.removeChild(node.firstChild);
}
};
2018-07-22 14:14:02 +02:00
// Clicking outside the dialog will close the dialog
let onClick = function(ev) {
if ( ev.target.classList.contains('modalDialog') ) {
toggleOff();
return;
}
ev.stopPropagation();
};
2018-07-22 14:14:02 +02:00
let nodeFromFilter = function(filter, lists) {
if ( Array.isArray(lists) === false || lists.length === 0 ) {
2018-07-22 14:14:02 +02:00
return;
}
2018-07-22 14:14:02 +02:00
let p = document.createElement('p');
2018-07-22 14:14:02 +02:00
vAPI.i18n.safeTemplateToDOM(
'loggerStaticFilteringFinderSentence1',
{ filter: filter },
p
);
let ul = document.createElement('ul');
for ( let list of lists ) {
let li = document.querySelector('#filterFinderListEntry > li')
.cloneNode(true);
let a = li.querySelector('a:nth-of-type(1)');
a.href += encodeURIComponent(list.assetKey);
a.textContent = list.title;
if ( list.supportURL ) {
2018-07-22 14:14:02 +02:00
a = li.querySelector('a:nth-of-type(2)');
a.setAttribute('href', list.supportURL);
}
ul.appendChild(li);
}
p.appendChild(ul);
return p;
};
2018-07-22 14:14:02 +02:00
let reverseLookupDone = function(response) {
if ( response instanceof Object === false ) {
response = {};
}
2018-07-22 14:14:02 +02:00
let dialog = filterFinderDialog.querySelector('.dialog');
removeAllChildren(dialog);
2018-07-22 14:14:02 +02:00
for ( let filter in response ) {
let p = nodeFromFilter(filter, response[filter]);
if ( p === undefined ) { continue; }
dialog.appendChild(p);
}
2018-07-22 14:14:02 +02:00
// https://github.com/gorhill/uBlock/issues/2179
if ( dialog.childElementCount === 0 ) {
vAPI.i18n.safeTemplateToDOM(
'loggerStaticFilteringFinderSentence2',
{ filter: rawFilter },
dialog
);
}
document.body.appendChild(filterFinderDialog);
filterFinderDialog.addEventListener('click', onClick, true);
};
2018-07-22 14:14:02 +02:00
let toggleOn = function(ev) {
let row = ev.target.parentElement;
rawFilter = row.cells[1].textContent;
2018-07-22 14:14:02 +02:00
if ( rawFilter === '' ) { return; }
if ( row.classList.contains('cat_net') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'listsFromNetFilter',
compiledFilter: row.getAttribute('data-filter') || '',
rawFilter: rawFilter
},
reverseLookupDone
);
} else if ( row.classList.contains('cat_cosmetic') ) {
2016-03-06 16:51:06 +01:00
messaging.send(
'loggerUI',
{
what: 'listsFromCosmeticFilter',
url: row.cells[6].textContent,
2016-03-06 16:51:06 +01:00
rawFilter: rawFilter,
},
reverseLookupDone
);
}
};
2018-07-22 14:14:02 +02:00
let toggleOff = function() {
filterFinderDialog.removeEventListener('click', onClick, true);
document.body.removeChild(filterFinderDialog);
2018-07-22 14:14:02 +02:00
rawFilter = '';
2015-05-21 20:15:17 +02:00
};
return {
toggleOn: toggleOn
};
})();
/******************************************************************************/
2015-05-09 00:28:01 +02:00
/******************************************************************************/
const rowFilterer = (function() {
const userFilters = [];
const builtinFilters = [];
let filters = [];
2015-05-09 00:28:01 +02:00
const parseInput = function() {
userFilters.length = 0;
const rawParts =
uDom.nodeFromSelector('#filterInput > input')
.value
.trim()
.split(/\s+/);
const n = rawParts.length;
const reStrs = [];
let not = false;
for ( let i = 0; i < n; i++ ) {
let rawPart = rawParts[i];
2015-05-23 15:27:24 +02:00
if ( rawPart.charAt(0) === '!' ) {
if ( reStrs.length === 0 ) {
not = true;
}
rawPart = rawPart.slice(1);
}
let reStr = '';
if ( rawPart.startsWith('/') && rawPart.endsWith('/') ) {
reStr = rawPart.slice(1, -1);
try {
new RegExp(reStr);
} catch(ex) {
reStr = '';
}
2015-05-09 00:28:01 +02:00
}
if ( reStr === '' ) {
const hardBeg = rawPart.startsWith('|');
if ( hardBeg ) {
rawPart = rawPart.slice(1);
}
const hardEnd = rawPart.endsWith('|');
if ( hardEnd ) {
rawPart = rawPart.slice(0, -1);
}
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
reStr = rawPart.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// https://github.com/orgs/uBlockOrigin/teams/ublock-issues-volunteers/discussions/51
// Be more flexible when interpreting leading/trailing pipes,
// as leading/trailing pipes are often used in static filters.
if ( hardBeg ) {
reStr = reStr !== '' ? '(?:^|\\s|\\|)' + reStr : '\\|';
}
if ( hardEnd ) {
reStr += '(?:\\||\\s|$)';
}
2015-05-09 00:28:01 +02:00
}
if ( reStr === '' ) { continue; }
2015-05-23 15:27:24 +02:00
reStrs.push(reStr);
2015-05-28 02:48:04 +02:00
if ( i < (n - 1) && rawParts[i + 1] === '||' ) {
i += 1;
2015-05-23 15:27:24 +02:00
continue;
}
reStr = reStrs.length === 1 ? reStrs[0] : reStrs.join('|');
userFilters.push({
2015-05-09 00:28:01 +02:00
re: new RegExp(reStr, 'i'),
r: !not
});
reStrs.length = 0;
2015-05-23 15:27:24 +02:00
not = false;
2015-05-09 00:28:01 +02:00
}
filters = builtinFilters.concat(userFilters);
2015-05-09 00:28:01 +02:00
};
const filterOne = function(tr, clean) {
if ( filters.length === 0 && clean === true ) { return; }
2015-05-09 00:28:01 +02:00
// do not filter out doc boundaries, they help separate important
// section of log.
const cl = tr.classList;
if ( cl.contains('maindoc') ) { return; }
if ( filters.length === 0 ) {
2015-05-09 00:28:01 +02:00
cl.remove('f');
return;
}
const cc = tr.cells;
const ccount = cc.length;
2015-05-09 00:28:01 +02:00
// each filter expression must hit (implicit and-op)
// if...
// positive filter expression = there must one hit on any field
// negative filter expression = there must be no hit on all fields
for ( const f of filters ) {
let hit = !f.r;
for ( let j = 1; j < ccount; j++ ) {
2015-05-09 00:28:01 +02:00
if ( f.re.test(cc[j].textContent) ) {
hit = f.r;
break;
}
}
if ( !hit ) {
cl.add('f');
return;
}
}
cl.remove('f');
};
const filterAll = function() {
const filterCount = filters.length;
uDom.nodeFromId('filterButton').classList.toggle(
'active',
filterCount !== 0
);
2015-05-09 00:28:01 +02:00
// Special case: no filter
if ( filterCount === 0 ) {
uDom('#netInspector tr').removeClass('f');
2015-05-09 00:28:01 +02:00
return;
}
for ( const row of document.querySelector('#netInspector tbody').rows ) {
filterOne(row);
2015-05-09 00:28:01 +02:00
}
};
const onFilterChangedAsync = (function() {
let timer;
const commit = ( ) => {
timer = undefined;
2015-05-09 00:28:01 +02:00
parseInput();
filterAll();
};
return function() {
if ( timer !== undefined ) {
2015-05-09 00:28:01 +02:00
clearTimeout(timer);
}
timer = vAPI.setTimeout(commit, 750);
2015-05-09 00:28:01 +02:00
};
})();
const onFilterButton = function() {
uDom.nodeFromId('netInspector').classList.toggle('f');
2015-05-09 00:28:01 +02:00
};
const onToggleExtras = function(ev) {
ev.target.classList.toggle('expanded');
};
const onToggleBuiltinExpression = function(ev) {
builtinFilters.length = 0;
ev.target.classList.toggle('on');
const filtexElems = ev.currentTarget.querySelectorAll('[data-filtex]');
const orExprs = [];
let not = false;
for ( const filtexElem of filtexElems ) {
let filtex = filtexElem.getAttribute('data-filtex');
let active = filtexElem.classList.contains('on');
if ( filtex === '!' ) {
if ( orExprs.length !== 0 ) {
builtinFilters.push({
re: new RegExp(orExprs.join('|')),
r: !not
});
orExprs.length = 0;
}
not = active;
} else if ( active ) {
orExprs.push(filtex);
}
}
if ( orExprs.length !== 0 ) {
builtinFilters.push({
re: new RegExp(orExprs.join('|')),
r: !not
});
}
filters = builtinFilters.concat(userFilters);
uDom.nodeFromId('filterExprButton').classList.toggle(
'active',
builtinFilters.length !== 0
);
filterAll();
};
2015-05-09 00:28:01 +02:00
uDom('#filterButton').on('click', onFilterButton);
uDom('#filterInput > input').on('input', onFilterChangedAsync);
uDom('#filterExprButton').on('click', onToggleExtras);
uDom('#filterExprPicker').on('click', '[data-filtex]', onToggleBuiltinExpression);
2015-05-09 00:28:01 +02:00
2015-06-26 01:09:47 +02:00
// https://github.com/gorhill/uBlock/issues/404
// Ensure page state is in sync with the state of its various widgets.
2015-06-26 01:09:47 +02:00
parseInput();
filterAll();
2015-05-09 00:28:01 +02:00
return {
filterOne,
filterAll,
2015-05-09 00:28:01 +02:00
};
})();
/******************************************************************************/
const toJunkyard = function(trs) {
2015-05-09 00:28:01 +02:00
trs.remove();
var i = trs.length;
while ( i-- ) {
trJunkyard.push(trs.nodeAt(i));
}
};
/******************************************************************************/
var clearBuffer = function() {
var tabClass = uDom.nodeFromId('pageSelector').value;
var btsAlso = tabClass === '' || tabClass === 'tab_bts';
var tbody = document.querySelector('#netInspector tbody');
2015-06-15 02:11:25 +02:00
var tr = tbody.lastElementChild;
var trPrevious;
while ( tr !== null ) {
trPrevious = tr.previousElementSibling;
if (
(tr.clientHeight > 0) &&
(tr.classList.contains('tab_bts') === false || btsAlso)
) {
2015-06-15 02:11:25 +02:00
trJunkyard.push(tbody.removeChild(tr));
}
tr = trPrevious;
2015-05-09 00:28:01 +02:00
}
2015-06-15 02:11:25 +02:00
uDom.nodeFromId('clear').classList.toggle(
'disabled',
tbody.childElementCount === 0
);
uDom.nodeFromId('clean').classList.toggle(
'disabled',
tbody.querySelector('#netInspector tr[data-tabid].void') === null
2015-06-15 02:11:25 +02:00
);
};
/******************************************************************************/
var cleanBuffer = function() {
var rows = uDom('#netInspector tr[data-tabid].void').remove();
var i = rows.length;
while ( i-- ) {
trJunkyard.push(rows.nodeAt(i));
}
uDom('#clean').addClass('disabled');
2015-05-09 00:28:01 +02:00
};
/******************************************************************************/
const pauseNetInspector = function() {
netInspectorPaused = uDom.nodeFromId('netInspector')
.classList
.toggle('paused');
};
/******************************************************************************/
const toggleVCompactView = function() {
uDom.nodeFromId('netInspector').classList.toggle('vCompact');
2018-01-08 20:29:39 +01:00
uDom('#netInspector .vExpanded').toggleClass('vExpanded');
};
const toggleVCompactRow = function(ev) {
2018-01-08 20:29:39 +01:00
ev.target.parentElement.classList.toggle('vExpanded');
};
/******************************************************************************/
const popupManager = (function() {
let realTabId = 0;
let popup = null;
let popupObserver = null;
const resizePopup = function() {
if ( popup === null ) { return; }
const popupBody = popup.contentWindow.document.body;
if ( popupBody.clientWidth !== 0 && popup.clientWidth !== popupBody.clientWidth ) {
popup.style.setProperty('width', popupBody.clientWidth + 'px');
2015-05-09 00:28:01 +02:00
}
if ( popupBody.clientHeight !== 0 && popup.clientHeight !== popupBody.clientHeight ) {
2015-05-10 15:28:50 +02:00
popup.style.setProperty('height', popupBody.clientHeight + 'px');
2015-05-09 00:28:01 +02:00
}
};
const onLoad = function() {
2015-05-09 00:28:01 +02:00
resizePopup();
popupObserver.observe(popup.contentDocument.body, {
subtree: true,
attributes: true
});
};
const setTabId = function(tabId) {
if ( popup === null ) { return; }
popup.setAttribute('src', 'popup.html?tabId=' + tabId);
};
const onTabIdChanged = function() {
const tabId = tabIdFromPageSelector();
if ( tabId === 0 ) { return toggleOff(); }
realTabId = tabId;
setTabId(realTabId);
};
2015-05-09 00:28:01 +02:00
const toggleOn = function() {
const tabId = tabIdFromPageSelector();
if ( tabId === 0 ) { return; }
realTabId = tabId;
2015-05-09 00:28:01 +02:00
popup = uDom.nodeFromId('popupContainer');
2015-05-09 00:28:01 +02:00
popup.addEventListener('load', onLoad);
popupObserver = new MutationObserver(resizePopup);
const parent = uDom.nodeFromId('inspectors');
const rect = parent.getBoundingClientRect();
popup.style.setProperty('right', (rect.right - parent.clientWidth) + 'px');
2015-06-26 06:08:41 +02:00
parent.classList.add('popupOn');
2015-05-09 00:28:01 +02:00
document.addEventListener('tabIdChanged', onTabIdChanged);
2015-05-09 00:28:01 +02:00
setTabId(realTabId);
};
2015-05-09 00:28:01 +02:00
const toggleOff = function() {
document.removeEventListener('tabIdChanged', onTabIdChanged);
uDom.nodeFromId('inspectors').classList.remove('popupOn');
2015-05-09 00:28:01 +02:00
popup.removeEventListener('load', onLoad);
popupObserver.disconnect();
popupObserver = null;
popup.setAttribute('src', '');
realTabId = 0;
2015-05-09 00:28:01 +02:00
};
const exports = {
toggleOn: function() {
void (realTabId === 0 ? toggleOn() : toggleOff());
2015-05-09 00:28:01 +02:00
},
toggleOff: function() {
if ( realTabId !== 0 ) {
2015-05-09 00:28:01 +02:00
toggleOff();
}
}
};
Object.defineProperty(exports, 'tabId', {
get: function() { return realTabId || 0; }
});
return exports;
})();
/******************************************************************************/
logger.resize = (function() {
let timer;
const resize = function() {
const vrect = document.body.getBoundingClientRect();
const elems = document.querySelectorAll('.vscrollable');
for ( const elem of elems ) {
const crect = elem.getBoundingClientRect();
const dh = crect.bottom - vrect.bottom;
if ( dh === 0 ) { continue; }
elem.style.height = (crect.height - dh) + 'px';
}
};
const resizeAsync = function() {
if ( timer !== undefined ) { return; }
timer = self.requestAnimationFrame(( ) => {
timer = undefined;
resize();
});
};
resizeAsync();
window.addEventListener('resize', resizeAsync, { passive: true });
return resizeAsync;
})();
/******************************************************************************/
const grabView = function() {
if ( logger.ownerId === undefined ) {
logger.ownerId = Date.now();
2018-01-08 20:29:39 +01:00
}
readLogBufferAsync();
};
const releaseView = function() {
if ( logger.ownerId === undefined ) { return; }
2018-01-08 20:29:39 +01:00
vAPI.messaging.send(
'loggerUI',
{ what: 'releaseView', ownerId: logger.ownerId }
2018-01-08 20:29:39 +01:00
);
logger.ownerId = undefined;
2018-01-08 20:29:39 +01:00
};
window.addEventListener('pagehide', releaseView);
window.addEventListener('pageshow', grabView);
// https://bugzilla.mozilla.org/show_bug.cgi?id=1398625
window.addEventListener('beforeunload', releaseView);
/******************************************************************************/
2015-08-01 17:30:54 +02:00
readLogBuffer();
2015-05-09 00:28:01 +02:00
2015-08-01 17:30:54 +02:00
uDom('#pageSelector').on('change', pageSelectorChanged);
uDom('#refresh').on('click', reloadTab);
uDom('#showpopup').on('click', popupManager.toggleOn);
uDom('#netInspector .vCompactToggler').on('click', toggleVCompactView);
2015-08-01 17:30:54 +02:00
uDom('#clean').on('click', cleanBuffer);
uDom('#clear').on('click', clearBuffer);
uDom('#pause').on('click', pauseNetInspector);
2015-08-01 17:30:54 +02:00
uDom('#maxEntries').on('change', onMaxEntriesChanged);
2018-01-08 20:29:39 +01:00
uDom('#netInspector table').on('click', 'tr > td:nth-of-type(1)', toggleVCompactRow);
uDom('#netInspector').on('click', 'tr.canLookup > td:nth-of-type(2)', reverseLookupManager.toggleOn);
uDom('#netInspector').on('click', 'tr.cat_net > td:nth-of-type(3)', netFilteringManager.toggleOn);
2015-06-26 01:09:47 +02:00
// https://github.com/gorhill/uBlock/issues/507
// Ensure tab selector is in sync with URL hash
pageSelectorFromURLHash();
2015-08-01 17:30:54 +02:00
window.addEventListener('hashchange', pageSelectorFromURLHash);
2015-05-09 00:28:01 +02:00
// Start to watch the current window geometry 2 seconds after the document
// is loaded, to be sure no spurious geometry changes will be triggered due
// to the window geometry pontentially not settling fast enough.
if ( self.location.search.includes('popup=1') ) {
window.addEventListener('load', ( ) => {
setTimeout(( ) => {
popupLoggerBox = {
x: self.screenX,
y: self.screenY,
w: self.outerWidth,
h: self.outerHeight,
};
}, 2000);
}, { once: true });
}
2015-05-09 00:28:01 +02:00
/******************************************************************************/
})();