Minor code review

This commit is contained in:
Raymond Hill 2023-01-07 12:52:46 -05:00
parent d40bd2aa3b
commit 789ee8d910
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 15 additions and 15 deletions

View File

@ -29,18 +29,18 @@
// https://github.com/gorhill/uMatrix/issues/232 // https://github.com/gorhill/uMatrix/issues/232
// Force `display` property, Firefox is still affected by the issue. // Force `display` property, Firefox is still affected by the issue.
(function() { (( ) => {
let noscripts = document.querySelectorAll('noscript'); const noscripts = document.querySelectorAll('noscript');
if ( noscripts.length === 0 ) { return; } if ( noscripts.length === 0 ) { return; }
let redirectTimer, const reMetaContent = /^\s*(\d+)\s*;\s*url=(['"]?)([^'"]+)\2/i;
reMetaContent = /^\s*(\d+)\s*;\s*url=(['"]?)([^'"]+)\2/i, const reSafeURL = /^https?:\/\//;
reSafeURL = /^https?:\/\//; let redirectTimer;
let autoRefresh = function(root) { const autoRefresh = function(root) {
let meta = root.querySelector('meta[http-equiv="refresh"][content]'); const meta = root.querySelector('meta[http-equiv="refresh"][content]');
if ( meta === null ) { return; } if ( meta === null ) { return; }
let match = reMetaContent.exec(meta.getAttribute('content')); const match = reMetaContent.exec(meta.getAttribute('content'));
if ( match === null || match[3].trim() === '' ) { return; } if ( match === null || match[3].trim() === '' ) { return; }
let url; let url;
@ -59,26 +59,26 @@
meta.parentNode.removeChild(meta); meta.parentNode.removeChild(meta);
}; };
let morphNoscript = function(from) { const morphNoscript = function(from) {
if ( /^application\/(?:xhtml\+)?xml/.test(document.contentType) ) { if ( /^application\/(?:xhtml\+)?xml/.test(document.contentType) ) {
let to = document.createElement('span'); const to = document.createElement('span');
while ( from.firstChild !== null ) { while ( from.firstChild !== null ) {
to.appendChild(from.firstChild); to.appendChild(from.firstChild);
} }
return to; return to;
} }
let parser = new DOMParser(); const parser = new DOMParser();
let doc = parser.parseFromString( const doc = parser.parseFromString(
'<span>' + from.textContent + '</span>', '<span>' + from.textContent + '</span>',
'text/html' 'text/html'
); );
return document.adoptNode(doc.querySelector('span')); return document.adoptNode(doc.querySelector('span'));
}; };
for ( let noscript of noscripts ) { for ( const noscript of noscripts ) {
let parent = noscript.parentNode; const parent = noscript.parentNode;
if ( parent === null ) { continue; } if ( parent === null ) { continue; }
let span = morphNoscript(noscript); const span = morphNoscript(noscript);
span.style.setProperty('display', 'inline', 'important'); span.style.setProperty('display', 'inline', 'important');
if ( redirectTimer === undefined ) { if ( redirectTimer === undefined ) {
autoRefresh(span); autoRefresh(span);