1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-23 02:42:41 +01:00

dom inspector quirks: keep identical filters in sync, do not create duplicate filters

This commit is contained in:
gorhill 2015-07-06 18:11:11 -04:00
parent 862f588fd4
commit 7a3fb2273e

View File

@ -50,6 +50,8 @@ var fingerprint = null;
var inspector = uDom.nodeFromId('domInspector');
var domTree = uDom.nodeFromId('domTree');
var tabSelector = uDom.nodeFromId('pageSelector');
var uidGenerator = 1;
var filterToIdMap = new Map();
/******************************************************************************/
@ -71,13 +73,21 @@ var nodeFromDomEntry = function(entry) {
node.setAttribute('data-cnt', value);
li.appendChild(node);
// cosmetic filter
if ( entry.filter !== undefined ) {
node = document.createElement('code');
node.classList.add('filter');
node.textContent = entry.filter;
li.appendChild(node);
li.classList.add('isCosmeticHide');
if ( entry.filter === undefined ) {
return li;
}
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');
return li;
};
@ -106,6 +116,8 @@ var renderDOMFull = function(response) {
var ul = domTreeParent.removeChild(domTree);
logger.removeAllChildren(domTree);
filterToIdMap.clear();
var lvl = 0;
var entries = response.layout;
var n = entries.length;
@ -115,7 +127,6 @@ var renderDOMFull = function(response) {
if ( entry.lvl === lvl ) {
li = nodeFromDomEntry(entry);
appendListItem(ul, li);
//expandIfBlockElement(li);
continue;
}
if ( entry.lvl > lvl ) {
@ -124,7 +135,6 @@ var renderDOMFull = function(response) {
li.classList.add('branch');
li = nodeFromDomEntry(entry);
appendListItem(ul, li);
//expandIfBlockElement(li);
lvl = entry.lvl;
continue;
}
@ -357,9 +367,15 @@ var startDialog = (function() {
for ( i = 0; i < entries.length; i++ ) {
taValue.push(inspectedHostname + '##' + entries[i]);
}
var ids = new Set(), id;
var nodes = domTree.querySelectorAll('code.filter.off');
for ( i = 0; i < nodes.length; i++ ) {
node = nodes[i];
id = node.getAttribute('data-filter-id');
if ( ids.has(id) ) {
continue;
}
ids.add(id);
unhideSelectors.push(node.textContent);
taValue.push(inspectedHostname + '#@#' + node.textContent);
}
@ -453,25 +469,47 @@ var onClick = function(ev) {
return;
}
// Toggle selector
if ( target.localName === 'code' ) {
var original = target.classList.contains('filter') === false;
// Not a node or filter
if ( target.localName !== 'code' ) {
return;
}
// Toggle cosmetic filter
if ( target.classList.contains('filter') ) {
messager.sendTo(
{
what: 'toggleNodes',
original: original,
target: original !== target.classList.toggle('off'),
selector: selectorFromNode(target, original ? 1 : 2),
nid: original ? nidFromNode(target) : ''
original: false,
target: target.classList.toggle('off'),
selector: selectorFromNode(target, 2),
nid: ''
},
inspectedTabId,
'dom-inspector.js'
);
var cantCreate = domTree.querySelector('.off') === null;
inspector.querySelector('.permatoolbar .revert').classList.toggle('disabled', cantCreate);
inspector.querySelector('.permatoolbar .commit').classList.toggle('disabled', cantCreate);
return;
uDom('[data-filter-id="' + target.getAttribute('data-filter-id') + '"]', inspector).toggleClass(
'off',
target.classList.contains('off')
);
}
// Toggle node
else {
messager.sendTo(
{
what: 'toggleNodes',
original: true,
target: target.classList.toggle('off') === false,
selector: selectorFromNode(target, 1),
nid: nidFromNode(target)
},
inspectedTabId,
'dom-inspector.js'
);
}
var cantCreate = domTree.querySelector('.off') === null;
inspector.querySelector('.permatoolbar .revert').classList.toggle('disabled', cantCreate);
inspector.querySelector('.permatoolbar .commit').classList.toggle('disabled', cantCreate);
};
/******************************************************************************/