From df95753f4d44b44c02b5d83e6eb02a09389f43c0 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Tue, 10 Mar 2026 23:02:46 +0100 Subject: [PATCH] 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. --- backend/routes/clickTtHttpPageRoutes.js | 124 +++++++++++------------- 1 file changed, 55 insertions(+), 69 deletions(-) diff --git a/backend/routes/clickTtHttpPageRoutes.js b/backend/routes/clickTtHttpPageRoutes.js index 45d18fdf..619a4842 100644 --- a/backend/routes/clickTtHttpPageRoutes.js +++ b/backend/routes/clickTtHttpPageRoutes.js @@ -162,75 +162,61 @@ function rewriteFormActionsInHtml(html, proxyBaseUrl, pageBaseUrl, sid) { function injectProxyNavigationScript(html, proxyBaseUrl, pageBaseUrl, sid) { if (!html || !proxyBaseUrl || !pageBaseUrl) return html; - const 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 = `