Bug 263817

Summary: Safari Web Extension content scripts (and scripting.executeScript) won't inject into about:srcdoc iframes
Product: WebKit Reporter: Guilherme Rizzo <guivr2011>
Component: WebKit ExtensionsAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: jberlin, kiara_rose, opendarwin, timothy, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Safari 16   
Hardware: Mac (Apple Silicon)   
OS: macOS 13   

Description Guilherme Rizzo 2023-10-27 19:04:26 PDT
I was trying to convert my extension to Safari Web Extension, but there's a major problem.

Setting a content script like this in the manifest.json:

"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "match_about_blank": true,
      "match_origin_as_fallback": true,
      "include_globs": true,
      "all_frames": true,
      "js": ["main.js"]
    }
  ],

Won't inject the content script into iframes without a standard URL, for example, about:srcdoc as the URL.

Example page: https://tailwindui.com/components/application-ui/forms/form-layouts
This page has iframes that for Safari are "about:srcdoc" and the content script won't get injected into them.

I've tried adding/removing match_about_blank, match_origin_as_fallback, include_globs, all_frames, nothing worked.

The same happens if you try to inject it with browser.scripting.executeScript:
browser.scripting.executeScript({
      files: ['main.js'],
      target: {
        tabId: tab.id,
        allFrames: true,
      },
    })

This won't inject the script into about:srcdoc iframes. I've also tried directly specifying the frameIds and it still doesn't work.
Comment 1 Radar WebKit Bug Importer 2023-10-27 19:04:36 PDT
<rdar://problem/117613804>
Comment 2 Guilherme Rizzo 2023-10-27 20:31:26 PDT
PS: My host_permissions is "<all_urls>"
Comment 3 Jeff Johnson 2024-01-29 19:04:25 PST
This also applies to about:blank iframes. For the sake of searchability, can about:blank be added to the bug title?
Comment 4 Jeff Johnson 2024-01-29 19:13:17 PST
Here's an example of a page that causes major problems for my Safari web extension on iPhone:

https://www.barstoolsports.com/blog/3501810/dave-portnoy-has-cancelled-barstool-radio-until-further-notice-after-their-refusal-to-talk-about-barstool-drama-today

My extension has a number of video features, but the features don't work on this page, because there's a video in iframe src="about:blank" role="caffeine-embed", where my content script doesn't run. (Apparently the iframe is added to the page by setting the innerHTML of a parent element.)