uBlock/src/js/document-blocked.js

286 lines
8.7 KiB
JavaScript
Raw Normal View History

2015-03-26 00:28:22 +01:00
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015-present Raymond Hill
2015-03-26 00:28:22 +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
*/
/* global uDom */
2018-07-22 14:14:50 +02:00
'use strict';
2015-03-26 00:28:22 +01:00
/******************************************************************************/
(function() {
/******************************************************************************/
2016-03-06 16:51:06 +01:00
var messaging = vAPI.messaging;
2015-03-30 19:10:29 +02:00
var details = {};
2015-03-26 00:28:22 +01:00
2015-03-30 19:10:29 +02:00
(function() {
var matches = /details=([^&]+)/.exec(window.location.search);
if ( matches === null ) {
return;
}
details = JSON.parse(atob(matches[1]));
})();
2015-03-26 00:28:22 +01:00
/******************************************************************************/
2015-06-12 01:33:30 +02:00
(function() {
var onReponseReady = function(response) {
2018-07-22 14:14:50 +02:00
if ( response instanceof Object === false ) { return; }
let lists;
for ( let rawFilter in response ) {
if ( response.hasOwnProperty(rawFilter) === false ) { continue; }
lists = response[rawFilter];
break;
}
2015-06-12 01:33:30 +02:00
if ( Array.isArray(lists) === false || lists.length === 0 ) {
return;
}
2018-07-22 14:14:50 +02:00
let parent = uDom.nodeFromSelector('#whyex > span:nth-of-type(2)');
2018-07-22 14:14:50 +02:00
for ( let list of lists ) {
let elem = document.querySelector('#templates .filterList')
.cloneNode(true);
let source = elem.querySelector('.filterListSource');
2018-07-22 14:14:50 +02:00
source.href += encodeURIComponent(list.assetKey);
source.textContent = list.title;
if (
2018-07-22 14:14:50 +02:00
typeof list.supportURL === 'string' &&
list.supportURL !== ''
) {
elem.querySelector('.filterListSupport')
2018-07-22 14:14:50 +02:00
.setAttribute('href', list.supportURL);
2015-06-12 01:33:30 +02:00
}
parent.appendChild(elem);
2015-06-12 01:33:30 +02:00
}
uDom.nodeFromId('whyex').style.removeProperty('display');
};
2016-03-06 16:51:06 +01:00
messaging.send(
'documentBlocked',
{
what: 'listsFromNetFilter',
compiledFilter: details.fc,
rawFilter: details.fs
},
onReponseReady
);
2015-06-12 01:33:30 +02:00
})();
/******************************************************************************/
var getTargetHostname = function() {
var hostname = details.hn;
var elem = document.querySelector('#proceed select');
if ( elem !== null ) {
hostname = elem.value;
}
return hostname;
};
/******************************************************************************/
2015-03-30 19:10:29 +02:00
var proceedToURL = function() {
window.location.replace(details.url);
};
/******************************************************************************/
2015-03-26 00:28:22 +01:00
2015-03-30 19:10:29 +02:00
var proceedTemporary = function() {
2016-03-06 16:51:06 +01:00
messaging.send(
'documentBlocked',
{
what: 'temporarilyWhitelistDocument',
hostname: getTargetHostname()
},
proceedToURL
);
2015-03-30 19:10:29 +02:00
};
2015-03-26 00:28:22 +01:00
2015-03-30 19:10:29 +02:00
/******************************************************************************/
var proceedPermanent = function() {
2016-03-06 16:51:06 +01:00
messaging.send(
'documentBlocked',
{
what: 'toggleHostnameSwitch',
name: 'no-strict-blocking',
hostname: getTargetHostname(),
deep: true,
2018-09-03 20:06:49 +02:00
state: true,
persist: true
2016-03-06 16:51:06 +01:00
},
proceedToURL
);
2015-03-26 00:28:22 +01:00
};
/******************************************************************************/
2015-03-30 19:10:29 +02:00
(function() {
var matches = /^(.*)\{\{hostname\}\}(.*)$/.exec(vAPI.i18n('docblockedProceed'));
if ( matches === null ) {
return;
}
var proceed = uDom('#templates .proceed').clone();
2015-03-30 19:10:29 +02:00
proceed.descendants('span:nth-of-type(1)').text(matches[1]);
proceed.descendants('span:nth-of-type(4)').text(matches[2]);
if ( details.hn === details.dn ) {
proceed.descendants('span:nth-of-type(2)').remove();
2015-04-06 18:22:41 +02:00
proceed.descendants('.hn').text(details.hn);
} else {
2015-04-06 18:22:41 +02:00
proceed.descendants('span:nth-of-type(3)').remove();
proceed.descendants('.hn').text(details.hn).attr('value', details.hn);
proceed.descendants('.dn').text(details.dn).attr('value', details.dn);
}
2015-03-30 19:10:29 +02:00
uDom('#proceed').append(proceed);
})();
/******************************************************************************/
2015-09-10 15:51:49 +02:00
uDom.nodeFromSelector('#theURL > p').textContent = details.url;
2015-06-12 01:33:30 +02:00
uDom.nodeFromId('why').textContent = details.fs;
2015-03-26 00:28:22 +01:00
2015-09-10 15:51:49 +02:00
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/691
// 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.
(function() {
if ( typeof URL !== 'function' ) {
return;
}
var reURL = /^https?:\/\//;
var liFromParam = function(name, value) {
2015-09-14 13:21:35 +02:00
if ( value === '' ) {
value = name;
name = '';
}
2015-09-10 15:51:49 +02:00
var li = document.createElement('li');
var span = document.createElement('span');
span.textContent = name;
li.appendChild(span);
2015-09-14 13:21:35 +02:00
if ( name !== '' && value !== '' ) {
li.appendChild(document.createTextNode(' = '));
}
2015-09-10 15:51:49 +02:00
span = document.createElement('span');
if ( reURL.test(value) ) {
var a = document.createElement('a');
a.href = a.textContent = value;
span.appendChild(a);
} else {
span.textContent = value;
}
li.appendChild(span);
return li;
};
2016-02-04 14:21:59 +01:00
var safeDecodeURIComponent = function(s) {
try {
s = decodeURIComponent(s);
} catch (ex) {
}
return s;
};
2015-09-10 15:51:49 +02:00
var renderParams = function(parentNode, rawURL) {
2015-10-13 16:56:59 +02:00
var a = document.createElement('a');
a.href = rawURL;
if ( a.search.length === 0 ) {
2015-09-10 15:51:49 +02:00
return false;
}
2015-09-12 16:51:11 +02:00
var pos = rawURL.indexOf('?');
var li = liFromParam(
vAPI.i18n('docblockedNoParamsPrompt'),
rawURL.slice(0, pos)
);
parentNode.appendChild(li);
2015-10-13 16:56:59 +02:00
var params = a.search.slice(1).split('&');
2015-09-12 16:51:11 +02:00
var param, name, value, ul;
2015-09-10 15:51:49 +02:00
for ( var i = 0; i < params.length; i++ ) {
param = params[i];
pos = param.indexOf('=');
if ( pos === -1 ) {
pos = param.length;
}
2016-02-04 14:21:59 +01:00
name = safeDecodeURIComponent(param.slice(0, pos));
value = safeDecodeURIComponent(param.slice(pos + 1));
2015-09-10 15:51:49 +02:00
li = liFromParam(name, value);
if ( reURL.test(value) ) {
ul = document.createElement('ul');
renderParams(ul, value);
li.appendChild(ul);
}
parentNode.appendChild(li);
}
return true;
};
if ( renderParams(uDom.nodeFromId('parsed'), details.url) === false ) {
return;
}
var toggler = document.createElement('span');
toggler.className = 'fa';
uDom('#theURL > p').append(toggler);
uDom(toggler).on('click', function() {
2015-09-12 16:51:11 +02:00
var cl = uDom.nodeFromId('theURL').classList;
cl.toggle('collapsed');
vAPI.localStorage.setItem(
'document-blocked-expand-url',
(cl.contains('collapsed') === false).toString()
);
2015-09-10 15:51:49 +02:00
});
2015-09-12 16:51:11 +02:00
uDom.nodeFromId('theURL').classList.toggle(
'collapsed',
vAPI.localStorage.getItem('document-blocked-expand-url') !== 'true'
);
2015-09-10 15:51:49 +02:00
})();
/******************************************************************************/
2015-03-26 00:28:22 +01:00
if ( window.history.length > 1 ) {
uDom('#back').on('click', function() { window.history.back(); });
2015-03-26 13:12:06 +01:00
uDom('#bye').css('display', 'none');
2015-03-26 00:28:22 +01:00
} else {
2015-03-26 13:12:06 +01:00
uDom('#bye').on('click', function() { window.close(); });
2015-03-26 00:28:22 +01:00
uDom('#back').css('display', 'none');
}
2015-03-30 19:10:29 +02:00
uDom('#proceedTemporary').attr('href', details.url).on('click', proceedTemporary);
uDom('#proceedPermanent').attr('href', details.url).on('click', proceedPermanent);
2015-03-26 00:28:22 +01:00
2015-03-30 23:42:12 +02:00
/******************************************************************************/
2015-03-26 00:28:22 +01:00
})();
/******************************************************************************/