Fix "Close this window" not working on document-blocked page

Related discussion:
- https://www.reddit.com/r/uBlockOrigin/comments/breeux/
This commit is contained in:
Raymond Hill 2019-05-21 18:56:59 -04:00
parent 6f4ccec836
commit e8c2f7eea3
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 114 additions and 117 deletions

View File

@ -25,43 +25,46 @@
/******************************************************************************/ /******************************************************************************/
(function() { (( ) => {
/******************************************************************************/ /******************************************************************************/
var messaging = vAPI.messaging; const messaging = vAPI.messaging;
var details = {}; let details = {};
(function() { {
var matches = /details=([^&]+)/.exec(window.location.search); const matches = /details=([^&]+)/.exec(window.location.search);
if ( matches === null ) { if ( matches !== null ) {
return; details = JSON.parse(atob(matches[1]));
} }
details = JSON.parse(atob(matches[1])); }
})();
/******************************************************************************/ /******************************************************************************/
(function() { messaging.send(
var onReponseReady = function(response) { 'documentBlocked',
{
what: 'listsFromNetFilter',
compiledFilter: details.fc,
rawFilter: details.fs
},
response => {
if ( response instanceof Object === false ) { return; } if ( response instanceof Object === false ) { return; }
let lists; let lists;
for ( let rawFilter in response ) { for ( const rawFilter in response ) {
if ( response.hasOwnProperty(rawFilter) === false ) { continue; } if ( response.hasOwnProperty(rawFilter) === false ) { continue; }
lists = response[rawFilter]; lists = response[rawFilter];
break; break;
} }
if ( Array.isArray(lists) === false || lists.length === 0 ) {
return;
}
let parent = uDom.nodeFromSelector('#whyex > span:nth-of-type(2)'); if ( Array.isArray(lists) === false || lists.length === 0 ) { return; }
for ( let list of lists ) {
let elem = document.querySelector('#templates .filterList') const parent = uDom.nodeFromSelector('#whyex > span:nth-of-type(2)');
.cloneNode(true); for ( const list of lists ) {
let source = elem.querySelector('.filterListSource'); const elem = document.querySelector('#templates .filterList')
.cloneNode(true);
const source = elem.querySelector('.filterListSource');
source.href += encodeURIComponent(list.assetKey); source.href += encodeURIComponent(list.assetKey);
source.textContent = list.title; source.textContent = list.title;
if ( if (
@ -74,74 +77,15 @@ var details = {};
parent.appendChild(elem); parent.appendChild(elem);
} }
uDom.nodeFromId('whyex').style.removeProperty('display'); uDom.nodeFromId('whyex').style.removeProperty('display');
};
messaging.send(
'documentBlocked',
{
what: 'listsFromNetFilter',
compiledFilter: details.fc,
rawFilter: details.fs
},
onReponseReady
);
})();
/******************************************************************************/
var getTargetHostname = function() {
var hostname = details.hn;
var elem = document.querySelector('#proceed select');
if ( elem !== null ) {
hostname = elem.value;
} }
return hostname; );
};
/******************************************************************************/ /******************************************************************************/
var proceedToURL = function() { (( ) => {
window.location.replace(details.url); const matches = /^(.*)\{\{hostname\}\}(.*)$/.exec(vAPI.i18n('docblockedProceed'));
}; if ( matches === null ) { return; }
const proceed = uDom('#templates .proceed').clone();
/******************************************************************************/
var proceedTemporary = function() {
messaging.send(
'documentBlocked',
{
what: 'temporarilyWhitelistDocument',
hostname: getTargetHostname()
},
proceedToURL
);
};
/******************************************************************************/
var proceedPermanent = function() {
messaging.send(
'documentBlocked',
{
what: 'toggleHostnameSwitch',
name: 'no-strict-blocking',
hostname: getTargetHostname(),
deep: true,
state: true,
persist: true
},
proceedToURL
);
};
/******************************************************************************/
(function() {
var matches = /^(.*)\{\{hostname\}\}(.*)$/.exec(vAPI.i18n('docblockedProceed'));
if ( matches === null ) {
return;
}
var proceed = uDom('#templates .proceed').clone();
proceed.descendants('span:nth-of-type(1)').text(matches[1]); proceed.descendants('span:nth-of-type(1)').text(matches[1]);
proceed.descendants('span:nth-of-type(4)').text(matches[2]); proceed.descendants('span:nth-of-type(4)').text(matches[2]);
@ -168,20 +112,18 @@ uDom.nodeFromId('why').textContent = details.fs;
// Parse URL to extract as much useful information as possible. This is useful // Parse URL to extract as much useful information as possible. This is useful
// to assist the user in deciding whether to navigate to the web page. // to assist the user in deciding whether to navigate to the web page.
(function() { (( ) => {
if ( typeof URL !== 'function' ) { if ( typeof URL !== 'function' ) { return; }
return;
}
var reURL = /^https?:\/\//; const reURL = /^https?:\/\//;
var liFromParam = function(name, value) { const liFromParam = function(name, value) {
if ( value === '' ) { if ( value === '' ) {
value = name; value = name;
name = ''; name = '';
} }
var li = document.createElement('li'); const li = document.createElement('li');
var span = document.createElement('span'); let span = document.createElement('span');
span.textContent = name; span.textContent = name;
li.appendChild(span); li.appendChild(span);
if ( name !== '' && value !== '' ) { if ( name !== '' && value !== '' ) {
@ -189,7 +131,7 @@ uDom.nodeFromId('why').textContent = details.fs;
} }
span = document.createElement('span'); span = document.createElement('span');
if ( reURL.test(value) ) { if ( reURL.test(value) ) {
var a = document.createElement('a'); const a = document.createElement('a');
a.href = a.textContent = value; a.href = a.textContent = value;
span.appendChild(a); span.appendChild(a);
} else { } else {
@ -199,7 +141,7 @@ uDom.nodeFromId('why').textContent = details.fs;
return li; return li;
}; };
var safeDecodeURIComponent = function(s) { const safeDecodeURIComponent = function(s) {
try { try {
s = decodeURIComponent(s); s = decodeURIComponent(s);
} catch (ex) { } catch (ex) {
@ -207,33 +149,29 @@ uDom.nodeFromId('why').textContent = details.fs;
return s; return s;
}; };
var renderParams = function(parentNode, rawURL) { const renderParams = function(parentNode, rawURL) {
var a = document.createElement('a'); const a = document.createElement('a');
a.href = rawURL; a.href = rawURL;
if ( a.search.length === 0 ) { if ( a.search.length === 0 ) { return false; }
return false;
}
var pos = rawURL.indexOf('?'); let pos = rawURL.indexOf('?');
var li = liFromParam( const li = liFromParam(
vAPI.i18n('docblockedNoParamsPrompt'), vAPI.i18n('docblockedNoParamsPrompt'),
rawURL.slice(0, pos) rawURL.slice(0, pos)
); );
parentNode.appendChild(li); parentNode.appendChild(li);
var params = a.search.slice(1).split('&'); const params = a.search.slice(1).split('&');
var param, name, value, ul; for ( const param of params ) {
for ( var i = 0; i < params.length; i++ ) { let pos = param.indexOf('=');
param = params[i];
pos = param.indexOf('=');
if ( pos === -1 ) { if ( pos === -1 ) {
pos = param.length; pos = param.length;
} }
name = safeDecodeURIComponent(param.slice(0, pos)); const name = safeDecodeURIComponent(param.slice(0, pos));
value = safeDecodeURIComponent(param.slice(pos + 1)); const value = safeDecodeURIComponent(param.slice(pos + 1));
li = liFromParam(name, value); const li = liFromParam(name, value);
if ( reURL.test(value) ) { if ( reURL.test(value) ) {
ul = document.createElement('ul'); const ul = document.createElement('ul');
renderParams(ul, value); renderParams(ul, value);
li.appendChild(ul); li.appendChild(ul);
} }
@ -246,12 +184,12 @@ uDom.nodeFromId('why').textContent = details.fs;
return; return;
} }
var toggler = document.createElement('span'); const toggler = document.createElement('span');
toggler.className = 'fa'; toggler.className = 'fa';
uDom('#theURL > p').append(toggler); uDom('#theURL > p').append(toggler);
uDom(toggler).on('click', function() { uDom(toggler).on('click', function() {
var cl = uDom.nodeFromId('theURL').classList; const cl = uDom.nodeFromId('theURL').classList;
cl.toggle('collapsed'); cl.toggle('collapsed');
vAPI.localStorage.setItem( vAPI.localStorage.setItem(
'document-blocked-expand-url', 'document-blocked-expand-url',
@ -267,14 +205,67 @@ uDom.nodeFromId('why').textContent = details.fs;
/******************************************************************************/ /******************************************************************************/
// https://www.reddit.com/r/uBlockOrigin/comments/breeux/close_this_window_doesnt_work_on_firefox/
if ( window.history.length > 1 ) { if ( window.history.length > 1 ) {
uDom('#back').on('click', function() { window.history.back(); }); uDom('#back').on(
'click',
( ) => {
window.history.back();
}
);
uDom('#bye').css('display', 'none'); uDom('#bye').css('display', 'none');
} else { } else {
uDom('#bye').on('click', function() { window.close(); }); uDom('#bye').on(
'click',
( ) => {
messaging.send(
'documentBlocked',
{ what: 'closeThisTab', }
);
}
);
uDom('#back').css('display', 'none'); uDom('#back').css('display', 'none');
} }
/******************************************************************************/
const getTargetHostname = function() {
const elem = document.querySelector('#proceed select');
if ( elem === null ) { return details.hn; }
return elem.value;
};
const proceedToURL = function() {
window.location.replace(details.url);
};
const proceedTemporary = function() {
messaging.send(
'documentBlocked',
{
what: 'temporarilyWhitelistDocument',
hostname: getTargetHostname()
},
proceedToURL
);
};
const proceedPermanent = function() {
messaging.send(
'documentBlocked',
{
what: 'toggleHostnameSwitch',
name: 'no-strict-blocking',
hostname: getTargetHostname(),
deep: true,
state: true,
persist: true
},
proceedToURL
);
};
uDom('#proceedTemporary').attr('href', details.url).on('click', proceedTemporary); uDom('#proceedTemporary').attr('href', details.url).on('click', proceedTemporary);
uDom('#proceedPermanent').attr('href', details.url).on('click', proceedPermanent); uDom('#proceedPermanent').attr('href', details.url).on('click', proceedPermanent);

View File

@ -1273,11 +1273,13 @@ vAPI.messaging.listen('loggerUI', onMessage);
// channel: documentBlocked // channel: documentBlocked
(function() { (( ) => {
/******************************************************************************/ /******************************************************************************/
var onMessage = function(request, sender, callback) { const onMessage = function(request, sender, callback) {
const tabId = sender && sender.tab ? sender.tab.id : 0;
// Async // Async
switch ( request.what ) { switch ( request.what ) {
default: default:
@ -1288,6 +1290,10 @@ var onMessage = function(request, sender, callback) {
let response; let response;
switch ( request.what ) { switch ( request.what ) {
case 'closeThisTab':
vAPI.tabs.remove(tabId);
break;
case 'temporarilyWhitelistDocument': case 'temporarilyWhitelistDocument':
µBlock.webRequest.strictBlockBypass(request.hostname); µBlock.webRequest.strictBlockBypass(request.hostname);
break; break;