Skip to content

Commit 07516a1

Browse files
authored
Merge pull request #18 from ut-code/feature/updatesidepanel
対象ページのみで有効化とAlt+Sでショートカット
2 parents 9f7a233 + 1547991 commit 07516a1

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

public/manifest.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@
2020
"js": ["content.js"],
2121
"run_at": "document_idle"
2222
}
23-
]
23+
],
24+
"commands": {
25+
"_execute_action": {
26+
"suggested_key": { "default": "Alt+S" },
27+
"description": "Open side panel"
28+
}
29+
}
2430
}
31+

src/background.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,43 @@ chrome.runtime.onInstalled.addListener(() => {
22
console.log("拡張機能がインストールされました!");
33
});
44

5-
// ツールバーアイコンをクリックしたときにサイドパネルを開く
6-
chrome.action.onClicked.addListener(async (tab) => {
7-
await chrome.sidePanel.open({ windowId: tab.windowId, tabId: tab.id });
5+
6+
const targetURL = 'https://browser-hack.utcode.net/'
7+
function isTargetPage(url?: string): boolean {
8+
if (!url) return false
9+
return url.startsWith(targetURL)
10+
}
11+
12+
async function updatePanelAvailability(tabId: number) {
13+
try {
14+
const tab = await chrome.tabs.get(tabId);
15+
if (isTargetPage(tab.url)) {
16+
await chrome.sidePanel.setOptions({
17+
tabId,
18+
path: 'src/sidepanel/index.html',
19+
enabled: true
20+
});
21+
} else {
22+
await chrome.sidePanel.setOptions({
23+
tabId,
24+
enabled: false
25+
});
26+
}
27+
} catch (e) {
28+
console.log('Failed to update side panel for tab:', tabId, e);
29+
}
30+
}
31+
32+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
33+
if (changeInfo.status === 'complete' && tab.url) {
34+
updatePanelAvailability(tabId);
35+
}
836
});
37+
38+
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
39+
updatePanelAvailability(tabId);
40+
});
41+
42+
chrome.sidePanel
43+
.setPanelBehavior({ openPanelOnActionClick: true })
44+
.catch((error) => console.error(error));

0 commit comments

Comments
 (0)