fix: Add error handling for script injection and improve logging for URL mappings
This commit is contained in:
parent
0eba82b8af
commit
4011f46ab7
2 changed files with 69 additions and 36 deletions
|
|
@ -2,6 +2,7 @@
|
|||
// Prevents browser drag for ALT+drag, hands off to Qt for file drag
|
||||
|
||||
(function() {
|
||||
try {
|
||||
if (window.__webdrop_intercept_injected) return;
|
||||
window.__webdrop_intercept_injected = true;
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
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
|
||||
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++) {
|
||||
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] Mapping', (i+1) + ':', m.url_prefix, '→', m.local_path);
|
||||
}
|
||||
})();
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue