const MENU_ID = "grit-copy";
function inject(tabId, frameIds) {
chrome.scripting
.executeScript({
target: frameIds ? { tabId, frameIds } : { tabId, allFrames: true },
files: ["grit.js"]
})
.catch(() => {});
}
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
id: MENU_ID,
title: "Copy Git",
contexts: ["selection"]
});
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId !== MENU_ID || !tab?.id) return;
inject(tab.id, [info.frameId ?? 0]);
});
chrome.commands.onCommand.addListener(async (name, tab) => {
if (name !== "copy-git") return;
const id = tab?.id ?? (await chrome.tabs.query({ active: true, currentWindow: true }))[0]?.id;
if (id) inject(id);
});