fix: early polyfill injection via onCommitted + CSP bypass#7
fix: early polyfill injection via onCommitted + CSP bypass#7madmath wants to merge 1 commit intoigrigorik:mainfrom
Conversation
…and bypass CSP The manifest content_scripts entry (world: MAIN, document_start) can lose the race against inline page scripts and may be wrapped as an ES module by CRXJS in dev mode. Additionally, injecting via an ISOLATED world loader with script.textContent is blocked by strict page CSP. Add a webNavigation.onCommitted handler that injects the polyfill programmatically via chrome.scripting.executeScript with files:[] and injectImmediately: true. This bypasses page CSP and runs at document_start-equivalent timing. The manifest entry is kept as a belt-and-suspenders fallback; the polyfill's own guard prevents double-initialization. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@madmath huh, do you have a ~reliable way to repro this? Intuitively, the strategy in this PR should be strictly worse.. According to Google docs content scripts declared via manifest are the first to be injected, and should be executing before any executeScript call, etc. Manifest scripts are injected by the browser engine synchronously with document creation vs. much laxer contract for |
|
You can repro with https://peroh.com, it has a script in the head which will look for window.navigator.modelContext and prior to this change, it's not available. Previous discussion online talks about this approach, which is probably why Claude picked it up. Let me know what you want to do! |
|
I can't reproduce this, the tools are coming up reliably here. Are you testing on local preview / staging? @madmath sanity check: are you using latest version from main? |
|
Annnnd... I can't reproduce. I was on latest (last commit Feb 17), not sure what happened. Glad it's fine now! |
|
\o/ |
Issue
I observed that the polyfill doesn't inject soon enough in the page lifecycle. Which doesn't guarantee tools would register.
Summary
webNavigation.onCommitted+chrome.scripting.executeScriptwithfiles:[]andinjectImmediately: true, guaranteeing it runs before page scripts and bypasses strict page CSPcontent_scriptsentry (world: MAIN, document_start) as a belt-and-suspenders fallback; the polyfill's own guard prevents double-initTest plan
navigator.modelContextis available in console<script>checking'modelContext' in navigatorat parse time), confirmavailable: true🤖 Generated with Claude Code