Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot read properties of undefined (reading 'contextMenus') #972

Open
3 tasks done
hhmy27 opened this issue May 9, 2024 · 3 comments
Open
3 tasks done

[BUG] Cannot read properties of undefined (reading 'contextMenus') #972

hhmy27 opened this issue May 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@hhmy27
Copy link

hhmy27 commented May 9, 2024

What happened?

// code at src/background/index.ts
import "@plasmohq/messaging/background"

export {}

console.log(
  "Live now; make now always the most precious time. Now will never come again.",
)

chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "openSidePanel",
    title: "Open side panel",
    contexts: ["all"]
  })
})

chrome.contextMenus.onClicked.addListener((info, tab) => {
  console.log("background open side panel")
  if (info.menuItemId === "openSidePanel") {
    chrome.sidePanel.open({ windowId: tab.windowId })
  }
})

I'm trying to add an "open side panel" button to the menu bar after the plugin is installed. When the user clicks on it, the side panel of Chrome will open.
This part of the code is consistent with Chrome's official documentation, but when I use it on Plasmo, it prompts me with Uncaught TypeError: Cannot read properties of undefined (reading 'onClicked'), which leaves me very confused. Does anyone know where the problem might be?

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

(OPTIONAL) Contribution

  • I would like to fix this BUG via a PR

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
@hhmy27 hhmy27 added the bug Something isn't working label May 9, 2024
@hhmy27
Copy link
Author

hhmy27 commented May 9, 2024

I found this section in the Plasmo documentation:
If you already have a background.ts or background.js file, you will have to create a background folder and move your script to background/index.ts or background/index.js.

This implies that our background.js must be stored under the background/ directory, but once I do this, I encounter this error:
Service worker registration failed. Status code: 15

This makes my service worker completely unusable, which is a serious issue for me, as it's almost impossible for me to continue developing. Can any developers of plasmo pay attention to this issue?

@hhmy27
Copy link
Author

hhmy27 commented May 9, 2024

A possible reason is: Manifest v3 requires our background.js/ts to be mounted under the root path to be recognized, but our generated code declares it like this:
"background": { "service_worker": "static/background/index.js" },
which leads to the service worker not being able to be mounted.

I have created a demo repo to help developers locate the bug: https://github.com/hhmy27/test-messaging

@AlexGodard
Copy link

@hhmy27 It seems like you're missing

    "permissions": [
      "contextMenus"
    ]

in your package.json. I tried adding it and it works after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants