Skip to content

Commit

Permalink
Per-tab panels
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrl committed Dec 6, 2023
1 parent bb164a8 commit a763277
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ chrome.runtime.onInstalled.addListener(() => {

chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error));

// Completely stupid hack, openPanelOnActionClick: true is only able to open a panel that persists through all the tabs
// the only way to avoid this is to remove side_panel.default_path from manifest, set openPanelOnActionClick to true and then
// for every tab you navigate to you would set tabId for the side panel in the listener
//
// It is also not possible to use chrome.sidePanel.open with the needed tab.id because it demands user interaction first.
// Clicking on the action (chrome.action.onClicked) is not considered a user interaction... however it works on the second attempt
chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
if (!tab.url) return;
const url = new URL(tab.url);
await chrome.sidePanel.setOptions({
tabId: tab.id,
path: 'src/sidepanel/sidepanel.html',
enabled: true
});
});

chrome.runtime.onMessage.addListener(
async function(request, sender, sendResponse) {
const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
Expand Down
3 changes: 0 additions & 3 deletions src/manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export default defineManifest(async (env) => ({
page: "src/options/options.html",
open_in_tab: false,
},
side_panel: {
default_path: "src/sidepanel/sidepanel.html",
},
action: {
default_icon: {
"16": "src/assets/icons/icon-16.png",
Expand Down

0 comments on commit a763277

Please sign in to comment.