mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
this fixes #1031
This commit is contained in:
parent
66038d081d
commit
e2e86de89a
5 changed files with 81 additions and 61 deletions
|
|
@ -291,6 +291,63 @@ var uBlockCollapser = (function() {
|
||||||
//var timer = window.performance || Date;
|
//var timer = window.performance || Date;
|
||||||
//var tStart = timer.now();
|
//var tStart = timer.now();
|
||||||
|
|
||||||
|
var hideElements = (function() {
|
||||||
|
if ( document.body === null ) {
|
||||||
|
return function() {};
|
||||||
|
}
|
||||||
|
if ( document.body.shadowRoot === undefined ) {
|
||||||
|
return function(selectors) {
|
||||||
|
// https://github.com/chrisaljoudi/uBlock/issues/207
|
||||||
|
// Do not call querySelectorAll() using invalid CSS selectors
|
||||||
|
if ( selectors.length === 0 ) { return; }
|
||||||
|
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
|
||||||
|
while ( i-- ) {
|
||||||
|
elems[i].style.setProperty('display', 'none', 'important');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return function(selectors) {
|
||||||
|
if ( selectors.length === 0 ) { return; }
|
||||||
|
var elems = document.querySelectorAll(selectors);
|
||||||
|
var i = elems.length;
|
||||||
|
if ( i === 0 ) { 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-- ) {
|
||||||
|
elem = elems[i];
|
||||||
|
shadow = elem.shadowRoot;
|
||||||
|
// https://www.chromestatus.com/features/4668884095336448
|
||||||
|
// "Multiple shadow roots is being deprecated."
|
||||||
|
if ( shadow !== null ) {
|
||||||
|
if ( shadow.className !== sessionId ) {
|
||||||
|
elem.style.setProperty('display', 'none', 'important');
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// https://github.com/gorhill/uBlock/pull/555
|
||||||
|
// Not all nodes can be shadowed:
|
||||||
|
// https://github.com/w3c/webcomponents/issues/102
|
||||||
|
// https://github.com/gorhill/uBlock/issues/762
|
||||||
|
// Remove display style that might get in the way of the shadow
|
||||||
|
// node doing its magic.
|
||||||
|
try {
|
||||||
|
shadow = elem.createShadowRoot();
|
||||||
|
shadow.className = sessionId;
|
||||||
|
elem.style.removeProperty('display');
|
||||||
|
} catch (ex) {
|
||||||
|
elem.style.setProperty('display', 'none', 'important');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/789
|
// https://github.com/chrisaljoudi/uBlock/issues/789
|
||||||
// https://github.com/gorhill/uBlock/issues/873
|
// https://github.com/gorhill/uBlock/issues/873
|
||||||
// Be sure that our style tags used for cosmetic filtering are still applied.
|
// Be sure that our style tags used for cosmetic filtering are still applied.
|
||||||
|
|
@ -298,7 +355,7 @@ var uBlockCollapser = (function() {
|
||||||
var doc = document,
|
var doc = document,
|
||||||
html = doc.documentElement,
|
html = doc.documentElement,
|
||||||
head = doc.head,
|
head = doc.head,
|
||||||
newParent = html || head;
|
newParent = head || html;
|
||||||
if ( newParent === null ) {
|
if ( newParent === null ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -307,9 +364,24 @@ var uBlockCollapser = (function() {
|
||||||
for ( var i = 0; i < styles.length; i++ ) {
|
for ( var i = 0; i < styles.length; i++ ) {
|
||||||
style = styles[i];
|
style = styles[i];
|
||||||
oldParent = style.parentNode;
|
oldParent = style.parentNode;
|
||||||
if ( oldParent !== head && oldParent !== html ) {
|
// https://github.com/gorhill/uBlock/issues/1031
|
||||||
newParent.appendChild(style);
|
// If our style tag was disabled, force a re-insert into the page.
|
||||||
|
if (
|
||||||
|
style.disabled &&
|
||||||
|
oldParent !== null &&
|
||||||
|
style[vAPI.sessionId] === undefined
|
||||||
|
) {
|
||||||
|
oldParent.removeChild(style);
|
||||||
|
oldParent = null;
|
||||||
}
|
}
|
||||||
|
if ( oldParent === head || oldParent === html ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
style.disabled = false;
|
||||||
|
newParent.appendChild(style);
|
||||||
|
// The page tried to get rid of us: reapply inline styles to
|
||||||
|
// blocked elements.
|
||||||
|
hideElements(style.textContent.slice(0, style.textContent.lastIndexOf('\n')));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
checkStyleTags();
|
checkStyleTags();
|
||||||
|
|
@ -427,6 +499,7 @@ var uBlockCollapser = (function() {
|
||||||
// Boost specificity of our CSS rules.
|
// Boost specificity of our CSS rules.
|
||||||
var styleText = ':root ' + selectors.join(',\n:root ');
|
var styleText = ':root ' + selectors.join(',\n:root ');
|
||||||
var style = document.createElement('style');
|
var style = document.createElement('style');
|
||||||
|
style.setAttribute('type', 'text/css');
|
||||||
// The linefeed before the style block is very important: do no remove!
|
// The linefeed before the style block is very important: do no remove!
|
||||||
style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}'));
|
style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}'));
|
||||||
var parent = document.head || document.documentElement;
|
var parent = document.head || document.documentElement;
|
||||||
|
|
@ -444,63 +517,6 @@ var uBlockCollapser = (function() {
|
||||||
//console.debug('µBlock> generic cosmetic filters: injecting %d CSS rules:', selectors.length, text);
|
//console.debug('µBlock> generic cosmetic filters: injecting %d CSS rules:', selectors.length, text);
|
||||||
};
|
};
|
||||||
|
|
||||||
var hideElements = (function() {
|
|
||||||
if ( document.body === null ) {
|
|
||||||
return function() {};
|
|
||||||
}
|
|
||||||
if ( document.body.shadowRoot === undefined ) {
|
|
||||||
return function(selectors) {
|
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/207
|
|
||||||
// Do not call querySelectorAll() using invalid CSS selectors
|
|
||||||
if ( selectors.length === 0 ) { return; }
|
|
||||||
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
|
|
||||||
while ( i-- ) {
|
|
||||||
elems[i].style.setProperty('display', 'none', 'important');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return function(selectors) {
|
|
||||||
if ( selectors.length === 0 ) { return; }
|
|
||||||
var elems = document.querySelectorAll(selectors);
|
|
||||||
var i = elems.length;
|
|
||||||
if ( i === 0 ) { 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-- ) {
|
|
||||||
elem = elems[i];
|
|
||||||
shadow = elem.shadowRoot;
|
|
||||||
// https://www.chromestatus.com/features/4668884095336448
|
|
||||||
// "Multiple shadow roots is being deprecated."
|
|
||||||
if ( shadow !== null ) {
|
|
||||||
if ( shadow.className !== sessionId ) {
|
|
||||||
elem.style.setProperty('display', 'none', 'important');
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// https://github.com/gorhill/uBlock/pull/555
|
|
||||||
// Not all nodes can be shadowed:
|
|
||||||
// https://github.com/w3c/webcomponents/issues/102
|
|
||||||
// https://github.com/gorhill/uBlock/issues/762
|
|
||||||
// Remove display style that might get in the way of the shadow
|
|
||||||
// node doing its magic.
|
|
||||||
try {
|
|
||||||
shadow = elem.createShadowRoot();
|
|
||||||
shadow.className = sessionId;
|
|
||||||
elem.style.removeProperty('display');
|
|
||||||
} catch (ex) {
|
|
||||||
elem.style.setProperty('display', 'none', 'important');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Extract and return the staged nodes which (may) match the selectors.
|
// Extract and return the staged nodes which (may) match the selectors.
|
||||||
|
|
||||||
var selectNodes = function(selector) {
|
var selectNodes = function(selector) {
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ var cosmeticFilters = function(details) {
|
||||||
// Boost specificity of our CSS rules.
|
// Boost specificity of our CSS rules.
|
||||||
var styleText = ':root ' + hide.join(',\n:root ');
|
var styleText = ':root ' + hide.join(',\n:root ');
|
||||||
var style = document.createElement('style');
|
var style = document.createElement('style');
|
||||||
|
style.setAttribute('type', 'text/css');
|
||||||
// The linefeed before the style block is very important: do not remove!
|
// The linefeed before the style block is very important: do not remove!
|
||||||
style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}'));
|
style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}'));
|
||||||
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
|
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,7 @@ while ( i-- ) {
|
||||||
selectors.push(style.textContent.replace(reProperties, ''));
|
selectors.push(style.textContent.replace(reProperties, ''));
|
||||||
if ( style.sheet !== null ) {
|
if ( style.sheet !== null ) {
|
||||||
style.sheet.disabled = true;
|
style.sheet.disabled = true;
|
||||||
|
style[vAPI.sessionId] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,7 @@ while ( i-- ) {
|
||||||
selectors.push(style.textContent.replace(reProperties, ''));
|
selectors.push(style.textContent.replace(reProperties, ''));
|
||||||
if ( style.sheet !== null ) {
|
if ( style.sheet !== null ) {
|
||||||
style.sheet.disabled = false;
|
style.sheet.disabled = false;
|
||||||
|
style[vAPI.sessionId] = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -742,6 +742,7 @@ var cosmeticFilterMapper = (function() {
|
||||||
nodesFromStyleTag(styleTag, rootNode);
|
nodesFromStyleTag(styleTag, rootNode);
|
||||||
if ( styleTag.sheet !== null ) {
|
if ( styleTag.sheet !== null ) {
|
||||||
styleTag.sheet.disabled = true;
|
styleTag.sheet.disabled = true;
|
||||||
|
styleTag[vAPI.sessionId] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -759,9 +760,9 @@ var cosmeticFilterMapper = (function() {
|
||||||
styleTag = styleTags[i];
|
styleTag = styleTags[i];
|
||||||
if ( styleTag.sheet !== null ) {
|
if ( styleTag.sheet !== null ) {
|
||||||
styleTag.sheet.disabled = false;
|
styleTag.sheet.disabled = false;
|
||||||
|
styleTag[vAPI.sessionId] = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reset();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue