refactor(clickTtHttpPageRoutes): optimize proxy navigation script for improved readability and performance

- Refactored the proxy navigation script by restructuring the code into a more concise format using an array to build the script content, enhancing readability.
- Maintained the core functionality while ensuring compatibility with existing event handling and URL management logic.
This commit is contained in:
Torsten Schulz (local)
2026-03-10 23:02:46 +01:00
parent 79ce79db8c
commit df95753f4d

View File

@@ -162,75 +162,61 @@ function rewriteFormActionsInHtml(html, proxyBaseUrl, pageBaseUrl, sid) {
function injectProxyNavigationScript(html, proxyBaseUrl, pageBaseUrl, sid) {
if (!html || !proxyBaseUrl || !pageBaseUrl) return html;
const script = `
<script>
(function () {
const PROXY_BASE_URL = ${JSON.stringify(proxyBaseUrl)};
const PAGE_BASE_URL = ${JSON.stringify(pageBaseUrl)};
const SID = ${JSON.stringify(sid || '')};
function normalizeUrl(value, base) {
if (!value) return null;
const trimmed = String(value).trim();
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('javascript:')) return null;
try {
return new URL(trimmed, base || PAGE_BASE_URL).href;
} catch {
return null;
}
}
function shouldProxyUrl(value) {
return /(^|\\.)click-tt\\.de$|(^|\\.)httv\\.de$/i.test(new URL(value).hostname);
}
function buildProxyUrl(targetUrl) {
const separator = PROXY_BASE_URL.includes('?') ? '&' : '?';
return PROXY_BASE_URL + separator + 'url=' + encodeURIComponent(targetUrl) + (SID ? '&sid=' + encodeURIComponent(SID) : '');
}
function navigateViaProxy(targetUrl) {
if (!targetUrl) return false;
if (!shouldProxyUrl(targetUrl)) return false;
window.location.assign(buildProxyUrl(targetUrl));
return true;
}
function getSubmitTarget(form, submitter) {
const action = submitter && submitter.getAttribute && submitter.getAttribute('formaction');
if (action) return normalizeUrl(action, PAGE_BASE_URL);
return normalizeUrl(form.getAttribute('action') || PAGE_BASE_URL, PAGE_BASE_URL);
}
document.addEventListener('click', function (event) {
const anchor = event.target && event.target.closest ? event.target.closest('a[href]') : null;
if (!anchor) return;
if (event.defaultPrevented) return;
const targetUrl = normalizeUrl(anchor.getAttribute('href'), PAGE_BASE_URL);
if (!targetUrl || !shouldProxyUrl(targetUrl)) return;
event.preventDefault();
navigateViaProxy(targetUrl);
}, false);
document.addEventListener('submit', function (event) {
const form = event.target;
if (!form || !form.tagName || form.tagName.toLowerCase() !== 'form') return;
const submitter = event.submitter || null;
const targetUrl = getSubmitTarget(form, submitter);
if (!targetUrl || !shouldProxyUrl(targetUrl)) return;
form.setAttribute('action', buildProxyUrl(targetUrl));
}, true);
const nativeSubmit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = function patchedSubmit() {
const targetUrl = getSubmitTarget(this, null);
if (targetUrl && shouldProxyUrl(targetUrl)) {
this.setAttribute('action', buildProxyUrl(targetUrl));
}
return nativeSubmit.apply(this, arguments);
};
})();
</script>`;
const scriptContent = [
'(function(){',
`var PROXY_BASE_URL=${JSON.stringify(proxyBaseUrl)};`,
`var PAGE_BASE_URL=${JSON.stringify(pageBaseUrl)};`,
`var SID=${JSON.stringify(sid || '')};`,
'function normalizeUrl(value, base){',
'if(!value)return null;',
"var trimmed=String(value).trim();",
"if(!trimmed||trimmed.indexOf('#')===0||trimmed.indexOf('javascript:')===0)return null;",
'try{return new URL(trimmed, base||PAGE_BASE_URL).href;}catch(e){return null;}',
'}',
'function shouldProxyUrl(value){',
'try{var hostname=new URL(value).hostname;return /(^|\\\\.)click-tt\\\\.de$|(^|\\\\.)httv\\\\.de$/i.test(hostname);}catch(e){return false;}',
'}',
'function buildProxyUrl(targetUrl){',
"var separator=PROXY_BASE_URL.indexOf('?')>=0?'&':'?';",
"return PROXY_BASE_URL+separator+'url='+encodeURIComponent(targetUrl)+(SID?'&sid='+encodeURIComponent(SID):'');",
'}',
'function navigateViaProxy(targetUrl){',
'if(!targetUrl||!shouldProxyUrl(targetUrl))return false;',
'window.location.assign(buildProxyUrl(targetUrl));',
'return true;',
'}',
'function getSubmitTarget(form, submitter){',
"var action=submitter&&submitter.getAttribute?submitter.getAttribute('formaction'):null;",
'if(action)return normalizeUrl(action,PAGE_BASE_URL);',
"return normalizeUrl((form&&form.getAttribute?form.getAttribute('action'):null)||PAGE_BASE_URL,PAGE_BASE_URL);",
'}',
"document.addEventListener('click',function(event){",
"var anchor=event.target&&event.target.closest?event.target.closest('a[href]'):null;",
'if(!anchor||event.defaultPrevented)return;',
"var targetUrl=normalizeUrl(anchor.getAttribute('href'),PAGE_BASE_URL);",
'if(!targetUrl||!shouldProxyUrl(targetUrl))return;',
'event.preventDefault();',
'navigateViaProxy(targetUrl);',
'},false);',
"document.addEventListener('submit',function(event){",
'var form=event.target;',
"if(!form||!form.tagName||form.tagName.toLowerCase()!=='form')return;",
'var submitter=event.submitter||null;',
'var targetUrl=getSubmitTarget(form,submitter);',
'if(!targetUrl||!shouldProxyUrl(targetUrl))return;',
"form.setAttribute('action',buildProxyUrl(targetUrl));",
'},true);',
'if(window.HTMLFormElement&&window.HTMLFormElement.prototype&&window.HTMLFormElement.prototype.submit){',
'var nativeSubmit=window.HTMLFormElement.prototype.submit;',
'window.HTMLFormElement.prototype.submit=function patchedSubmit(){',
'var targetUrl=getSubmitTarget(this,null);',
"if(targetUrl&&shouldProxyUrl(targetUrl)){this.setAttribute('action',buildProxyUrl(targetUrl));}",
'return nativeSubmit.apply(this,arguments);',
'};',
'}',
'})();',
].join('');
const script = `<script>${scriptContent}<\/script>`;
if (/<\/body>/i.test(html)) {
return html.replace(/<\/body>/i, `${script}</body>`);