Skip to content
Permalink
Browse files

#435: using shadow nodes instead of modifying directly nodes' style attr

  • Loading branch information...
gorhill committed Jul 6, 2015
1 parent c11421d commit febb18147a268999fb5ca339bab741d254cf766b
@@ -446,12 +446,34 @@ var uBlockCollapser = (function() {
if ( document.body === null ) {
return;
}
// https://github.com/chrisaljoudi/uBlock/issues/158
// Using CSSStyleDeclaration.setProperty is more reliable
var elems = document.querySelectorAll(selectors);
var i = elems.length;
if ( i === 0 ) {
return;
}
// https://github.com/chrisaljoudi/uBlock/issues/158
// Using CSSStyleDeclaration.setProperty is more reliable
var template = vAPI.perNodeShadowTemplate;
if ( template === null ) {
while ( i-- ) {
elems[i].style.setProperty('display', 'none', 'important');
}
return;
}
// https://github.com/gorhill/uBlock/issues/435
// Using shadow content so that we do not have to modify style
// attribute.
var sessionId = vAPI.sessionId;
var elem, shadow;
while ( i-- ) {
elems[i].style.setProperty('display', 'none', 'important');
elem = elems[i];
shadow = elem.shadowRoot;
if ( shadow !== null && shadow.className === sessionId ) {
continue;
}
shadow = elem.createShadowRoot();
shadow.className = sessionId;
shadow.appendChild(template.cloneNode(true));
}
};

@@ -54,6 +54,14 @@ if ( vAPI.contentscriptStartInjected ) {
}
vAPI.contentscriptStartInjected = true;
vAPI.styles = vAPI.styles || [];
vAPI.perNodeShadowTemplate = (function() {
if ( typeof document.documentElement.createShadowRoot !== 'function' ) {
return null;
}
var content = document.createElement('content');
content.select = '#' + vAPI.sessionId;
return content;
})();

/******************************************************************************/

@@ -157,12 +165,34 @@ var hideElements = function(selectors) {
if ( document.body === null ) {
return;
}
// https://github.com/chrisaljoudi/uBlock/issues/158
// Using CSSStyleDeclaration.setProperty is more reliable
var elems = document.querySelectorAll(selectors);
var i = elems.length;
if ( i === 0 ) {
return;
}
// https://github.com/chrisaljoudi/uBlock/issues/158
// Using CSSStyleDeclaration.setProperty is more reliable
var template = vAPI.perNodeShadowTemplate;
if ( template === null ) {
while ( i-- ) {
elems[i].style.setProperty('display', 'none', 'important');
}
return;
}
// https://github.com/gorhill/uBlock/issues/435
// Using shadow content so that we do not have to modify style
// attribute.
var sessionId = vAPI.sessionId;
var elem, shadow;
while ( i-- ) {
elems[i].style.setProperty('display', 'none', 'important');
elem = elems[i];
shadow = elem.shadowRoot;
if ( shadow !== null && shadow.className === sessionId ) {
continue;
}
shadow = elem.createShadowRoot();
shadow.className = sessionId;
shadow.appendChild(template.cloneNode(true));
}
};

@@ -29,8 +29,11 @@

/******************************************************************************/

var showdomButton = uDom.nodeFromId('showdom');

// Don't bother if the browser is not modern enough.
if ( typeof Map === undefined || typeof WeakMap === undefined ) {
showdomButton.classList.add('disabled');
return;
}

@@ -44,7 +47,6 @@ var inspectedURL = '';
var inspectedHostname = '';
var pollTimer = null;
var fingerprint = null;
var showdomButton = uDom.nodeFromId('showdom');
var inspector = uDom.nodeFromId('domInspector');
var domTree = uDom.nodeFromId('domTree');
var tabSelector = uDom.nodeFromId('pageSelector');
@@ -49,6 +49,8 @@ if ( Array.isArray(styles) === false ) {
return;
}

var sessionId = vAPI.sessionId;

/******************************************************************************/

// Remove all cosmetic filtering-related styles from the DOM
@@ -60,15 +62,12 @@ var style, i;
i = styles.length;
while ( i-- ) {
style = styles[i];
if ( style.parentElement === null ) {
continue;
}
style.parentElement.removeChild(style);
selectors.push(style.textContent.replace(reProperties, ''));
if ( style.sheet !== null ) {
style.sheet.disabled = true;
}
}

// Remove `display: none !important` attribute

if ( selectors.length === 0 ) {
return;
}
@@ -78,13 +77,23 @@ try {
elems = document.querySelectorAll(selectors.join(','));
} catch (e) {
}

i = elems.length;

var elem, shadow;
while ( i-- ) {
style = elems[i].style;
if ( typeof style === 'object' || typeof style.removeProperty === 'function' ) {
style.removeProperty('display');
elem = elems[i];
shadow = elem.shadowRoot;
if ( shadow === undefined ) {
style = elem.style;
if ( typeof style === 'object' || typeof style.removeProperty === 'function' ) {
style.removeProperty('display');
}
continue;
}
if ( shadow === null || shadow.className !== sessionId ) {
continue;
}
shadow.children[0].select = '';
}

/******************************************************************************/
@@ -49,30 +49,25 @@ if ( Array.isArray(styles) === false ) {
return;
}

var sessionId = vAPI.sessionId;

/******************************************************************************/

// Insert all cosmetic filtering-related style tags in the DOM

var selectors = [];
var reProperties = /\s*\{[^}]+\}\s*/;
var style, i;
var parent = document.head || document.body || document.documentElement;

i = styles.length;
while ( i-- ) {
style = styles[i];
if ( style.parentElement !== null ) {
continue;
}
if ( parent === null ) {
continue;
}
selectors.push(style.textContent.replace(reProperties, ''));
parent.appendChild(style);
if ( style.sheet !== null ) {
style.sheet.disabled = false;
}
}

// Add `display: none !important` attribute

if ( selectors.length === 0 ) {
return;
}
@@ -83,12 +78,22 @@ try {
} catch (e) {
}

var elem, shadow, selector = '#' + sessionId;
i = elems.length;
while ( i-- ) {
style = elems[i].style;
if ( typeof style === 'object' || typeof style.removeProperty === 'function' ) {
style.setProperty('display', 'none', 'important');
elem = elems[i];
shadow = elem.shadowRoot;
if ( shadow === undefined ) {
style = elems[i].style;
if ( typeof style === 'object' || typeof style.removeProperty === 'function' ) {
style.setProperty('display', 'none', 'important');
}
continue;
}
if ( shadow === null || shadow.className !== sessionId ) {
continue;
}
shadow.children[0].select = selector;
}

/******************************************************************************/

0 comments on commit febb181

Please sign in to comment.
You can’t perform that action at this time.