2015-06-28 23:42:08 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-01-10 17:50:08 +01:00
|
|
|
Copyright (C) 2015-2018 Raymond Hill
|
2015-06-28 23:42:08 +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
|
|
|
|
*/
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
/* global uDom */
|
|
|
|
|
|
|
|
'use strict';
|
2015-06-28 23:42:08 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-07-06 13:48:56 +02:00
|
|
|
var showdomButton = uDom.nodeFromId('showdom');
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
// Don't bother if the browser is not modern enough.
|
2017-11-20 14:42:32 +01:00
|
|
|
if (
|
|
|
|
typeof Map === 'undefined' ||
|
|
|
|
Map.polyfill ||
|
|
|
|
typeof WeakMap === 'undefined'
|
|
|
|
) {
|
2015-07-06 13:48:56 +02:00
|
|
|
showdomButton.classList.add('disabled');
|
2015-06-28 23:42:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var logger = self.logger;
|
2017-11-20 14:42:32 +01:00
|
|
|
var inspectorConnectionId;
|
2018-02-26 19:59:16 +01:00
|
|
|
var inspectedTabId = 0;
|
2015-07-01 00:02:29 +02:00
|
|
|
var inspectedURL = '';
|
2015-06-28 23:42:08 +02:00
|
|
|
var inspectedHostname = '';
|
|
|
|
var inspector = uDom.nodeFromId('domInspector');
|
|
|
|
var domTree = uDom.nodeFromId('domTree');
|
2015-07-07 00:11:11 +02:00
|
|
|
var uidGenerator = 1;
|
|
|
|
var filterToIdMap = new Map();
|
2015-06-28 23:42:08 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-20 14:42:32 +01:00
|
|
|
var messaging = vAPI.messaging;
|
|
|
|
|
|
|
|
messaging.addChannelListener('loggerUI', function(msg) {
|
|
|
|
switch ( msg.what ) {
|
|
|
|
case 'connectionBroken':
|
|
|
|
if ( inspectorConnectionId === msg.id ) {
|
|
|
|
filterToIdMap.clear();
|
|
|
|
logger.removeAllChildren(domTree);
|
|
|
|
inspectorConnectionId = undefined;
|
|
|
|
}
|
|
|
|
injectInspector();
|
|
|
|
break;
|
|
|
|
case 'connectionMessage':
|
|
|
|
if ( msg.payload.what === 'domLayoutFull' ) {
|
|
|
|
inspectedURL = msg.payload.url;
|
|
|
|
inspectedHostname = msg.payload.hostname;
|
|
|
|
renderDOMFull(msg.payload);
|
|
|
|
} else if ( msg.payload.what === 'domLayoutIncremental' ) {
|
|
|
|
renderDOMIncremental(msg.payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'connectionRequested':
|
|
|
|
if ( msg.from !== 'domInspector' ) { return false; }
|
2018-01-08 20:29:39 +01:00
|
|
|
if (
|
|
|
|
msg.tabId === undefined ||
|
|
|
|
msg.tabId !== inspectedTabId
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-20 14:42:32 +01:00
|
|
|
filterToIdMap.clear();
|
|
|
|
logger.removeAllChildren(domTree);
|
|
|
|
inspectorConnectionId = msg.id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
var nodeFromDomEntry = function(entry) {
|
|
|
|
var node, value;
|
|
|
|
var li = document.createElement('li');
|
|
|
|
li.setAttribute('id', entry.nid);
|
|
|
|
// expander/collapser
|
2017-11-20 14:42:32 +01:00
|
|
|
li.appendChild(document.createElement('span'));
|
2015-06-28 23:42:08 +02:00
|
|
|
// selector
|
|
|
|
node = document.createElement('code');
|
|
|
|
node.textContent = entry.sel;
|
|
|
|
li.appendChild(node);
|
|
|
|
// descendant count
|
|
|
|
value = entry.cnt || 0;
|
|
|
|
node = document.createElement('span');
|
|
|
|
node.textContent = value !== 0 ? value.toLocaleString() : '';
|
|
|
|
node.setAttribute('data-cnt', value);
|
|
|
|
li.appendChild(node);
|
|
|
|
// cosmetic filter
|
2015-07-07 00:11:11 +02:00
|
|
|
if ( entry.filter === undefined ) {
|
|
|
|
return li;
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
2015-07-07 00:11:11 +02:00
|
|
|
node = document.createElement('code');
|
|
|
|
node.classList.add('filter');
|
|
|
|
value = filterToIdMap.get(entry.filter);
|
|
|
|
if ( value === undefined ) {
|
|
|
|
value = uidGenerator.toString();
|
|
|
|
filterToIdMap.set(entry.filter, value);
|
|
|
|
uidGenerator += 1;
|
|
|
|
}
|
|
|
|
node.setAttribute('data-filter-id', value);
|
|
|
|
node.textContent = entry.filter;
|
|
|
|
li.appendChild(node);
|
|
|
|
li.classList.add('isCosmeticHide');
|
2015-06-28 23:42:08 +02:00
|
|
|
return li;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var appendListItem = function(ul, li) {
|
|
|
|
ul.appendChild(li);
|
|
|
|
// Ancestor nodes of a node which is affected by a cosmetic filter will
|
|
|
|
// be marked as "containing cosmetic filters", for user convenience.
|
|
|
|
if ( li.classList.contains('isCosmeticHide') === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
li = li.parentElement.parentElement;
|
|
|
|
if ( li === null ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
li.classList.add('hasCosmeticHide');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var renderDOMFull = function(response) {
|
2015-07-01 15:19:13 +02:00
|
|
|
var domTreeParent = domTree.parentElement;
|
|
|
|
var ul = domTreeParent.removeChild(domTree);
|
2015-06-28 23:42:08 +02:00
|
|
|
logger.removeAllChildren(domTree);
|
|
|
|
|
2015-07-07 00:11:11 +02:00
|
|
|
filterToIdMap.clear();
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
var lvl = 0;
|
|
|
|
var entries = response.layout;
|
|
|
|
var n = entries.length;
|
|
|
|
var li, entry;
|
|
|
|
for ( var i = 0; i < n; i++ ) {
|
|
|
|
entry = entries[i];
|
|
|
|
if ( entry.lvl === lvl ) {
|
|
|
|
li = nodeFromDomEntry(entry);
|
|
|
|
appendListItem(ul, li);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( entry.lvl > lvl ) {
|
|
|
|
ul = document.createElement('ul');
|
|
|
|
li.appendChild(ul);
|
|
|
|
li.classList.add('branch');
|
|
|
|
li = nodeFromDomEntry(entry);
|
|
|
|
appendListItem(ul, li);
|
|
|
|
lvl = entry.lvl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// entry.lvl < lvl
|
|
|
|
while ( entry.lvl < lvl ) {
|
|
|
|
ul = li.parentNode;
|
|
|
|
li = ul.parentNode;
|
|
|
|
ul = li.parentNode;
|
|
|
|
lvl -= 1;
|
|
|
|
}
|
|
|
|
li = nodeFromDomEntry(entry);
|
2015-08-31 16:05:59 +02:00
|
|
|
appendListItem(ul, li);
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
|
|
|
while ( ul.parentNode !== null ) {
|
|
|
|
ul = ul.parentNode;
|
|
|
|
}
|
|
|
|
ul.firstElementChild.classList.add('show');
|
|
|
|
|
2015-07-01 15:19:13 +02:00
|
|
|
domTreeParent.appendChild(domTree);
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
2015-06-29 16:46:20 +02:00
|
|
|
// https://www.youtube.com/watch?v=IDGNA83mxDo
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var patchIncremental = function(from, delta) {
|
|
|
|
var span, cnt;
|
|
|
|
var li = from.parentElement.parentElement;
|
|
|
|
var patchCosmeticHide = delta >= 0 &&
|
2015-07-08 02:41:24 +02:00
|
|
|
from.classList.contains('isCosmeticHide') &&
|
|
|
|
li.classList.contains('hasCosmeticHide') === false;
|
2015-06-28 23:42:08 +02:00
|
|
|
// Include descendants count when removing a node
|
|
|
|
if ( delta < 0 ) {
|
|
|
|
delta -= countFromNode(from);
|
|
|
|
}
|
|
|
|
for ( ; li.localName === 'li'; li = li.parentElement.parentElement ) {
|
|
|
|
span = li.children[2];
|
|
|
|
if ( delta !== 0 ) {
|
|
|
|
cnt = countFromNode(li) + delta;
|
|
|
|
span.textContent = cnt !== 0 ? cnt.toLocaleString() : '';
|
|
|
|
span.setAttribute('data-cnt', cnt);
|
|
|
|
}
|
|
|
|
if ( patchCosmeticHide ) {
|
2015-07-08 02:41:24 +02:00
|
|
|
li.classList.add('hasCosmeticHide');
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var renderDOMIncremental = function(response) {
|
|
|
|
// Process each journal entry:
|
|
|
|
// 1 = node added
|
|
|
|
// -1 = node removed
|
|
|
|
var journal = response.journal;
|
2017-11-20 14:42:32 +01:00
|
|
|
var nodes = new Map(response.nodes);
|
2015-06-28 23:42:08 +02:00
|
|
|
var entry, previous, li, ul;
|
|
|
|
for ( var i = 0, n = journal.length; i < n; i++ ) {
|
|
|
|
entry = journal[i];
|
|
|
|
// Remove node
|
|
|
|
if ( entry.what === -1 ) {
|
|
|
|
li = document.getElementById(entry.nid);
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( li === null ) { continue; }
|
2015-06-28 23:42:08 +02:00
|
|
|
patchIncremental(li, -1);
|
|
|
|
li.parentNode.removeChild(li);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Modify node
|
|
|
|
if ( entry.what === 0 ) {
|
|
|
|
// TODO: update selector/filter
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Add node as sibling
|
|
|
|
if ( entry.what === 1 && entry.l ) {
|
|
|
|
previous = document.getElementById(entry.l);
|
|
|
|
// This should not happen
|
|
|
|
if ( previous === null ) {
|
|
|
|
// throw new Error('No left sibling!?');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ul = previous.parentElement;
|
2017-11-20 14:42:32 +01:00
|
|
|
li = nodeFromDomEntry(nodes.get(entry.nid));
|
2015-06-28 23:42:08 +02:00
|
|
|
ul.insertBefore(li, previous.nextElementSibling);
|
|
|
|
patchIncremental(li, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Add node as child
|
|
|
|
if ( entry.what === 1 && entry.u ) {
|
|
|
|
li = document.getElementById(entry.u);
|
|
|
|
// This should not happen
|
|
|
|
if ( li === null ) {
|
|
|
|
// throw new Error('No parent!?');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ul = li.querySelector('ul');
|
|
|
|
if ( ul === null ) {
|
|
|
|
ul = document.createElement('ul');
|
|
|
|
li.appendChild(ul);
|
|
|
|
li.classList.add('branch');
|
|
|
|
}
|
2017-11-20 14:42:32 +01:00
|
|
|
li = nodeFromDomEntry(nodes.get(entry.nid));
|
2015-06-28 23:42:08 +02:00
|
|
|
ul.appendChild(li);
|
|
|
|
patchIncremental(li, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-29 16:46:20 +02:00
|
|
|
// https://www.youtube.com/watch?v=6u2KPtJB9h8
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var countFromNode = function(li) {
|
|
|
|
var span = li.children[2];
|
|
|
|
var cnt = parseInt(span.getAttribute('data-cnt'), 10);
|
2015-07-07 22:54:41 +02:00
|
|
|
return isNaN(cnt) ? 0 : cnt;
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
var selectorFromNode = function(node) {
|
2015-06-28 23:42:08 +02:00
|
|
|
var selector = '';
|
|
|
|
var code;
|
|
|
|
while ( node !== null ) {
|
|
|
|
if ( node.localName === 'li' ) {
|
2016-06-28 01:09:04 +02:00
|
|
|
code = node.querySelector('code');
|
2015-06-28 23:42:08 +02:00
|
|
|
if ( code !== null ) {
|
|
|
|
selector = code.textContent + ' > ' + selector;
|
|
|
|
if ( selector.indexOf('#') !== -1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = node.parentElement;
|
|
|
|
}
|
|
|
|
return selector.slice(0, -3);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-06-28 01:09:04 +02:00
|
|
|
var selectorFromFilter = function(node) {
|
|
|
|
while ( node !== null ) {
|
|
|
|
if ( node.localName === 'li' ) {
|
|
|
|
var code = node.querySelector('code:nth-of-type(2)');
|
|
|
|
if ( code !== null ) {
|
|
|
|
return code.textContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = node.parentElement;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
var nidFromNode = function(node) {
|
|
|
|
var li = node;
|
|
|
|
while ( li !== null ) {
|
|
|
|
if ( li.localName === 'li' ) {
|
|
|
|
return li.id || '';
|
|
|
|
}
|
|
|
|
li = li.parentElement;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var startDialog = (function() {
|
|
|
|
var dialog = uDom.nodeFromId('cosmeticFilteringDialog');
|
2015-07-01 00:02:29 +02:00
|
|
|
var textarea = dialog.querySelector('textarea');
|
|
|
|
var hideSelectors = [];
|
|
|
|
var unhideSelectors = [];
|
|
|
|
var inputTimer = null;
|
|
|
|
|
|
|
|
var onInputChanged = (function() {
|
|
|
|
var parse = function() {
|
|
|
|
inputTimer = null;
|
|
|
|
hideSelectors = [];
|
|
|
|
unhideSelectors = [];
|
|
|
|
|
|
|
|
var line, matches;
|
|
|
|
var re = /^([^#]*)(#@?#)(.+)$/;
|
|
|
|
var lines = textarea.value.split(/\s*\n\s*/);
|
|
|
|
for ( var i = 0; i < lines.length; i++ ) {
|
|
|
|
line = lines[i].trim();
|
|
|
|
if ( line === '' || line.charAt(0) === '!' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matches = re.exec(line);
|
|
|
|
if ( matches === null || matches.length !== 4 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( inspectedHostname.lastIndexOf(matches[1]) === -1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( matches[2] === '##' ) {
|
|
|
|
hideSelectors.push(matches[3]);
|
|
|
|
} else {
|
|
|
|
unhideSelectors.push(matches[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showCommitted();
|
|
|
|
};
|
|
|
|
|
|
|
|
return function parseAsync() {
|
|
|
|
if ( inputTimer === null ) {
|
|
|
|
inputTimer = vAPI.setTimeout(parse, 743);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2015-06-28 23:42:08 +02:00
|
|
|
|
2017-11-20 14:42:32 +01:00
|
|
|
var onClicked = function(ev) {
|
2015-06-28 23:42:08 +02:00
|
|
|
var target = ev.target;
|
|
|
|
|
|
|
|
// click outside the dialog proper
|
|
|
|
if ( target.classList.contains('modalDialog') ) {
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
ev.stopPropagation();
|
2015-07-01 00:02:29 +02:00
|
|
|
|
|
|
|
if ( target.id === 'createCosmeticFilters' ) {
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send('loggerUI', { what: 'createUserFilter', filters: textarea.value });
|
2015-07-01 00:02:29 +02:00
|
|
|
// Force a reload for the new cosmetic filter(s) to take effect
|
2016-03-06 16:51:06 +01:00
|
|
|
messaging.send('loggerUI', { what: 'reloadTab', tabId: inspectedTabId });
|
2015-07-01 00:02:29 +02:00
|
|
|
return stop();
|
|
|
|
}
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
2017-11-20 14:42:32 +01:00
|
|
|
var showCommitted = function() {
|
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'showCommitted',
|
|
|
|
hide: hideSelectors.join(',\n'),
|
|
|
|
unhide: unhideSelectors.join(',\n')
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var showInteractive = function() {
|
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'showInteractive',
|
|
|
|
hide: hideSelectors.join(',\n'),
|
|
|
|
unhide: unhideSelectors.join(',\n')
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var start = function() {
|
|
|
|
hideSelectors = [];
|
|
|
|
textarea.addEventListener('input', onInputChanged);
|
|
|
|
var node;
|
|
|
|
for ( node of domTree.querySelectorAll('code.off') ) {
|
|
|
|
if ( node.classList.contains('filter') === false ) {
|
|
|
|
hideSelectors.push(selectorFromNode(node));
|
|
|
|
}
|
2015-07-01 00:02:29 +02:00
|
|
|
}
|
2017-11-20 14:42:32 +01:00
|
|
|
var taValue = [];
|
2015-07-01 00:02:29 +02:00
|
|
|
var d = new Date();
|
|
|
|
taValue.push('! ' + d.toLocaleString() + ' ' + inspectedURL);
|
2017-11-20 14:42:32 +01:00
|
|
|
for ( var selector of hideSelectors ) {
|
|
|
|
taValue.push(inspectedHostname + '##' + selector);
|
2015-07-01 00:02:29 +02:00
|
|
|
}
|
2015-07-07 00:11:11 +02:00
|
|
|
var ids = new Set(), id;
|
2017-11-20 14:42:32 +01:00
|
|
|
for ( node of domTree.querySelectorAll('code.filter.off') ) {
|
2015-07-07 00:11:11 +02:00
|
|
|
id = node.getAttribute('data-filter-id');
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( ids.has(id) ) { continue; }
|
2015-07-07 00:11:11 +02:00
|
|
|
ids.add(id);
|
2015-07-01 00:02:29 +02:00
|
|
|
unhideSelectors.push(node.textContent);
|
|
|
|
taValue.push(inspectedHostname + '#@#' + node.textContent);
|
|
|
|
}
|
|
|
|
textarea.value = taValue.join('\n');
|
|
|
|
document.body.appendChild(dialog);
|
2017-11-20 14:42:32 +01:00
|
|
|
dialog.addEventListener('click', onClicked, true);
|
2015-07-01 00:02:29 +02:00
|
|
|
showCommitted();
|
|
|
|
};
|
|
|
|
|
|
|
|
var stop = function() {
|
|
|
|
if ( inputTimer !== null ) {
|
|
|
|
clearTimeout(inputTimer);
|
|
|
|
inputTimer = null;
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
2015-07-01 00:02:29 +02:00
|
|
|
showInteractive();
|
|
|
|
hideSelectors = [];
|
|
|
|
unhideSelectors = [];
|
|
|
|
textarea.removeEventListener('input', onInputChanged);
|
2017-11-20 14:42:32 +01:00
|
|
|
dialog.removeEventListener('click', onClicked, true);
|
2015-07-01 00:02:29 +02:00
|
|
|
document.body.removeChild(dialog);
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return start;
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-20 14:42:32 +01:00
|
|
|
var onClicked = function(ev) {
|
2015-06-28 23:42:08 +02:00
|
|
|
ev.stopPropagation();
|
|
|
|
|
2018-02-26 19:59:16 +01:00
|
|
|
if ( inspectedTabId === 0 ) { return; }
|
2015-06-28 23:42:08 +02:00
|
|
|
|
|
|
|
var target = ev.target;
|
|
|
|
var parent = target.parentElement;
|
|
|
|
|
|
|
|
// Expand/collapse branch
|
|
|
|
if (
|
|
|
|
target.localName === 'span' &&
|
|
|
|
parent instanceof HTMLLIElement &&
|
|
|
|
parent.classList.contains('branch') &&
|
|
|
|
target === parent.firstElementChild
|
|
|
|
) {
|
2017-11-20 14:42:32 +01:00
|
|
|
var state = parent.classList.toggle('show');
|
|
|
|
if ( !state ) {
|
|
|
|
for ( var node of parent.querySelectorAll('.branch') ) {
|
|
|
|
node.classList.remove('show');
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 23:42:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:11:11 +02:00
|
|
|
// Not a node or filter
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( target.localName !== 'code' ) { return; }
|
2015-07-07 00:11:11 +02:00
|
|
|
|
|
|
|
// Toggle cosmetic filter
|
|
|
|
if ( target.classList.contains('filter') ) {
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'toggleFilter',
|
|
|
|
original: false,
|
|
|
|
target: target.classList.toggle('off'),
|
|
|
|
selector: selectorFromNode(target),
|
|
|
|
filter: selectorFromFilter(target),
|
|
|
|
nid: nidFromNode(target)
|
|
|
|
});
|
2015-07-07 00:11:11 +02:00
|
|
|
uDom('[data-filter-id="' + target.getAttribute('data-filter-id') + '"]', inspector).toggleClass(
|
|
|
|
'off',
|
|
|
|
target.classList.contains('off')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Toggle node
|
|
|
|
else {
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'toggleNodes',
|
|
|
|
original: true,
|
|
|
|
target: target.classList.toggle('off') === false,
|
|
|
|
selector: selectorFromNode(target),
|
|
|
|
nid: nidFromNode(target)
|
|
|
|
});
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
2015-07-07 00:11:11 +02:00
|
|
|
|
|
|
|
var cantCreate = domTree.querySelector('.off') === null;
|
|
|
|
inspector.querySelector('.permatoolbar .revert').classList.toggle('disabled', cantCreate);
|
|
|
|
inspector.querySelector('.permatoolbar .commit').classList.toggle('disabled', cantCreate);
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var onMouseOver = (function() {
|
|
|
|
var mouseoverTarget = null;
|
|
|
|
var mouseoverTimer = null;
|
|
|
|
|
|
|
|
var timerHandler = function() {
|
|
|
|
mouseoverTimer = null;
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'highlightOne',
|
|
|
|
selector: selectorFromNode(mouseoverTarget),
|
|
|
|
nid: nidFromNode(mouseoverTarget),
|
|
|
|
scrollTo: true
|
|
|
|
});
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(ev) {
|
2018-02-26 19:59:16 +01:00
|
|
|
if ( inspectedTabId === 0 ) { return; }
|
2015-07-01 00:02:29 +02:00
|
|
|
// Convenience: skip real-time highlighting if shift key is pressed.
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( ev.shiftKey ) { return; }
|
2015-06-28 23:42:08 +02:00
|
|
|
// Find closest `li`
|
|
|
|
var target = ev.target;
|
|
|
|
while ( target !== null ) {
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( target.localName === 'li' ) { break; }
|
2015-06-28 23:42:08 +02:00
|
|
|
target = target.parentElement;
|
|
|
|
}
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( target === mouseoverTarget ) { return; }
|
2015-06-28 23:42:08 +02:00
|
|
|
mouseoverTarget = target;
|
|
|
|
if ( mouseoverTimer === null ) {
|
|
|
|
mouseoverTimer = vAPI.setTimeout(timerHandler, 50);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const currentTabId = function() {
|
2018-02-26 19:59:16 +01:00
|
|
|
if ( showdomButton.classList.contains('active') === false ) { return 0; }
|
2018-01-10 17:50:08 +01:00
|
|
|
return logger.tabIdFromPageSelector();
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const injectInspector = function() {
|
|
|
|
const tabId = currentTabId();
|
|
|
|
if ( tabId <= 0 ) { return; }
|
2015-06-28 23:42:08 +02:00
|
|
|
inspectedTabId = tabId;
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.send('loggerUI', {
|
|
|
|
what: 'scriptlet',
|
|
|
|
tabId: tabId,
|
|
|
|
scriptlet: 'dom-inspector'
|
|
|
|
});
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-29 16:46:20 +02:00
|
|
|
var shutdownInspector = function() {
|
2017-11-20 14:42:32 +01:00
|
|
|
if ( inspectorConnectionId !== undefined ) {
|
|
|
|
messaging.disconnectFrom(inspectorConnectionId);
|
|
|
|
inspectorConnectionId = undefined;
|
2015-06-29 16:46:20 +02:00
|
|
|
}
|
2015-06-28 23:42:08 +02:00
|
|
|
logger.removeAllChildren(domTree);
|
2017-11-20 14:42:32 +01:00
|
|
|
inspector.classList.add('vCompact');
|
2018-02-26 19:59:16 +01:00
|
|
|
inspectedTabId = 0;
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var onTabIdChanged = function() {
|
2018-12-13 18:30:54 +01:00
|
|
|
const tabId = currentTabId();
|
|
|
|
if ( tabId <= 0 ) {
|
|
|
|
return toggleOff();
|
|
|
|
}
|
|
|
|
if ( inspectedTabId !== tabId ) {
|
2015-06-28 23:42:08 +02:00
|
|
|
shutdownInspector();
|
2017-11-20 14:42:32 +01:00
|
|
|
injectInspector();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var toggleVCompactView = function() {
|
|
|
|
var state = !inspector.classList.toggle('vCompact');
|
|
|
|
var branches = document.querySelectorAll('#domInspector li.branch');
|
|
|
|
for ( var branch of branches ) {
|
|
|
|
branch.classList.toggle('show', state);
|
2015-06-28 23:42:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-20 14:42:32 +01:00
|
|
|
var toggleHCompactView = function() {
|
|
|
|
inspector.classList.toggle('hCompact');
|
|
|
|
};
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var toggleHighlightMode = function() {
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.sendTo(inspectorConnectionId, {
|
|
|
|
what: 'highlightMode',
|
|
|
|
invert: uDom.nodeFromSelector('#domInspector .permatoolbar .highlightMode').classList.toggle('invert')
|
|
|
|
});
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var revert = function() {
|
|
|
|
uDom('#domTree .off').removeClass('off');
|
2017-11-20 14:42:32 +01:00
|
|
|
messaging.sendTo(inspectorConnectionId, { what: 'resetToggledNodes' });
|
2015-06-28 23:42:08 +02:00
|
|
|
inspector.querySelector('.permatoolbar .revert').classList.add('disabled');
|
|
|
|
inspector.querySelector('.permatoolbar .commit').classList.add('disabled');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const toggleOn = function() {
|
|
|
|
uDom.nodeFromId('inspectors').classList.add('dom');
|
2015-06-28 23:42:08 +02:00
|
|
|
window.addEventListener('beforeunload', toggleOff);
|
2018-01-09 14:08:17 +01:00
|
|
|
document.addEventListener('tabIdChanged', onTabIdChanged);
|
2017-11-20 14:42:32 +01:00
|
|
|
domTree.addEventListener('click', onClicked, true);
|
2015-06-28 23:42:08 +02:00
|
|
|
domTree.addEventListener('mouseover', onMouseOver, true);
|
2017-11-20 14:42:32 +01:00
|
|
|
uDom.nodeFromSelector('#domInspector .vCompactToggler').addEventListener('click', toggleVCompactView);
|
|
|
|
uDom.nodeFromSelector('#domInspector .hCompactToggler').addEventListener('click', toggleHCompactView);
|
2015-06-28 23:42:08 +02:00
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .highlightMode').addEventListener('click', toggleHighlightMode);
|
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .revert').addEventListener('click', revert);
|
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .commit').addEventListener('click', startDialog);
|
|
|
|
injectInspector();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const toggleOff = function() {
|
|
|
|
showdomButton.classList.remove('active');
|
|
|
|
uDom.nodeFromId('inspectors').classList.remove('dom');
|
2015-06-28 23:42:08 +02:00
|
|
|
shutdownInspector();
|
|
|
|
window.removeEventListener('beforeunload', toggleOff);
|
2018-01-09 14:08:17 +01:00
|
|
|
document.removeEventListener('tabIdChanged', onTabIdChanged);
|
2017-11-20 14:42:32 +01:00
|
|
|
domTree.removeEventListener('click', onClicked, true);
|
2015-06-28 23:42:08 +02:00
|
|
|
domTree.removeEventListener('mouseover', onMouseOver, true);
|
2017-11-20 14:42:32 +01:00
|
|
|
uDom.nodeFromSelector('#domInspector .vCompactToggler').removeEventListener('click', toggleVCompactView);
|
|
|
|
uDom.nodeFromSelector('#domInspector .hCompactToggler').removeEventListener('click', toggleHCompactView);
|
2015-06-28 23:42:08 +02:00
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .highlightMode').removeEventListener('click', toggleHighlightMode);
|
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .revert').removeEventListener('click', revert);
|
|
|
|
uDom.nodeFromSelector('#domInspector .permatoolbar .commit').removeEventListener('click', startDialog);
|
2018-02-26 19:59:16 +01:00
|
|
|
inspectedTabId = 0;
|
2015-06-28 23:42:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const toggle = function() {
|
2015-06-28 23:42:08 +02:00
|
|
|
if ( showdomButton.classList.toggle('active') ) {
|
|
|
|
toggleOn();
|
|
|
|
} else {
|
|
|
|
toggleOff();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
showdomButton.addEventListener('click', toggle);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|