I would like to use the sidepanel to notify the user to reload the page when they first install the extension and try to use it so that the content script gets ingested into the page. When I try to do this using the sidePanel, it gives me the error that "sidepanel.open() may only be called in response to a user gesture" even though it is literally responding to a user gesture.
Here is my reproducible example:
When the user loads the extension, the service worker is listening for context menu clicks. When the context menu is clicked, we send a message to the content script to see if its loaded. If it is loaded, we get a response and we open the sidepanel. If it is not loaded, we will get a null response and we open the sidepanel too. My main point here is to show that if we get a response, the sidepanel opens. However, if we don't get a response, I get the sidepanel
does not open and I get this error
`sidePanel.open()` may only be called in response to a user gesture. This is strange because i'm literally applying the same action but it works in one but not the other.
background.js
// Listen for clicks on the context menu item
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "explainAI") {
const tabId = tab.id;
chrome.sidePanel.setOptions({
tabId: tabId,
path: 'sidepanel.html',
enabled: true
});
console.log("Explain AI in Context menu clicked.");
chrome.tabs.sendMessage(
tabId,
{
action: 'checkContentLoaded'
},
function (resp) {
if (!resp) {
console.log('no response received')
chrome.sidePanel.open({tabId: tabId});
} else {
console.log('response received')
chrome.sidePanel.open({tabId: tabId});
}
}
)
// startExplainAI(tab);
}
});
content.js
// check if content is loaded
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "checkContentLoaded") {
sendResponse({ isLoaded: true })
}
});
Thank you!