Implement intelligent platform script injection system using chrome.scripting.executeScript #22
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This PR implements an intelligent script injection system that replaces the monolithic
src/contentstructure with dynamic platform-specific script injection using Chrome'schrome.scripting.executeScriptAPI, addressing the need for precise code separation while ensuring scripts have full Chrome API access.Problem Solved
The original codebase had a centralized content injection system where all platform logic was bundled together through
Uninject.ts. This created dependencies and made it difficult to maintain platform-specific functionality independently.Solution Overview
Intelligent Script Injection System
Implemented dynamic injection of four platform-specific scripts using
chrome.scripting.executeScript:content-discourse.js(6.28 kB) - Handles Discourse forums with batch image parsing and single image additioncontent-x.js(7.54 kB) - Manages X/Twitter image carousels and video link copyingcontent-pixiv.js(7.80 kB) - Processes Pixiv images with CORS handling and canvas downloadingcontent-reddit.js(5.84 kB) - Handles Reddit image containers with srcset optimizationKey Technical Features
Smart Content Script: The main content script (
content-smart.ts) performs platform detection and requests background script injection:Chrome API Injection: Background script uses
chrome.scripting.executeScriptfor dynamic injection with full API access:Full Chrome API Access: Platform scripts run in the
ISOLATEDworld with complete Chrome extension API access:Smart Platform Detection: Multi-layered detection using hostname, meta tags, and page elements:
Integrated Build System
Platform scripts are now integrated into the main vite build process:
npm run buildcommand builds all componentsBackground Script Integration
Extended the background script system with intelligent injection handlers:
INJECT_PLATFORM_SCRIPT- Useschrome.scripting.executeScriptfor dynamic injectionPLATFORM_DETECTED- Logs platform detection eventsAUTONOMOUS_SCRIPT_READY- Tracks script lifecyclePerformance Improvements
Testing & Validation
Comprehensive testing confirmed:
chrome.scripting.executeScriptFiles Added/Modified
Smart Injection System:
src/content/content-smart.ts- Smart platform detection and injection requestsrc/background/handlers/autonomousScripts.ts- Chrome API injection handlerssrc/background/utils/handlers.ts- Message routingPlatform Scripts (Chrome API Enabled):
src/autonomous-scripts/discourse-script.tssrc/autonomous-scripts/x-script.tssrc/autonomous-scripts/pixiv-script.tssrc/autonomous-scripts/reddit-script.tsBuild System Integration:
vite.config.ts- Integrated platform scripts as build entriespackage.json- Removed separate build commandscripts/build.js- Fixed build command for npm compatibilityDocumentation:
INTELLIGENT_INJECTION.md- Complete system documentationThis implementation achieves precise code separation through intelligent script injection using Chrome's standard APIs, enabling independent platform maintenance while ensuring scripts have full Chrome extension privileges and API access.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.