pull/2/head
gorhill 8 years ago
parent 1c27c01ac5
commit 48d9a0fd26

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uMatrix - a Chromium browser extension to black/white list requests. uMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2014-2016 Raymond Hill Copyright (C) 2014-2017 Raymond Hill
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -22,13 +22,13 @@
/* global punycode, vAPI, uDom */ /* global punycode, vAPI, uDom */
/* jshint esnext: true, bitwise: false */ /* jshint esnext: true, bitwise: false */
'use strict';
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
@ -67,6 +67,29 @@ var matrixHeaderPrettyNames = {
var firstPartyLabel = ''; var firstPartyLabel = '';
var blacklistedHostnamesLabel = ''; var blacklistedHostnamesLabel = '';
var nodeToExpandosMap = (function() {
if ( typeof window.WeakMap === 'function' ) {
return new window.WeakMap();
}
})();
var expandosFromNode = function(node) {
if (
node instanceof HTMLElement === false &&
typeof node.nodeAt === 'function'
) {
node = node.nodeAt(0);
}
if ( nodeToExpandosMap ) {
var expandos = nodeToExpandosMap.get(node);
if ( expandos === undefined ) {
nodeToExpandosMap.set(node, (expandos = Object.create(null)));
}
return expandos;
}
return node;
};
var messager = vAPI.messaging.channel('popup.js'); var messager = vAPI.messaging.channel('popup.js');
/******************************************************************************/ /******************************************************************************/
@ -263,9 +286,11 @@ function getPermanentColor(hostname, type) {
return matrixSnapshot.rows[hostname].permanent[matrixSnapshot.headers[type]]; return matrixSnapshot.rows[hostname].permanent[matrixSnapshot.headers[type]];
} }
function getCellClass(hostname, type) { function addCellClass(cell, hostname, type) {
return 't' + getTemporaryColor(hostname, type).toString(16) + var cl = cell.classList;
' p' + getPermanentColor(hostname, type).toString(16); cell.classList.add('matCell');
cell.classList.add('t' + getTemporaryColor(hostname, type).toString(16));
cell.classList.add('p' + getPermanentColor(hostname, type).toString(16));
} }
/******************************************************************************/ /******************************************************************************/
@ -313,11 +338,11 @@ function toggleMainCollapseState(uelem) {
function toggleSpecificCollapseState(uelem) { function toggleSpecificCollapseState(uelem) {
// Remember collapse state forever, but only if it is different // Remember collapse state forever, but only if it is different
// from main collapse switch. // from main collapse switch.
var section = uelem.ancestors('.matSection.collapsible').toggleClass('collapsed'); var section = uelem.ancestors('.matSection.collapsible').toggleClass('collapsed'),
var domain = section.prop('domain'); domain = expandosFromNode(section).domain,
var collapsed = section.hasClass('collapsed'); collapsed = section.hasClass('collapsed'),
var mainCollapseState = getUISetting('popupCollapseDomains') === true; mainCollapseState = getUISetting('popupCollapseDomains') === true,
var specificCollapseStates = getUISetting('popupCollapseSpecificDomains') || {}; specificCollapseStates = getUISetting('popupCollapseSpecificDomains') || {};
if ( collapsed !== mainCollapseState ) { if ( collapsed !== mainCollapseState ) {
specificCollapseStates[domain] = collapsed; specificCollapseStates[domain] = collapsed;
setUISetting('popupCollapseSpecificDomains', specificCollapseStates); setUISetting('popupCollapseSpecificDomains', specificCollapseStates);
@ -332,26 +357,23 @@ function toggleSpecificCollapseState(uelem) {
// Update count value of matrix cells(s) // Update count value of matrix cells(s)
function updateMatrixCounts() { function updateMatrixCounts() {
var matCells = uDom('.matrix .matRow.rw > .matCell'); var matCells = uDom('.matrix .matRow.rw > .matCell'),
var i = matCells.length; i = matCells.length,
var matRow, matCell, count, counts; matRow, matCell, count, counts,
var headers = matrixSnapshot.headers; headers = matrixSnapshot.headers,
var rows = matrixSnapshot.rows; rows = matrixSnapshot.rows,
expandos;
while ( i-- ) { while ( i-- ) {
matCell = matCells.nodeAt(i); matCell = matCells.nodeAt(i);
if ( matCell.hostname === '*' ) { expandos = expandosFromNode(matCell);
continue; if ( expandos.hostname === '*' || expandos.reqType === '*' ) {
}
if ( matCell.reqType === '*' ) {
continue; continue;
} }
matRow = matCell.parentNode; matRow = matCell.parentNode;
counts = matRow.classList.contains('meta') ? 'totals' : 'counts'; counts = matRow.classList.contains('meta') ? 'totals' : 'counts';
count = rows[matCell.hostname][counts][headers[matCell.reqType]]; count = rows[expandos.hostname][counts][headers[expandos.reqType]];
if ( count === matCell.count) { if ( count === expandos.count ) { continue; }
continue; expandos.count = count;
}
matCell.count = count;
matCell.textContent = count ? count : '\u00A0'; matCell.textContent = count ? count : '\u00A0';
} }
} }
@ -362,12 +384,13 @@ function updateMatrixCounts() {
// Color changes when rules change // Color changes when rules change
function updateMatrixColors() { function updateMatrixColors() {
var cells = uDom('.matrix .matRow.rw > .matCell').removeClass(); var cells = uDom('.matrix .matRow.rw > .matCell').removeClass(),
var i = cells.length; i = cells.length,
var cell; cell, expandos;
while ( i-- ) { while ( i-- ) {
cell = cells.nodeAt(i); cell = cells.nodeAt(i);
cell.className = 'matCell ' + getCellClass(cell.hostname, cell.reqType); expandos = expandosFromNode(cell);
addCellClass(cell, expandos.hostname, expandos.reqType);
} }
resizePopup(); resizePopup();
} }
@ -418,9 +441,10 @@ function getCellAction(hostname, type, leaning) {
function handleFilter(button, leaning) { function handleFilter(button, leaning) {
// our parent cell knows who we are // our parent cell knows who we are
var cell = button.ancestors('div.matCell'); var cell = button.ancestors('div.matCell'),
var type = cell.prop('reqType'); expandos = expandosFromNode(cell),
var desHostname = cell.prop('hostname'); type = expandos.reqType,
desHostname = expandos.hostname;
// https://github.com/gorhill/uMatrix/issues/24 // https://github.com/gorhill/uMatrix/issues/24
// No hostname can happen -- like with blacklist meta row // No hostname can happen -- like with blacklist meta row
if ( desHostname === '' ) { if ( desHostname === '' ) {
@ -515,53 +539,63 @@ var createMatrixRow = function() {
function renderMatrixHeaderRow() { function renderMatrixHeaderRow() {
var matHead = uDom('#matHead.collapsible'); var matHead = uDom('#matHead.collapsible');
matHead.toggleClass('collapsed', getUISetting('popupCollapseDomains') === true); matHead.toggleClass('collapsed', getUISetting('popupCollapseDomains') === true);
var cells = matHead.descendants('.matCell'); var cells = matHead.descendants('.matCell'), cell, expandos;
cells.at(0) cell = cells.nodeAt(0);
.prop('reqType', '*') expandos = expandosFromNode(cell);
.prop('hostname', '*') expandos.reqType = '*';
.addClass(getCellClass('*', '*')); expandos.hostname = '*';
cells.at(1) addCellClass(cell, '*', '*');
.prop('reqType', 'cookie') cell = cells.nodeAt(1);
.prop('hostname', '*') expandos = expandosFromNode(cell);
.addClass(getCellClass('*', 'cookie')); expandos.reqType = 'cookie';
cells.at(2) expandos.hostname = '*';
.prop('reqType', 'css') addCellClass(cell, '*', 'cookie');
.prop('hostname', '*') cell = cells.nodeAt(2);
.addClass(getCellClass('*', 'css')); expandos = expandosFromNode(cell);
cells.at(3) expandos.reqType = 'css';
.prop('reqType', 'image') expandos.hostname = '*';
.prop('hostname', '*') addCellClass(cell, '*', 'css');
.addClass(getCellClass('*', 'image')); cell = cells.nodeAt(3);
cells.at(4) expandos = expandosFromNode(cell);
.prop('reqType', 'plugin') expandos.reqType = 'image';
.prop('hostname', '*') expandos.hostname = '*';
.addClass(getCellClass('*', 'plugin')); addCellClass(cell, '*', 'image');
cells.at(5) cell = cells.nodeAt(4);
.prop('reqType', 'script') expandos = expandosFromNode(cell);
.prop('hostname', '*') expandos.reqType = 'plugin';
.addClass(getCellClass('*', 'script')); expandos.hostname = '*';
cells.at(6) addCellClass(cell, '*', 'plugin');
.prop('reqType', 'xhr') cell = cells.nodeAt(5);
.prop('hostname', '*') expandos = expandosFromNode(cell);
.addClass(getCellClass('*', 'xhr')); expandos.reqType = 'script';
cells.at(7) expandos.hostname = '*';
.prop('reqType', 'frame') addCellClass(cell, '*', 'script');
.prop('hostname', '*') cell = cells.nodeAt(6);
.addClass(getCellClass('*', 'frame')); expandos = expandosFromNode(cell);
cells.at(8) expandos.reqType = 'xhr';
.prop('reqType', 'other') expandos.hostname = '*';
.prop('hostname', '*') addCellClass(cell, '*', 'xhr');
.addClass(getCellClass('*', 'other')); cell = cells.nodeAt(7);
expandos = expandosFromNode(cell);
expandos.reqType = 'frame';
expandos.hostname = '*';
addCellClass(cell, '*', 'frame');
cell = cells.nodeAt(8);
expandos = expandosFromNode(cell);
expandos.reqType = 'other';
expandos.hostname = '*';
addCellClass(cell, '*', 'other');
uDom('#matHead .matRow').css('display', ''); uDom('#matHead .matRow').css('display', '');
} }
/******************************************************************************/ /******************************************************************************/
function renderMatrixCellDomain(cell, domain) { function renderMatrixCellDomain(cell, domain) {
var contents = cell.prop('reqType', '*') var expandos = expandosFromNode(cell);
.prop('hostname', domain) expandos.hostname = domain;
.addClass(getCellClass(domain, '*')) expandos.reqType = '*';
.contents(); addCellClass(cell.nodeAt(0), domain, '*');
var contents = cell.contents();
contents.nodeAt(0).textContent = domain === '1st-party' ? contents.nodeAt(0).textContent = domain === '1st-party' ?
firstPartyLabel : firstPartyLabel :
punycode.toUnicode(domain); punycode.toUnicode(domain);
@ -569,28 +603,31 @@ function renderMatrixCellDomain(cell, domain) {
} }
function renderMatrixCellSubdomain(cell, domain, subomain) { function renderMatrixCellSubdomain(cell, domain, subomain) {
var contents = cell.prop('reqType', '*') var expandos = expandosFromNode(cell);
.prop('hostname', subomain) expandos.hostname = subomain;
.addClass(getCellClass(subomain, '*')) expandos.reqType = '*';
.contents(); addCellClass(cell.nodeAt(0), subomain, '*');
var contents = cell.contents();
contents.nodeAt(0).textContent = punycode.toUnicode(subomain.slice(0, subomain.lastIndexOf(domain)-1)) + '.'; contents.nodeAt(0).textContent = punycode.toUnicode(subomain.slice(0, subomain.lastIndexOf(domain)-1)) + '.';
contents.nodeAt(1).textContent = punycode.toUnicode(domain); contents.nodeAt(1).textContent = punycode.toUnicode(domain);
} }
function renderMatrixMetaCellDomain(cell, domain) { function renderMatrixMetaCellDomain(cell, domain) {
var contents = cell.prop('reqType', '*') var expandos = expandosFromNode(cell);
.prop('hostname', domain) expandos.hostname = domain;
.addClass(getCellClass(domain, '*')) expandos.reqType = '*';
.contents(); addCellClass(cell.nodeAt(0), domain, '*');
var contents = cell.contents();
contents.nodeAt(0).textContent = '\u2217.' + punycode.toUnicode(domain); contents.nodeAt(0).textContent = '\u2217.' + punycode.toUnicode(domain);
contents.nodeAt(1).textContent = ' '; contents.nodeAt(1).textContent = ' ';
} }
function renderMatrixCellType(cell, hostname, type, count) { function renderMatrixCellType(cell, hostname, type, count) {
cell.prop('reqType', type) var expandos = expandosFromNode(cell);
.prop('hostname', hostname) expandos.hostname = hostname;
.prop('count', count) expandos.reqType = type;
.addClass(getCellClass(hostname, type)); expandos.count = count;
addCellClass(cell.nodeAt(0), hostname, type);
if ( count ) { if ( count ) {
cell.text(count); cell.text(count);
} else { } else {
@ -642,10 +679,11 @@ function makeMatrixMetaRowDomain(domain) {
function renderMatrixMetaCellType(cell, count) { function renderMatrixMetaCellType(cell, count) {
// https://github.com/gorhill/uMatrix/issues/24 // https://github.com/gorhill/uMatrix/issues/24
// Don't forget to reset cell properties // Don't forget to reset cell properties
cell.addClass('t1') var expandos = expandosFromNode(cell);
.prop('reqType', '') expandos.hostname = '';
.prop('hostname', '') expandos.reqType = '';
.prop('count', count); expandos.count = count;
cell.addClass('t1');
if ( count ) { if ( count ) {
cell.text(count); cell.text(count);
} else { } else {
@ -658,7 +696,9 @@ function makeMatrixMetaRow(totals) {
var matrixRow = createMatrixRow().at(0).addClass('ro'); var matrixRow = createMatrixRow().at(0).addClass('ro');
var cells = matrixRow.descendants('.matCell'); var cells = matrixRow.descendants('.matCell');
var contents = cells.at(0).addClass('t81').contents(); var contents = cells.at(0).addClass('t81').contents();
cells.at(0).prop('reqType', '*').prop('hostname', ''); var expandos = expandosFromNode(cells.nodeAt(0));
expandos.hostname = '';
expandos.reqType = '*';
contents.nodeAt(0).textContent = ' '; contents.nodeAt(0).textContent = ' ';
contents.nodeAt(1).textContent = blacklistedHostnamesLabel.replace( contents.nodeAt(1).textContent = blacklistedHostnamesLabel.replace(
'{{count}}', '{{count}}',
@ -731,7 +771,8 @@ function makeMatrixGroup0SectionDomain() {
} }
function makeMatrixGroup0Section() { function makeMatrixGroup0Section() {
var domainDiv = createMatrixSection().prop('domain', '1st-party'); var domainDiv = createMatrixSection();
expandosFromNode(domainDiv).domain = '1st-party';
makeMatrixGroup0SectionDomain().appendTo(domainDiv); makeMatrixGroup0SectionDomain().appendTo(domainDiv);
return domainDiv; return domainDiv;
} }
@ -766,8 +807,8 @@ function makeMatrixGroup1SectionMetaDomain(domain) {
function makeMatrixGroup1Section(hostnames) { function makeMatrixGroup1Section(hostnames) {
var domain = hostnames[0]; var domain = hostnames[0];
var domainDiv = createMatrixSection() var domainDiv = createMatrixSection()
.toggleClass('collapsed', getCollapseState(domain)) .toggleClass('collapsed', getCollapseState(domain));
.prop('domain', domain); expandosFromNode(domainDiv).domain = domain;
if ( hostnames.length > 1 ) { if ( hostnames.length > 1 ) {
makeMatrixGroup1SectionMetaDomain(domain) makeMatrixGroup1SectionMetaDomain(domain)
.appendTo(domainDiv); .appendTo(domainDiv);
@ -814,8 +855,8 @@ function makeMatrixGroup2SectionMetaDomain(domain) {
function makeMatrixGroup2Section(hostnames) { function makeMatrixGroup2Section(hostnames) {
var domain = hostnames[0]; var domain = hostnames[0];
var domainDiv = createMatrixSection() var domainDiv = createMatrixSection()
.toggleClass('collapsed', getCollapseState(domain)) .toggleClass('collapsed', getCollapseState(domain));
.prop('domain', domain); expandosFromNode(domainDiv).domain = domain;
if ( hostnames.length > 1 ) { if ( hostnames.length > 1 ) {
makeMatrixGroup2SectionMetaDomain(domain).appendTo(domainDiv); makeMatrixGroup2SectionMetaDomain(domain).appendTo(domainDiv);
} }
@ -862,8 +903,8 @@ function makeMatrixGroup3SectionMetaDomain(domain) {
function makeMatrixGroup3Section(hostnames) { function makeMatrixGroup3Section(hostnames) {
var domain = hostnames[0]; var domain = hostnames[0];
var domainDiv = createMatrixSection() var domainDiv = createMatrixSection()
.toggleClass('collapsed', getCollapseState(domain)) .toggleClass('collapsed', getCollapseState(domain));
.prop('domain', domain); expandosFromNode(domainDiv).domain = domain;
if ( hostnames.length > 1 ) { if ( hostnames.length > 1 ) {
makeMatrixGroup3SectionMetaDomain(domain).appendTo(domainDiv); makeMatrixGroup3SectionMetaDomain(domain).appendTo(domainDiv);
} }
@ -905,8 +946,8 @@ function makeMatrixGroup4SectionSubomain(domain, subdomain) {
function makeMatrixGroup4Section(hostnames) { function makeMatrixGroup4Section(hostnames) {
var domain = hostnames[0]; var domain = hostnames[0];
var domainDiv = createMatrixSection() var domainDiv = createMatrixSection();
.prop('domain', domain); expandosFromNode(domainDiv).domain = domain;
makeMatrixGroup4SectionDomain(domain) makeMatrixGroup4SectionDomain(domain)
.appendTo(domainDiv); .appendTo(domainDiv);
for ( var i = 1; i < hostnames.length; i++ ) { for ( var i = 1; i < hostnames.length; i++ ) {

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
µBlock - a browser extension to block requests. µBlock - a browser extension to block requests.
Copyright (C) 2014 Raymond Hill Copyright (C) 2014-2017 Raymond Hill
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -22,6 +22,8 @@
/* global DOMTokenList */ /* global DOMTokenList */
/* exported uDom */ /* exported uDom */
'use strict';
/******************************************************************************/ /******************************************************************************/
// It's just a silly, minimalist DOM framework: this allows me to not rely // It's just a silly, minimalist DOM framework: this allows me to not rely
@ -32,8 +34,6 @@
var uDom = (function() { var uDom = (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
var DOMList = function() { var DOMList = function() {

Loading…
Cancel
Save