1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Modernize code: URLSearchParams() API support is now widespread

This commit is contained in:
Raymond Hill 2021-07-25 07:08:03 -04:00
parent bf7b0702e4
commit 89064478dd
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -104,9 +104,8 @@ uDom.nodeFromId('why').textContent = details.fs;
/******************************************************************************/ /******************************************************************************/
// https://github.com/gorhill/uBlock/issues/691 // https://github.com/gorhill/uBlock/issues/691
// 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
// to assist the user in deciding whether to navigate to the web page. // useful to assist the user in deciding whether to navigate to the web page.
(( ) => { (( ) => {
if ( typeof URL !== 'function' ) { return; } if ( typeof URL !== 'function' ) { return; }
@ -136,36 +135,25 @@ uDom.nodeFromId('why').textContent = details.fs;
return li; return li;
}; };
const safeDecodeURIComponent = function(s) {
try {
s = decodeURIComponent(s);
} catch (ex) {
}
return s;
};
// https://github.com/uBlockOrigin/uBlock-issues/issues/1649 // https://github.com/uBlockOrigin/uBlock-issues/issues/1649
// Limit recursion. // Limit recursion.
const renderParams = function(parentNode, rawURL, depth = 0) { const renderParams = function(parentNode, rawURL, depth = 0) {
const a = document.createElement('a'); let url;
a.href = rawURL; try {
if ( a.search.length === 0 ) { return false; } url = new URL(rawURL);
} catch(ex) {
return false;
}
let pos = rawURL.indexOf('?'); const search = url.search.slice(1);
const li = liFromParam( if ( search === '' ) { return false; }
vAPI.i18n('docblockedNoParamsPrompt'),
rawURL.slice(0, pos) url.search = '';
); const li = liFromParam(vAPI.i18n('docblockedNoParamsPrompt'), url.href);
parentNode.appendChild(li); parentNode.appendChild(li);
const params = a.search.slice(1).split('&'); const params = new self.URLSearchParams(search);
for ( const param of params ) { for ( const [ name, value ] of params ) {
let pos = param.indexOf('=');
if ( pos === -1 ) {
pos = param.length;
}
const name = safeDecodeURIComponent(param.slice(0, pos));
const value = safeDecodeURIComponent(param.slice(pos + 1));
const li = liFromParam(name, value); const li = liFromParam(name, value);
if ( depth < 2 && reURL.test(value) ) { if ( depth < 2 && reURL.test(value) ) {
const ul = document.createElement('ul'); const ul = document.createElement('ul');
@ -174,6 +162,7 @@ uDom.nodeFromId('why').textContent = details.fs;
} }
parentNode.appendChild(li); parentNode.appendChild(li);
} }
return true; return true;
}; };