So I am creating a chrome extension and keep having problems when trying to run a function which calls another function
manifest.json is:
{
"name": "Selected text alert",
"version": "1.0",
"manifest_version": 3,
"action": {"default_title": "Click to show an alert"},
"permissions": ["activeTab", "scripting"],
"background": {
"service_worker": "background.js"},
"icons": {
"16": "icon.png",
"48": "icon.png",
"128": "icon.png"}
}
background.js is:
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: {
tabId: tab.id
},
target: {
tabId: tab.id
},
function: allfunction
});
});
function getSelectedText1() {
let selectedText = window.getSelection().toString();
if (selectedText.length \> 0) {
console.log("Selected Text: " + selectedText);
return selectedText;
}
return null;
}
function createAlert(text) {
alert("Selected Text:" + text);
}
function allfunction(){
let selectedText = getSelectedText1();
createAlert(selectedText);
}
in the console window i get this error
VM689:2 Uncaught ReferenceError: getSelectedText1 is not defined
at allfunction (<anonymous>:2:24)
at <anonymous>:4:3
VM689
(function allfunction(){
let selectedText = getSelectedText1();
createAlert(selectedText);
})()
from my guess it seems to not see the other functions Thank you for any suggestions
when calling the extension icon is pressed it cannot find the other functions within the file
selectedText.length \> 0? (Probably not related to the error message in the question, but still worth fixing)