Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Extension-samples/sidebar/main-sb.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Main sidebar</title>
</head>
<body>
<h1>Heading</h1>
<p>Sentence.</p>
</body>
</html>
12 changes: 12 additions & 0 deletions Extension-samples/sidebar/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "My sidebar extension",
"side_panel": {
"default_path": "sidebar.html"
},
"action": {
"default_title": "Click to open sidebar"
},
"permissions": [
"sidePanel"
]
}
56 changes: 56 additions & 0 deletions Extension-samples/sidebar/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const BING_ORIGIN = 'https://www.bing.com';

chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
if (!tab.url) return;
const url = new URL(tab.url);
// Enable the sidebar when at bing.com.
if (url.origin === BING_ORIGIN) {
await chrome.sidePanel.setOptions({
tabId,
path: 'sidepanel.html',
enabled: true
});
} else {
// Disable the sidebar when at other sites.
await chrome.sidePanel.setOptions({
tabId,
enabled: false
});
}
});

// Allow users to open the sidebar by clicking the action toolbar icon.
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error));

// Switch to a different sidebar.
const welcomePage = 'sidebar/welcome-sb.html';
const mainPage = 'sidebar/main-sb.html';

chrome.runtime.onInstalled.addListener(() => {
chrome.sidePanel.setOptions({ path: welcomePage });
});

chrome.tabs.onActivated.addListener(async ({ tabId }) => {
const { path } = await chrome.sidePanel.getOptions({ tabId });
if (path === welcomePage) {
chrome.sidePanel.setOptions({ path: mainPage });
}
});

// Open the sidebar upon user interaction.
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'openSidePanel',
title: 'Open sidebar',
contexts: ['all']
});
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'openSidePanel') {
// Open the sidebar in all the tabs of the current window.
chrome.sidePanel.open({ windowId: tab.windowId });
}
});
10 changes: 10 additions & 0 deletions Extension-samples/sidebar/sidebar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>My Sidebar</title>
</head>
<body>
<h1>Sidebar extension for all sites</h1>
<p>This sidebar is enabled on all sites</p>
</body>
</html>
10 changes: 10 additions & 0 deletions Extension-samples/sidebar/welcome-sb.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome sidebar</title>
</head>
<body>
<h1>Heading</h1>
<p>Sentence.</p>
</body>
</html>