I’m having a pretty frustrating problem with these firefox extensions. Specifically, the problem is that I cannot obtain and then use the data following the sendResponse of onMessage.addListener. Here is a short description:
- Here I manage the actions that must occur when starting “call_LLM_Api”. Path background.js
browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
console.log("Message recived form background script:", message);
if (message.type === "call_LLM_Api") {
try {
console.log("Async logic...");
sendResponse({ result: "testtt", angelolo: "ok done" });
} catch (error) {
console.error("Error in back script", error);
sendResponse({ error: error.message });
}
return true;
}
});
- Here the function that makes the call, which in turn is called by other functions which are then handled by popup.js on the click of buttons. Path popup/training.js
async function ApiCall(data) {
try {
// cover browser.runtime.sendMessage in Promise
const response = await new Promise((resolve, reject) => {
browser.runtime.sendMessage(
{
type: "call_LLM_Api",
data: data,
},
(response) => {
if (browser.runtime.lastError) {
reject(new Error(browser.runtime.lastError.message));
} else if (!response) {
// Gestisce il caso in cui la risposta รจ undefined
reject(new Error("Response is undefined"));
} else if (response.error) {
reject(new Error(response.error));
} else {
resolve(response);
}
}
);
});
console.log("background response script:", response);
return response.result; // Ritorna il risultato
} catch (error) {
console.error("Error in message send:", error);
throw error; // Propaga l'errore
}
}
- Here is the manifest.json to define the working context a bit:
{
"manifest_version": 2,
"name": "Policy Analyzer",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"description": "Analyze the Privacy policy you are accepting by accessing a web page.",
"icons":{
"16": "icons/icon-16.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"browser_action":
{
"default_icon":{
"16": "icons/icon-16.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"default_title": "De Compile Privacy",
"default_popup": "popup/popup.html"
},
"permissions": [
"geolocation",
"tabs",
"activeTab",
"http://*/*",
"https://*/*",
"storage",
"webNavigation"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
So the problem seems to be that I can never get correct output from the calling function, it is always either undefined…
Error in message send: Error: Response is undefined
this problem seems quite serious and for this reason I ask you for support…
Thanks to anyone who wants to contribute!!