I am trying to make the selected text change into the p element of id 'ext' in popup.html and popup.js when I click the buy button but I seem to get undefined.
This is my manifest.json
{ "name": "hoko's ext", "description": "my ext", "version": "1.0", "manifest_version": 3, "action": { "default_popup": "popup.html" }, "permissions": [ "activeTab" ], "background": { "service-worker": "background.js" }, "content_scripts": [ { "matches": ["https://*/*"], "js": ["content.js"] } ] } This is my popup.html
<!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <h1>I am the popup</h1> <button id="btn">Buy</button> <p id="ext">Hello</p> <script type="text/javascript" src="popup.js"></script> </body> </html> This is my background.js service worker
chrome.runtime.onMessage.addListener(receiver); window.word = 'Hello'; function receiver(request, sender, sendResponse) { if (request.text === 'Hello') { sendResponse(request); }} This is my content script. The error is at content.js:13 (anonymous function)
console.log('yo'); window.addEventListener('mouseup', word); function word() { console.log('word selected'); let select = window.getSelection().toString(); console.log(select); if (select.length > 0) { let message = { text: select }; chrome.runtime.sendMessage(message); } } And lastly this is my popup.js script
const btn = document.getElementById('btn'); btn.addEventListener('click', () => { chrome.runtime.sendMessage({text: "Hello"}, function(response) { document.getElementById('ext').innerHTML = response; } ); })