I wanna make a button to change some on current page
this button is in popup, how should i handle this? should i send it to background then call content? or onmessage it in content dirtectly? or other message communiting ways?
7 Replies
you'll probably need to provide more context
i wanna run a js snippet which is store in storageAPI, but if i use chrome.runtime.sendMessage at Popup.tsx and chrome.runtime.onMessage at background.ts, it would told me unsafe-eval and dont run it。Do I have any other better solution? Besides allowing unsafe-eval
@hibichan has reached level 1. GG!
My English is bad, hope I can express what I mean
background:
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "executeScript") {
const { script, tabId } = request
chrome.scripting.executeScript({
target: { tabId: tabId },
func: function (code) {
eval(code)
},
args: [script]
})
}
})
button in popup:
const executeScript = (script: string) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { const activeTab = tabs[0]; if (activeTab?.id !== undefined) { chrome.runtime.sendMessage({ action: "executeScript", tabId: activeTab.id, script }); } }); }; func: new Function(script) doesnt work
const executeScript = (script: string) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { const activeTab = tabs[0]; if (activeTab?.id !== undefined) { chrome.runtime.sendMessage({ action: "executeScript", tabId: activeTab.id, script }); } }); }; func: new Function(script) doesnt work
I'm not sure if I get your question correctly, but if the gist is you have a button in popup, and want to somehow communicate with a content script that uses the storage api.. if so,
in your popup, use the
in your popup, use the
sendToContentScript
provided by the plasmo messaging API. In your content script, use the useMessage
hook from the same API to intercept the message sent by the popup
As for the storage API implementation that you need, you may provide some contextthanks very much! im going to do
Gave +1 Rep to @ce (current:
#30
- 1
)