feat: Enhance drag interception with dynamic URL pattern matching configuration injection
This commit is contained in:
parent
f385ee6410
commit
0eba82b8af
2 changed files with 62 additions and 9 deletions
|
|
@ -408,6 +408,7 @@ class MainWindow(QMainWindow):
|
|||
before any page scripts that might interfere with drag events.
|
||||
|
||||
Embeds qwebchannel.js inline to avoid CSP issues with qrc:// URLs.
|
||||
Injects configuration that bridge script uses for dynamic URL pattern matching.
|
||||
"""
|
||||
from PySide6.QtCore import QFile, QIODevice
|
||||
|
||||
|
|
@ -427,6 +428,9 @@ class MainWindow(QMainWindow):
|
|||
else:
|
||||
logger.warning("Failed to load qwebchannel.js from resources")
|
||||
|
||||
# Generate configuration injection script
|
||||
config_code = self._generate_config_injection_script()
|
||||
|
||||
# Load bridge script from file
|
||||
# Using intercept script - prevents browser drag, hands off to Qt
|
||||
script_path = Path(__file__).parent / "bridge_script_intercept.js"
|
||||
|
|
@ -444,11 +448,8 @@ class MainWindow(QMainWindow):
|
|||
except (OSError, IOError) as e:
|
||||
logger.warning(f"Download interceptor not found: {e}")
|
||||
|
||||
# Combine qwebchannel.js + bridge script + download interceptor (inline to avoid CSP)
|
||||
if qwebchannel_code:
|
||||
combined_code = qwebchannel_code + "\n\n" + bridge_code
|
||||
else:
|
||||
combined_code = bridge_code
|
||||
# Combine: qwebchannel.js + config + bridge script + download interceptor
|
||||
combined_code = qwebchannel_code + "\n\n" + config_code + "\n\n" + bridge_code
|
||||
|
||||
if download_interceptor_code:
|
||||
combined_code += "\n\n" + download_interceptor_code
|
||||
|
|
@ -459,6 +460,41 @@ class MainWindow(QMainWindow):
|
|||
except (OSError, IOError) as e:
|
||||
logger.warning(f"Failed to load bridge script: {e}")
|
||||
|
||||
def _generate_config_injection_script(self) -> str:
|
||||
"""Generate JavaScript code that injects configuration.
|
||||
|
||||
Creates a script that sets window.webdropConfig with the current
|
||||
URL mappings, allowing the bridge script to dynamically check
|
||||
against configured patterns instead of hardcoded values.
|
||||
|
||||
Returns:
|
||||
JavaScript code as string
|
||||
"""
|
||||
# Convert URL mappings to format expected by bridge script
|
||||
mappings = []
|
||||
for mapping in self.config.url_mappings:
|
||||
mappings.append({
|
||||
"url_prefix": mapping.url_prefix,
|
||||
"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);
|
||||
}
|
||||
})();
|
||||
"""
|
||||
return config_js
|
||||
|
||||
def _inject_drag_bridge(self, html_content: str) -> str:
|
||||
"""Return HTML content unmodified.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue