From 4011f46ab75b2dc215c2303ea76cc03e12e13963 Mon Sep 17 00:00:00 2001 From: claudi Date: Wed, 18 Feb 2026 10:28:15 +0100 Subject: [PATCH] fix: Add error handling for script injection and improve logging for URL mappings --- .../ui/bridge_script_intercept.js | 47 ++++++++------- src/webdrop_bridge/ui/main_window.py | 58 ++++++++++++++----- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/webdrop_bridge/ui/bridge_script_intercept.js b/src/webdrop_bridge/ui/bridge_script_intercept.js index 83fa803..b3c2f52 100644 --- a/src/webdrop_bridge/ui/bridge_script_intercept.js +++ b/src/webdrop_bridge/ui/bridge_script_intercept.js @@ -2,29 +2,30 @@ // Prevents browser drag for ALT+drag, hands off to Qt for file drag (function() { - if (window.__webdrop_intercept_injected) return; - window.__webdrop_intercept_injected = true; + try { + if (window.__webdrop_intercept_injected) return; + window.__webdrop_intercept_injected = true; - // Intercept mode enabled - var INTERCEPT_ENABLED = true; + // Intercept mode enabled + var INTERCEPT_ENABLED = true; - console.log('%c[WebDrop Intercept] Script loaded - INTERCEPT_ENABLED=' + INTERCEPT_ENABLED, 'background: #2196F3; color: white; font-weight: bold; padding: 4px 8px;'); - - var currentDragUrl = null; - var angularDragHandlers = []; - var originalAddEventListener = EventTarget.prototype.addEventListener; - var listenerPatchActive = true; - - // Capture Authorization token from XHR requests - window.capturedAuthToken = null; - var originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; - XMLHttpRequest.prototype.setRequestHeader = function(header, value) { - if (header === 'Authorization' && value.startsWith('Bearer ')) { - window.capturedAuthToken = value; - console.log('[Intercept] Captured auth token'); - } - return originalXHRSetRequestHeader.apply(this, arguments); - }; + console.log('%c[WebDrop Intercept] Script loaded - INTERCEPT_ENABLED=' + INTERCEPT_ENABLED, 'background: #2196F3; color: white; font-weight: bold; padding: 4px 8px;'); + + var currentDragUrl = null; + var angularDragHandlers = []; + var originalAddEventListener = EventTarget.prototype.addEventListener; + var listenerPatchActive = true; + + // Capture Authorization token from XHR requests + window.capturedAuthToken = null; + var originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; + XMLHttpRequest.prototype.setRequestHeader = function(header, value) { + if (header === 'Authorization' && value.startsWith('Bearer ')) { + window.capturedAuthToken = value; + console.log('[Intercept] Captured auth token'); + } + return originalXHRSetRequestHeader.apply(this, arguments); + }; // ============================================================================ // PART 1: Intercept Angular's dragstart listener registration @@ -189,4 +190,8 @@ console.log('%c[WebDrop Intercept] Ready! ALT-drag will use Qt file drag.', 'background: #4CAF50; color: white; font-weight: bold; padding: 4px 8px;'); + } catch(e) { + console.error('[WebDrop Intercept] FATAL ERROR in bridge script:', e); + console.error('[WebDrop Intercept] Stack:', e.stack); + } })(); diff --git a/src/webdrop_bridge/ui/main_window.py b/src/webdrop_bridge/ui/main_window.py index be7f17e..4099a2c 100644 --- a/src/webdrop_bridge/ui/main_window.py +++ b/src/webdrop_bridge/ui/main_window.py @@ -422,7 +422,7 @@ class MainWindow(QMainWindow): qwebchannel_code = "" qwebchannel_file = QFile(":/qtwebchannel/qwebchannel.js") if qwebchannel_file.open(QIODevice.OpenModeFlag.ReadOnly | QIODevice.OpenModeFlag.Text): - qwebchannel_code = bytes(qwebchannel_file.readAll()).decode('utf-8') + qwebchannel_code = bytes(qwebchannel_file.readAll()).decode('utf-8') # type: ignore qwebchannel_file.close() logger.debug("Loaded qwebchannel.js inline to avoid CSP issues") else: @@ -454,6 +454,15 @@ class MainWindow(QMainWindow): if download_interceptor_code: combined_code += "\n\n" + download_interceptor_code + logger.debug(f"Combined script size: {len(combined_code)} chars " + f"(qwebchannel: {len(qwebchannel_code)}, " + f"config: {len(config_code)}, " + f"bridge: {len(bridge_code)}, " + f"interceptor: {len(download_interceptor_code)})") + logger.debug(f"URL mappings in config: {len(self.config.url_mappings)}") + for i, mapping in enumerate(self.config.url_mappings): + logger.debug(f" Mapping {i+1}: {mapping.url_prefix} → {mapping.local_path}") + script.setSourceCode(combined_code) self.web_view.page().scripts().insert(script) logger.debug(f"Installed bridge script from {script_path}") @@ -478,20 +487,39 @@ class MainWindow(QMainWindow): "local_path": mapping.local_path }) - # Generate JavaScript code - config_js = """ -(function() { - // WebDrop Bridge - Configuration Injection - window.webdropConfig = """ + json.dumps({ - "urlMappings": mappings - }) + """; - console.log('[WebDrop Config] Injected configuration with', - window.webdropConfig.urlMappings.length, 'URL mappings'); - for (var i = 0; i < window.webdropConfig.urlMappings.length; i++) { - var m = window.webdropConfig.urlMappings[i]; - console.log('[WebDrop Config] Mapping', (i+1) + ':', m.url_prefix, '→', m.local_path); - } -})(); + logger.debug(f"Generating config injection with {len(mappings)} URL mappings") + for i, m in enumerate(mappings): + logger.debug(f" [{i+1}] {m['url_prefix']} -> {m['local_path']}") + + # Generate config object as JSON + config_obj = {"urlMappings": mappings} + config_json = json.dumps(config_obj) + + logger.debug(f"Config JSON size: {len(config_json)} bytes") + + # Generate JavaScript code - Safe injection with error handling + config_js = f""" +(function() {{ + try {{ + // WebDrop Bridge - Configuration Injection + console.log('[WebDrop Config] Starting configuration injection...'); + window.webdropConfig = {config_json}; + console.log('[WebDrop Config] Configuration object created'); + + if (window.webdropConfig && window.webdropConfig.urlMappings) {{ + console.log('[WebDrop Config] SUCCESS: ' + window.webdropConfig.urlMappings.length + ' URL mappings loaded'); + for (var i = 0; i < window.webdropConfig.urlMappings.length; i++) {{ + var m = window.webdropConfig.urlMappings[i]; + console.log('[WebDrop Config] [' + (i+1) + '] ' + m.url_prefix + ' -> ' + m.local_path); + }} + }} else {{ + console.warn('[WebDrop Config] WARNING: No valid URL mappings found in config object'); + }} + }} catch(e) {{ + console.error('[WebDrop Config] ERROR during configuration injection: ' + e.message); + if (e.stack) console.error('[WebDrop Config] Stack: ' + e.stack); + }} +}})(); """ return config_js