first commit
This commit is contained in:
44
background.js
Normal file
44
background.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// Let Chrome open the panel when the toolbar icon is clicked
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
chrome.sidePanel?.setPanelBehavior?.({ openPanelOnActionClick: true });
|
||||
});
|
||||
|
||||
// If you still want to ensure the correct path on click, setOptions ONLY:
|
||||
chrome.action.onClicked.addListener(async () => {
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
if (!tab?.id) return;
|
||||
|
||||
// inject content.js before the panel opens (best effort; ignore errors)
|
||||
try {
|
||||
const pong = await chrome.tabs.sendMessage(tab.id, { type: 'dewemoji_ping' });
|
||||
if (!pong?.ok) {
|
||||
await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['content.js'] });
|
||||
}
|
||||
} catch {
|
||||
// No content script: inject it
|
||||
try { await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['content.js'] }); } catch {}
|
||||
}
|
||||
|
||||
await chrome.sidePanel.setOptions({ tabId: tab.id, path: "panel.html" }); // Chrome opens it
|
||||
});
|
||||
|
||||
// Keyboard shortcut: we do open() here (this is a user gesture)
|
||||
chrome.commands.onCommand.addListener(async (command) => {
|
||||
if (command !== "toggle-panel") return;
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
if (!tab?.id) return;
|
||||
|
||||
// inject content.js before we open the panel with keyboard
|
||||
try {
|
||||
const pong = await chrome.tabs.sendMessage(tab.id, { type: 'dewemoji_ping' });
|
||||
if (!pong?.ok) {
|
||||
await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['content.js'] });
|
||||
}
|
||||
} catch {
|
||||
// No content script: inject it
|
||||
try { await chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['content.js'] }); } catch {}
|
||||
}
|
||||
|
||||
await chrome.sidePanel.setOptions({ tabId: tab.id, path: "panel.html" });
|
||||
await chrome.sidePanel.open({ tabId: tab.id });
|
||||
});
|
||||
Reference in New Issue
Block a user