Customising the `browserAction.onClicked` behaviou...
# support-framework
f
Customising the
browserAction.onClicked
behaviour
I'm trying to replicate in Plasmo some of the behaviour from another extension I've written. One behaviour is: On click of the extension, I don't open the popup.tsx, but instead a modal. I'm trying to do that in plasmo by just logging the communication from background to content, but am unable to even inspect the logs. 🤔 (says inactive) Is removing of
popup.tsx
required for plasmo to work? Here's what I have done: 1.
pnpm dlx plasmo
2. Remove
popup.tsx
3. create service worker
Copy code
typescript
// apps/browser/background.ts
chrome.browserAction.onClicked.addListener(async (currentTab) => {
  chrome.tabs.sendMessage(currentTab.id, { greeting: "hello" }, (response) =>
    console.log(response.farewell)
  )
})

export {}
4. Create content script
Copy code
typescript
import type { PlasmoContentScript } from "plasmo"

export const config: PlasmoContentScript = {
  matches: ["https://*/*"]
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log(
    sender.tab
      ? "from a content script:" + sender.tab.url
      : "from the extension"
  )
  if (request.greeting === "hello") sendResponse({ farewell: "goodbye" })
})
Here's a video of the basic background/content messaging not working: https://www.loom.com/share/f771ad21f07c4813843a469edb2d8c08
q
In times like this, one thing I can suggest is to refresh the extension manually. You might've gotten to a weird state
l
Your content script on message listener must return true and the callback must be synchronous if you wanted to use sendResponse - this is a chrome API behavior
f
Will check that later. I’m used working with the webextension-polyfill usually so that might it. 🤨
Okay, so it looks like the service worker registration is failing because
chrome.browserAction
is
undefined
. How is that even possible I wonder?
Currently thinking some permissions in the manifest might be missing
Oh, I think I'm mixing up chrome apis from manifest version 2
✅ So I was using mv2's
browserAction
API, instead of the new
action
.
I'm still encountering an error about establishing the connection regarding the messages but at least I'm able to access the service worker's console now.
10 Views