mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-15 15:32:28 +02:00
Use a user stylesheet to implement the collapsing of placeholders
Related issue: - https://github.com/gorhill/uBlock/issues/2848
This commit is contained in:
parent
f4aebc9390
commit
d97c46ffd3
@ -1124,21 +1124,26 @@ vAPI.injectScriptlet = function(doc, text) {
|
|||||||
const messaging = vAPI.messaging;
|
const messaging = vAPI.messaging;
|
||||||
const toCollapse = new Map();
|
const toCollapse = new Map();
|
||||||
const src1stProps = {
|
const src1stProps = {
|
||||||
|
audio: 'currentSrc',
|
||||||
embed: 'src',
|
embed: 'src',
|
||||||
iframe: 'src',
|
iframe: 'src',
|
||||||
img: 'src',
|
img: 'currentSrc',
|
||||||
object: 'data'
|
object: 'data',
|
||||||
|
video: 'currentSrc',
|
||||||
};
|
};
|
||||||
const src2ndProps = {
|
const src2ndProps = {
|
||||||
img: 'srcset'
|
audio: 'src',
|
||||||
|
img: 'src',
|
||||||
|
video: 'src',
|
||||||
};
|
};
|
||||||
const tagToTypeMap = {
|
const tagToTypeMap = {
|
||||||
|
audio: 'media',
|
||||||
embed: 'object',
|
embed: 'object',
|
||||||
iframe: 'sub_frame',
|
iframe: 'sub_frame',
|
||||||
img: 'image',
|
img: 'image',
|
||||||
object: 'object'
|
object: 'object',
|
||||||
|
video: 'media',
|
||||||
};
|
};
|
||||||
|
|
||||||
let resquestIdGenerator = 1,
|
let resquestIdGenerator = 1,
|
||||||
processTimer,
|
processTimer,
|
||||||
cachedBlockedSet,
|
cachedBlockedSet,
|
||||||
@ -1154,6 +1159,21 @@ vAPI.injectScriptlet = function(doc, text) {
|
|||||||
cachedBlockedSetTimer = undefined;
|
cachedBlockedSetTimer = undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// https://github.com/chrisaljoudi/uBlock/issues/399
|
||||||
|
// https://github.com/gorhill/uBlock/issues/2848
|
||||||
|
// Use a user stylesheet to collapse placeholders.
|
||||||
|
const getCollapseToken = ( ) => {
|
||||||
|
if ( collapseToken === undefined ) {
|
||||||
|
collapseToken = vAPI.randomToken();
|
||||||
|
vAPI.userStylesheet.add(
|
||||||
|
`[${collapseToken}]\ndisplay:none!important;}`,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return collapseToken;
|
||||||
|
};
|
||||||
|
let collapseToken;
|
||||||
|
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/174
|
// https://github.com/chrisaljoudi/uBlock/issues/174
|
||||||
// Do not remove fragment from src URL
|
// Do not remove fragment from src URL
|
||||||
const onProcessed = function(response) {
|
const onProcessed = function(response) {
|
||||||
@ -1165,6 +1185,7 @@ vAPI.injectScriptlet = function(doc, text) {
|
|||||||
|
|
||||||
const targets = toCollapse.get(response.id);
|
const targets = toCollapse.get(response.id);
|
||||||
if ( targets === undefined ) { return; }
|
if ( targets === undefined ) { return; }
|
||||||
|
|
||||||
toCollapse.delete(response.id);
|
toCollapse.delete(response.id);
|
||||||
if ( cachedBlockedSetHash !== response.hash ) {
|
if ( cachedBlockedSetHash !== response.hash ) {
|
||||||
cachedBlockedSet = new Set(response.blockedResources);
|
cachedBlockedSet = new Set(response.blockedResources);
|
||||||
@ -1177,8 +1198,8 @@ vAPI.injectScriptlet = function(doc, text) {
|
|||||||
if ( cachedBlockedSet === undefined || cachedBlockedSet.size === 0 ) {
|
if ( cachedBlockedSet === undefined || cachedBlockedSet.size === 0 ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectors = [];
|
const selectors = [];
|
||||||
const iframeLoadEventPatch = vAPI.iframeLoadEventPatch;
|
|
||||||
let netSelectorCacheCountMax = response.netSelectorCacheCountMax;
|
let netSelectorCacheCountMax = response.netSelectorCacheCountMax;
|
||||||
|
|
||||||
for ( const target of targets ) {
|
for ( const target of targets ) {
|
||||||
@ -1195,32 +1216,24 @@ vAPI.injectScriptlet = function(doc, text) {
|
|||||||
if ( cachedBlockedSet.has(tagToTypeMap[tag] + ' ' + src) === false ) {
|
if ( cachedBlockedSet.has(tagToTypeMap[tag] + ' ' + src) === false ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/399
|
target.setAttribute(getCollapseToken(), '');
|
||||||
// Never remove elements from the DOM, just hide them
|
|
||||||
target.style.setProperty('display', 'none', 'important');
|
|
||||||
target.hidden = true;
|
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/1048
|
// https://github.com/chrisaljoudi/uBlock/issues/1048
|
||||||
// Use attribute to construct CSS rule
|
// Use attribute to construct CSS rule
|
||||||
if ( netSelectorCacheCount <= netSelectorCacheCountMax ) {
|
if ( netSelectorCacheCount > netSelectorCacheCountMax ) { continue; }
|
||||||
const value = target.getAttribute(prop);
|
const value = target.getAttribute(prop);
|
||||||
if ( value ) {
|
if ( value ) {
|
||||||
selectors.push(`${tag}[${prop}="${CSS.escape(value)}"]`);
|
selectors.push(`${tag}[${prop}="${CSS.escape(value)}"]`);
|
||||||
netSelectorCacheCount += 1;
|
netSelectorCacheCount += 1;
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( iframeLoadEventPatch !== undefined ) {
|
|
||||||
iframeLoadEventPatch(target);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( selectors.length !== 0 ) {
|
if ( selectors.length === 0 ) { return; }
|
||||||
messaging.send('contentscript', {
|
messaging.send('contentscript', {
|
||||||
what: 'cosmeticFiltersInjected',
|
what: 'cosmeticFiltersInjected',
|
||||||
type: 'net',
|
type: 'net',
|
||||||
hostname: window.location.hostname,
|
hostname: window.location.hostname,
|
||||||
selectors,
|
selectors,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const send = function() {
|
const send = function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user