Quinton42
Quinton42
PD🧩 Plasmo Developers
Created by AB7zz on 9/5/2023 in #👾extension
How do I message from background to my content script?
Here is the solution I use for bidirectional communication: I created a new file lib/eventStore.ts
import { Storage } from "@plasmohq/storage"

const eventStore = new Storage({
area: "local"
})

export const pub = (event: string, data: any) => {
eventStore.set(event, data)
}

export const sub = (event: string, callback: (data: any) => void) => {
eventStore.watch({
[event]: (change: { newValue: any }) => {
callback(change.newValue)
}
})
}
import { Storage } from "@plasmohq/storage"

const eventStore = new Storage({
area: "local"
})

export const pub = (event: string, data: any) => {
eventStore.set(event, data)
}

export const sub = (event: string, callback: (data: any) => void) => {
eventStore.watch({
[event]: (change: { newValue: any }) => {
callback(change.newValue)
}
})
}
`
19 replies