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
Shurgbee
Shurgbee•6mo ago
you'll probably need to provide more context
hibichan
hibichanOP•6mo ago
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
Arcane
Arcane•6mo ago
@hibichan has reached level 1. GG!
hibichan
hibichanOP•6mo ago
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
ce
ce•6mo ago
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 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 context
hibichan
hibichanOP•6mo ago
thanks very much! im going to do
YAGPDB.xyz
YAGPDB.xyz•6mo ago
Gave +1 Rep to @ce (current: #30 - 1)
Want results from more Discord servers?
Add your server