From 89f0e8f1a9a66b3c344562a1e68124ae1600f648 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 5 Dec 2015 18:02:04 -0500 Subject: [PATCH] this fixes #1015 --- src/js/contentscript-end.js | 8 +++++--- src/js/contentscript-start.js | 8 +++++--- src/js/scriptlets/cosmetic-logger.js | 6 +++++- src/js/scriptlets/cosmetic-survey.js | 3 +-- src/js/scriptlets/dom-inspector.js | 5 ++++- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/js/contentscript-end.js b/src/js/contentscript-end.js index f3ebc98c0..092768b2c 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -423,16 +423,18 @@ var uBlockCollapser = (function() { // - Injecting a style tag var addStyleTag = function(selectors) { - var selectorStr = selectors.join(',\n'); + // https://github.com/gorhill/uBlock/issues/1015 + // Boost specificity of our CSS rules. + var styleText = ':root ' + selectors.join(',\n:root '); var style = document.createElement('style'); // The linefeed before the style block is very important: do no remove! - style.appendChild(document.createTextNode(selectorStr + '\n{display:none !important;}')); + style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}')); var parent = document.head || document.documentElement; if ( parent ) { parent.appendChild(style); vAPI.styles.push(style); } - hideElements(selectorStr); + hideElements(styleText); messager.send({ what: 'cosmeticFiltersInjected', type: 'cosmetic', diff --git a/src/js/contentscript-start.js b/src/js/contentscript-start.js index 2f8f3beb6..a84f6ee4d 100644 --- a/src/js/contentscript-start.js +++ b/src/js/contentscript-start.js @@ -97,17 +97,19 @@ var cosmeticFilters = function(details) { } } if ( hide.length !== 0 ) { - var text = hide.join(',\n'); + // https://github.com/gorhill/uBlock/issues/1015 + // Boost specificity of our CSS rules. + var styleText = ':root ' + hide.join(',\n:root '); var style = document.createElement('style'); // The linefeed before the style block is very important: do not remove! - style.appendChild(document.createTextNode(text + '\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); var parent = document.head || document.documentElement; if ( parent ) { parent.appendChild(style); vAPI.styles.push(style); } - hideElements(text); + hideElements(styleText); } vAPI.donthideCosmeticFilters = donthideCosmeticFilters; vAPI.hideCosmeticFilters = hideCosmeticFilters; diff --git a/src/js/scriptlets/cosmetic-logger.js b/src/js/scriptlets/cosmetic-logger.js index 0b7c6736f..4503d732a 100644 --- a/src/js/scriptlets/cosmetic-logger.js +++ b/src/js/scriptlets/cosmetic-logger.js @@ -64,6 +64,10 @@ if ( injectedSelectors.length === 0 ) { return; } +// https://github.com/gorhill/uBlock/issues/1015 +// Remove runtime prefix used to augment the specificity of our CSS rules. +var reSpecificityBooster = /^:root\s+/; + var matchedSelectors = []; var selector; @@ -77,7 +81,7 @@ while ( i-- ) { continue; } loggedSelectors[selector] = true; - matchedSelectors.push(selector); + matchedSelectors.push(selector.replace(reSpecificityBooster, '')); } vAPI.loggedSelectors = loggedSelectors; diff --git a/src/js/scriptlets/cosmetic-survey.js b/src/js/scriptlets/cosmetic-survey.js index 39e71052c..7e9129a09 100644 --- a/src/js/scriptlets/cosmetic-survey.js +++ b/src/js/scriptlets/cosmetic-survey.js @@ -74,8 +74,7 @@ var localMessager = vAPI.messaging.channel('scriptlets'); localMessager.send({ what: 'liveCosmeticFilteringData', pageURL: window.location.href, - filteredElementCount: filteredElementCount, - injectedSelectors: injectedSelectors + filteredElementCount: filteredElementCount }, function() { localMessager.close(); }); diff --git a/src/js/scriptlets/dom-inspector.js b/src/js/scriptlets/dom-inspector.js index 5a42a0e79..0c144c4b6 100644 --- a/src/js/scriptlets/dom-inspector.js +++ b/src/js/scriptlets/dom-inspector.js @@ -693,6 +693,9 @@ var cosmeticFilterFromTarget = function(nid, coarseSelector) { /******************************************************************************/ var cosmeticFilterMapper = (function() { + // https://github.com/gorhill/uBlock/issues/1015 + var reSpecificityBooster = /^:root\s+/; + // https://github.com/gorhill/uBlock/issues/546 var matchesFnName; if ( typeof document.body.matches === 'function' ) { @@ -714,7 +717,7 @@ var cosmeticFilterMapper = (function() { var i = selectors.length; var selector, nodes, j, node; while ( i-- ) { - selector = selectors[i]; + selector = selectors[i].replace(reSpecificityBooster, ''); if ( filterMap.has(rootNode) === false && rootNode[matchesFnName](selector) ) { filterMap.set(rootNode, selector); hideNode(node);