PlasmoHQ / plasmo

🧩 The Browser Extension Framework
https://www.plasmo.com
MIT License
8.84k stars 314 forks source link

[BUG] When installing or updating a plugin, content will not be automatically injected into the opened tab page. #1002

Open duriann opened 3 weeks ago

duriann commented 3 weeks ago

What happened?

When installing or updating a plugin, content will not be automatically injected into the opened tab page.

Version

Latest

What OS are you seeing the problem on?

No response

What browsers are you seeing the problem on?

No response

Relevant log output

No response

(OPTIONAL) Contribution

Code of Conduct

rezamajidi commented 2 weeks ago

@duriann I also had this problem and currently using this workaround to fix that: Maybe it helps.

if (details.reason === "install" || details.reason === "update") {
    for (const cs of chrome.runtime.getManifest().content_scripts) {
      for (const tab of await chrome.tabs.query({ url: cs.matches })) {
        if (tab.url.match(/(chrome|chrome-extension):\/\//gi)) {
          continue
        }
        chrome.scripting.executeScript({
          files: cs.js,
          target: { tabId: tab.id, allFrames: cs.all_frames },
          injectImmediately: cs.run_at === "document_start"
        })
      }
    }
  }
duriann commented 2 weeks ago

@duriann I also had this problem and currently using this workaround to fix that: Maybe it helps.

if (details.reason === "install" || details.reason === "update") {
    for (const cs of chrome.runtime.getManifest().content_scripts) {
      for (const tab of await chrome.tabs.query({ url: cs.matches })) {
        if (tab.url.match(/(chrome|chrome-extension):\/\//gi)) {
          continue
        }
        chrome.scripting.executeScript({
          files: cs.js,
          target: { tabId: tab.id, allFrames: cs.all_frames },
          injectImmediately: cs.run_at === "document_start"
        })
      }
    }
  }

I did the same thing, but the plasmo-csui node will be added repeatedly on the injection page, and the console will keep prompting: Error: Extension context invalidated. This is because the old context is not uninstalled after injection. Do you have any solution?