antfu-collective / vitesse-webext

⚡️ WebExtension Vite Starter Template
MIT License
2.92k stars 225 forks source link

Sidepanel example #174

Closed FlavioZanoni closed 5 months ago

FlavioZanoni commented 7 months ago

Hello, I was using the template and needed to use it with the browser sidepanel, but there was no doc and no build step for the sidepanel, so i`ve made the necessary changes in this fork

I did not test it in Firefox due to an issue where it says that I can`t upload due to the ext being MV3. Are the firefox builds currently working ?

I can open a PR with the changes if you see that it has any value for the project

brauliobo commented 6 months ago

thanks for sharing @FlavioZanoni, I'll give it a try with @pvfm

iplanwebsites commented 5 months ago

Looks good! I assume Sidepanel will slowly replace extension popups.

antfu commented 5 months ago

Love to see a PR! Thanks

FlavioZanoni commented 5 months ago

Open ! sorry for the delay, was away for a couple of days.

brauliobo commented 5 months ago

One confusing thing about the sidepanel is how it is triggered. We've modified @FlavioZanoni code to remove the manifest's default_panel and default_popup actions as they trigger always either the sidepanel or the popup. Instead we used the following script in the background/main.js:

chrome.action.onClicked.addListener((tab) => {
  chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
    const currentTab = tabs[0]
    if (CONDITION) {
      chrome.action.setPopup({ tabId: currentTab.id, popup: "" })
      chrome.sidePanel.setOptions({ tabId: currentTab.id, path: "./dist/sidepanel/index.html" })
      chrome.sidePanel.open({ tabId: currentTab.id })
    } else {
      chrome.action.setPopup({ tabId: currentTab.id, popup: "./dist/popup/index.html" })
      chrome.action.openPopup()
    }
  })
})