2016-01-17 19:30:43 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2019-09-17 21:15:01 +02:00
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
'use strict';
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
/******************************************************************************/
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
(( ) => {
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This can happen
|
2020-10-22 14:44:55 +02:00
|
|
|
if ( typeof vAPI !== 'object' || vAPI.loadAllLargeMedia instanceof Function ) {
|
2016-01-17 19:30:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const largeMediaElementAttribute = 'data-' + vAPI.sessionId;
|
|
|
|
const largeMediaElementSelector =
|
2020-10-18 16:07:46 +02:00
|
|
|
':root audio[' + largeMediaElementAttribute + '],\n' +
|
|
|
|
':root img[' + largeMediaElementAttribute + '],\n' +
|
|
|
|
':root picture[' + largeMediaElementAttribute + '],\n' +
|
|
|
|
':root video[' + largeMediaElementAttribute + ']';
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const mediaNotLoaded = function(elem) {
|
2016-01-17 19:30:43 +01:00
|
|
|
switch ( elem.localName ) {
|
|
|
|
case 'audio':
|
2020-10-23 13:29:14 +02:00
|
|
|
case 'video': {
|
|
|
|
const src = elem.src || '';
|
|
|
|
if ( src.startsWith('blob:') ) {
|
|
|
|
elem.autoplay = false;
|
|
|
|
elem.pause();
|
|
|
|
}
|
|
|
|
return elem.readyState === 0 || elem.error !== null;
|
|
|
|
}
|
|
|
|
case 'img': {
|
|
|
|
if ( elem.naturalWidth !== 0 || elem.naturalHeight !== 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
2019-09-17 21:15:01 +02:00
|
|
|
const style = window.getComputedStyle(elem);
|
2016-01-23 22:57:20 +01:00
|
|
|
// For some reason, style can be null with Pale Moon.
|
|
|
|
return style !== null ?
|
|
|
|
style.getPropertyValue('display') !== 'none' :
|
|
|
|
elem.offsetHeight !== 0 && elem.offsetWidth !== 0;
|
2020-10-23 13:29:14 +02:00
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// For all media resources which have failed to load, trigger a reload.
|
|
|
|
|
|
|
|
// <audio> and <video> elements.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const surveyMissingMediaElements = function() {
|
2020-10-18 16:07:46 +02:00
|
|
|
let largeMediaElementCount = 0;
|
|
|
|
for ( const elem of document.querySelectorAll('audio,img,video') ) {
|
|
|
|
if ( mediaNotLoaded(elem) === false ) { continue; }
|
|
|
|
elem.setAttribute(largeMediaElementAttribute, '');
|
|
|
|
largeMediaElementCount += 1;
|
|
|
|
switch ( elem.localName ) {
|
|
|
|
case 'img': {
|
|
|
|
const picture = elem.closest('picture');
|
|
|
|
if ( picture !== null ) {
|
|
|
|
picture.setAttribute(largeMediaElementAttribute, '');
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return largeMediaElementCount;
|
|
|
|
};
|
|
|
|
|
2020-10-18 16:07:46 +02:00
|
|
|
if ( surveyMissingMediaElements() === 0 ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2020-10-18 16:07:46 +02:00
|
|
|
// Insert CSS to highlight blocked media elements.
|
|
|
|
if ( vAPI.largeMediaElementStyleSheet === undefined ) {
|
|
|
|
vAPI.largeMediaElementStyleSheet = [
|
|
|
|
largeMediaElementSelector + ' {',
|
|
|
|
'border: 2px dotted red !important;',
|
|
|
|
'box-sizing: border-box !important;',
|
|
|
|
'cursor: zoom-in !important;',
|
|
|
|
'display: inline-block;',
|
2020-10-23 13:29:14 +02:00
|
|
|
'filter: none !important;',
|
2020-10-18 16:07:46 +02:00
|
|
|
'font-size: 1rem !important;',
|
|
|
|
'min-height: 1em !important;',
|
|
|
|
'min-width: 1em !important;',
|
|
|
|
'opacity: 1 !important;',
|
|
|
|
'outline: none !important;',
|
2020-10-23 13:29:14 +02:00
|
|
|
'transform: none !important;',
|
2020-10-18 16:07:46 +02:00
|
|
|
'visibility: visible !important;',
|
|
|
|
'z-index: 2147483647',
|
|
|
|
'}',
|
|
|
|
].join('\n');
|
|
|
|
vAPI.userStylesheet.add(vAPI.largeMediaElementStyleSheet);
|
|
|
|
vAPI.userStylesheet.apply();
|
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
const loadMedia = async function(elem) {
|
|
|
|
const src = elem.getAttribute('src') || '';
|
|
|
|
elem.removeAttribute('src');
|
|
|
|
|
|
|
|
await vAPI.messaging.send('scriptlets', {
|
|
|
|
what: 'temporarilyAllowLargeMediaElement',
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( src !== '' ) {
|
|
|
|
elem.setAttribute('src', src);
|
|
|
|
}
|
2020-10-23 13:29:14 +02:00
|
|
|
elem.load();
|
2020-10-22 14:44:55 +02:00
|
|
|
};
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const loadImage = async function(elem) {
|
2020-10-22 14:44:55 +02:00
|
|
|
const src = elem.getAttribute('src') || '';
|
2019-09-17 21:15:01 +02:00
|
|
|
elem.removeAttribute('src');
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
await vAPI.messaging.send('scriptlets', {
|
|
|
|
what: 'temporarilyAllowLargeMediaElement',
|
|
|
|
});
|
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
if ( src !== '' ) {
|
|
|
|
elem.setAttribute('src', src);
|
|
|
|
}
|
|
|
|
};
|
2020-10-18 16:07:46 +02:00
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const loadMany = function(elems) {
|
|
|
|
for ( const elem of elems ) {
|
|
|
|
elem.removeAttribute(largeMediaElementAttribute);
|
|
|
|
switch ( elem.localName ) {
|
|
|
|
case 'audio':
|
|
|
|
case 'video':
|
|
|
|
loadMedia(elem);
|
|
|
|
break;
|
|
|
|
case 'img':
|
|
|
|
loadImage(elem);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-10-18 16:07:46 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-17 21:15:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const onMouseClick = function(ev) {
|
2020-10-18 16:07:46 +02:00
|
|
|
if ( ev.button !== 0 || ev.isTrusted === false ) { return; }
|
|
|
|
|
|
|
|
const toLoad = [];
|
|
|
|
const elems = document.elementsFromPoint instanceof Function
|
|
|
|
? document.elementsFromPoint(ev.clientX, ev.clientY)
|
|
|
|
: [ ev.target ];
|
|
|
|
for ( const elem of elems ) {
|
2020-10-22 14:44:55 +02:00
|
|
|
if ( elem.matches(largeMediaElementSelector) === false ) { continue; }
|
|
|
|
if ( mediaNotLoaded(elem) ) {
|
2020-10-18 16:07:46 +02:00
|
|
|
toLoad.push(elem);
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
if ( toLoad.length === 0 ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
loadMany(toLoad);
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('click', onMouseClick, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-10-23 13:29:14 +02:00
|
|
|
const onLoadedData = function(ev) {
|
|
|
|
const media = ev.target;
|
|
|
|
if ( media.localName !== 'audio' && media.localName !== 'video' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const src = media.src;
|
|
|
|
if ( typeof src === 'string' && src.startsWith('blob:') === false ) {
|
|
|
|
return;
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2020-10-23 13:29:14 +02:00
|
|
|
media.autoplay = false;
|
|
|
|
media.pause();
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('loadeddata', onLoadedData);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const onLoad = function(ev) {
|
|
|
|
ev.target.removeAttribute(largeMediaElementAttribute);
|
2016-01-17 19:30:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('load', onLoad, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const onLoadError = function(ev) {
|
|
|
|
const elem = ev.target;
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( mediaNotLoaded(elem) ) {
|
|
|
|
elem.setAttribute(largeMediaElementAttribute, '');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('error', onLoadError, true);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-10-22 14:44:55 +02:00
|
|
|
vAPI.loadAllLargeMedia = function() {
|
2020-10-22 15:01:59 +02:00
|
|
|
document.removeEventListener('click', onMouseClick, true);
|
2020-10-23 13:29:14 +02:00
|
|
|
document.removeEventListener('loadeddata', onLoadedData, true);
|
2020-10-22 14:44:55 +02:00
|
|
|
document.removeEventListener('load', onLoad, true);
|
|
|
|
document.removeEventListener('error', onLoadError, true);
|
|
|
|
|
|
|
|
const toLoad = [];
|
|
|
|
for ( const elem of document.querySelectorAll(largeMediaElementSelector) ) {
|
|
|
|
if ( mediaNotLoaded(elem) ) {
|
|
|
|
toLoad.push(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadMany(toLoad);
|
|
|
|
};
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
2018-05-03 15:55:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
DO NOT:
|
|
|
|
- Remove the following code
|
|
|
|
- Add code beyond the following code
|
|
|
|
Reason:
|
|
|
|
- https://github.com/gorhill/uBlock/pull/3721
|
|
|
|
- uBO never uses the return value from injected content scripts
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
void 0;
|