Note: i do see similar question but none of them worked for my specific situation.
I'm trying to modify the popup/extension script to show a string that i got from dom manipulation on a website. I'm sending the object to popup page but when i modify the text of the popup, it gives me an error and won't modify the text.
If i remove everything from popup.js then i can modify the text again.
Error using jquery: Error in event handler: ReferenceError: $ is not defined at chrome-extension://macmdofaedcjcklijkiomiojinlgejep/popup/popup.js:5:9 Error using vanilla Javascript: Error in event handler: ReferenceError: document is not defined at chrome-extension://macmdofaedcjcklijkiomiojinlgejep/popup/popup.js:5:9
popup.js file:
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.goldStandardTheme)
sendResponse({ response: request.goldStandardTheme });
});
// There is a p tag that i want the conent of that tag
// to change to the response from the request.goldStandardTheme** } );
Manifest file:
{
"manifest_version": 3,
"permissions": ["activeTab"],
"content_scripts": [
{
"js": ["scripts/content.js"],
"matches": ["<all_urls>"]
}
],
"action": {
"default_popup": "popup/popup.html",
"js": ["popup/popup.js"]
},
"background": {
"service_worker": "popup/popup.js"
}
}
popup.html:
<p>Hello World</p>
<script src="popup.js"></script>
content.js:
async () => {
const response = await chrome.runtime.sendMessage({
goldStandardTheme: finalText,
});
}; // do something with response here, not outside the function
console.log(response);
Any help would be much appreciated. I would except that when i use the follwoing code at popup.js, the text on the extension would update:
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.goldStandardTheme)
sendResponse({ response: request.goldStandardTheme });
document.getElementById('goldStandard').innerHTML = request.goldStandardTheme;
}
);