|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +chrome.runtime.onInstalled.addListener(() => { |
| 16 | + chrome.contextMenus.create({ |
| 17 | + id: 'openSidePanel', |
| 18 | + title: 'Open side panel', |
| 19 | + contexts: ['all'] |
| 20 | + }); |
| 21 | + chrome.tabs.create({ url: 'page.html' }); |
| 22 | +}); |
| 23 | + |
| 24 | +chrome.contextMenus.onClicked.addListener((info, tab) => { |
| 25 | + if (info.menuItemId === 'openSidePanel') { |
| 26 | + // This will open the panel in all the pages on the current window. |
| 27 | + chrome.sidePanel.open({ windowId: tab.windowId }); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +chrome.runtime.onMessage.addListener((message, sender) => { |
| 32 | + // The callback for runtime.onMessage must return falsy if we're not sending a response |
| 33 | + (async () => { |
| 34 | + if (message.type === 'open_side_panel') { |
| 35 | + // This will open a tab-specific side panel only on the current tab. |
| 36 | + await chrome.sidePanel.open({ tabId: sender.tab.id }); |
| 37 | + await chrome.sidePanel.setOptions({ |
| 38 | + tabId: sender.tab.id, |
| 39 | + path: 'sidepanel-tab.html', |
| 40 | + enabled: true |
| 41 | + }); |
| 42 | + } |
| 43 | + })(); |
| 44 | +}); |
0 commit comments