Bug 263819

Summary: tabs.sendMessage not getting any response back if script wasn't injected
Product: WebKit Reporter: Guilherme Rizzo <guivr2011>
Component: WebKit ExtensionsAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: karlcow, mike, rz.rustyzone, timothy, webkit-bug-importer
Priority: P2 Keywords: BrowserCompat, InRadar
Version: Safari 16   
Hardware: Mac (Apple Silicon)   
OS: macOS 13   

Description Guilherme Rizzo 2023-10-27 19:16:57 PDT
If you try to run:

browser.tabs.sendMessage(tab.id, { text: "are_you_there_content_script?" },
      function (msg) {
        msg = msg || {}
        if (msg.status !== 'yes') {
          chrome.scripting.executeScript({
            ...
          })
        }
      }
    )

In Chrome and Firefox, the function gets called no matter the response.

In Safari, the function never runs because no script has been injected into the page so far.

Always getting a response back, even if empty, undefined, or null, would make checking if scripts were injected much easier.

This is useful to run executeScript only when asked for and only once, without managing a state with a variable by pushing and removing tabIds where it has run.

Otherwise, people will add content scripts that always run - much heavier than executing scripts only when requested.
Comment 1 Radar WebKit Bug Importer 2023-10-27 19:17:05 PDT
<rdar://problem/117614209>