-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
49 lines (44 loc) · 1.44 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function toggleSidePanel(tabId) {
chrome.sidePanel.getOptions({ tabId }, (options) => {
if (options.enabled) {
chrome.sidePanel.open({ tabId });
} else {
chrome.sidePanel.setOptions({ tabId, path: 'sidepanel.html', enabled: true }, () => {
chrome.sidePanel.open({ tabId });
});
}
});
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
const url = new URL(tab.url);
const allowedHosts = ['chatgpt.com', 'gemini.google.com', 'claude.ai'];
if (allowedHosts.some(host => url.hostname.includes(host))) {
chrome.sidePanel.setOptions({ tabId, path: 'sidepanel.html', enabled: true });
} else {
chrome.sidePanel.setOptions({ tabId, enabled: false });
}
}
});
chrome.commands.onCommand.addListener((command) => {
if (command === "toggle-side-panel") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: () => {
chrome.runtime.sendMessage({ action: "openSidePanel" });
}
});
}
});
}
});
chrome.action.onClicked.addListener((tab) => {
toggleSidePanel(tab.id);
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "openSidePanel" && sender.tab) {
toggleSidePanel(sender.tab.id);
}
});