mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
this fixes #683
This commit is contained in:
parent
085e600078
commit
c7edec0d68
2 changed files with 94 additions and 41 deletions
|
|
@ -517,21 +517,84 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
||||||
// Permanent
|
// Permanent
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
// Listeners to mop up whatever is otherwise missed:
|
// https://github.com/gorhill/uBlock/issues/683
|
||||||
// - Future requests not blocked yet
|
// Instead of a closure we use a map to remember the element to collapse
|
||||||
// - Elements dynamically added to the page
|
// or hide.
|
||||||
// - Elements which resource URL changes
|
var filterRequestId = 1;
|
||||||
|
var filterRequests = {};
|
||||||
|
|
||||||
var loadedElements = {
|
var FilterRequest = function(target, selector) {
|
||||||
'iframe': 'src'
|
this.id = filterRequestId++;
|
||||||
|
this.target = target;
|
||||||
|
this.selector = selector;
|
||||||
};
|
};
|
||||||
|
|
||||||
var failedElements = {
|
FilterRequest.send = function(target, tagName, prop, src) {
|
||||||
'img': 'src',
|
var req = new FilterRequest(
|
||||||
'input': 'src',
|
target,
|
||||||
'object': 'data'
|
tagName + '[' + prop + '="' + src + '"]'
|
||||||
|
);
|
||||||
|
filterRequests[req.id] = req;
|
||||||
|
messager.send(
|
||||||
|
{
|
||||||
|
what: 'filterRequest',
|
||||||
|
id: req.id,
|
||||||
|
tagName: tagName,
|
||||||
|
requestURL: src,
|
||||||
|
pageHostname: window.location.hostname,
|
||||||
|
pageURL: window.location.href
|
||||||
|
},
|
||||||
|
onAnswerReceived
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Process answer: collapse, hide, or do nothing.
|
||||||
|
|
||||||
|
var onAnswerReceived = function(details) {
|
||||||
|
// This should not happen under normal circumstances. It probably can
|
||||||
|
// happen if the extension is disabled though.
|
||||||
|
if ( typeof details !== 'object' || details === null ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should definitely not happen
|
||||||
|
if ( filterRequests.hasOwnProperty(details.id) === false ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var req = filterRequests[details.id];
|
||||||
|
delete filterRequests[details.id];
|
||||||
|
|
||||||
|
if ( details.collapse === undefined ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log('contentscript-end.js > onAnswerReceived(%o)', req);
|
||||||
|
|
||||||
|
// If `!important` is not there, going back using history will
|
||||||
|
// likely cause the hidden element to re-appear.
|
||||||
|
if ( details.collapse ) {
|
||||||
|
// https://github.com/gorhill/uBlock/issues/399
|
||||||
|
// Never remove elements from the DOM, just hide them
|
||||||
|
req.target.style.setProperty('display', 'none', 'important');
|
||||||
|
} else {
|
||||||
|
req.target.style.setProperty('visibility', 'hidden', 'important');
|
||||||
|
}
|
||||||
|
|
||||||
|
messager.send({
|
||||||
|
what: 'injectedSelectors',
|
||||||
|
type: 'net',
|
||||||
|
hostname: window.location.hostname,
|
||||||
|
selectors: req.selector
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// https://github.com/gorhill/uBlock/issues/174
|
||||||
|
// Do not remove fragment from src URL
|
||||||
|
|
||||||
|
// TODO: Find out whether trying to send more than one filter request per
|
||||||
|
// message is worth it.
|
||||||
|
|
||||||
var onResource = function(target, dict) {
|
var onResource = function(target, dict) {
|
||||||
if ( !target ) {
|
if ( !target ) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -548,39 +611,22 @@ var messager = vAPI.messaging.channel('contentscript-end.js');
|
||||||
if ( src.lastIndexOf('http', 0) !== 0 ) {
|
if ( src.lastIndexOf('http', 0) !== 0 ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
FilterRequest.send(target, tagName, prop, src);
|
||||||
|
};
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/174
|
// Listeners to mop up whatever is otherwise missed:
|
||||||
// Do not remove fragment from src URL
|
// - Future requests not blocked yet
|
||||||
|
// - Elements dynamically added to the page
|
||||||
|
// - Elements which resource URL changes
|
||||||
|
|
||||||
var onAnswerReceived = function(details) {
|
var loadedElements = {
|
||||||
if ( typeof details !== 'object' || details === null ) {
|
'iframe': 'src'
|
||||||
return;
|
};
|
||||||
}
|
|
||||||
// If `!important` is not there, going back using history will
|
|
||||||
// likely cause the hidden element to re-appear.
|
|
||||||
if ( details.collapse ) {
|
|
||||||
// https://github.com/gorhill/uBlock/issues/399
|
|
||||||
// Never remove elements from the DOM, just hide them
|
|
||||||
target.style.setProperty('display', 'none', 'important');
|
|
||||||
} else {
|
|
||||||
target.style.setProperty('visibility', 'hidden', 'important');
|
|
||||||
}
|
|
||||||
messager.send({
|
|
||||||
what: 'injectedSelectors',
|
|
||||||
type: 'net',
|
|
||||||
hostname: window.location.hostname,
|
|
||||||
selectors: tagName + '[' + prop + '="' + src + '"]'
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var details = {
|
var failedElements = {
|
||||||
what: 'filterRequest',
|
'img': 'src',
|
||||||
tagName: tagName,
|
'input': 'src',
|
||||||
requestURL: src,
|
'object': 'data'
|
||||||
pageHostname: window.location.hostname,
|
|
||||||
pageURL: window.location.href
|
|
||||||
};
|
|
||||||
messager.send(details, onAnswerReceived);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var onResourceLoaded = function(ev) {
|
var onResourceLoaded = function(ev) {
|
||||||
|
|
|
||||||
|
|
@ -423,7 +423,10 @@ var filterRequest = function(pageStore, details) {
|
||||||
details.requestHostname = µburi.hostnameFromURI(details.requestURL);
|
details.requestHostname = µburi.hostnameFromURI(details.requestURL);
|
||||||
details.requestType = tagNameToRequestTypeMap[details.tagName];
|
details.requestType = tagNameToRequestTypeMap[details.tagName];
|
||||||
if ( µb.isBlockResult(pageStore.filterRequest(details)) ) {
|
if ( µb.isBlockResult(pageStore.filterRequest(details)) ) {
|
||||||
return { collapse: µb.userSettings.collapseBlocked };
|
return {
|
||||||
|
collapse: µb.userSettings.collapseBlocked,
|
||||||
|
id: details.id
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -465,8 +468,12 @@ var onMessage = function(details, sender, callback) {
|
||||||
// Evaluate a single request
|
// Evaluate a single request
|
||||||
case 'filterRequest':
|
case 'filterRequest':
|
||||||
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
|
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
|
||||||
|
// console.log('contentscript-end.js > filterRequest(%o)', details);
|
||||||
response = filterRequest(pageStore, details);
|
response = filterRequest(pageStore, details);
|
||||||
}
|
}
|
||||||
|
if ( response === undefined ) {
|
||||||
|
response = { id: details.id };
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue