74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
const HIGHLIGHT_JS = "vendor/highlightjs/highlight.min.js";
|
|
|
|
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
|
if (changeInfo.status !== "complete" || !tab?.id) return;
|
|
|
|
try {
|
|
const { autoFormat } = await chrome.storage.sync.get({ autoFormat: false });
|
|
if (!autoFormat) return;
|
|
|
|
// Haben wir Host-Permissions?
|
|
const have = await chrome.permissions.contains({ origins: ["<all_urls>"] });
|
|
if (!have) return;
|
|
|
|
// Nur im aktiven Tab (du wolltest Beschränkung auf aktiven Tab für Auto)
|
|
const active = tab.active === true;
|
|
if (!active) return;
|
|
|
|
console.log("[bg] auto inject on updated tab", tabId, tab.url);
|
|
await chrome.scripting.executeScript({
|
|
target: { tabId },
|
|
files: [HIGHLIGHT_JS, "format.js", "content.js"]
|
|
});
|
|
} catch (e) {
|
|
console.error("[bg] onUpdated error", e);
|
|
}
|
|
});
|
|
|
|
chrome.runtime.onInstalled.addListener(() => {
|
|
console.log("[bg] onInstalled");
|
|
chrome.contextMenus.create({
|
|
id: "manual-format",
|
|
title: "JSON jetzt formatieren",
|
|
contexts: ["page"]
|
|
});
|
|
chrome.contextMenus.create({
|
|
id: "open-options",
|
|
title: "Einstellungen",
|
|
contexts: ["action"]
|
|
});
|
|
});
|
|
|
|
async function inject(tabId) {
|
|
try {
|
|
console.log("[bg] injecting into tab", tabId);
|
|
await chrome.scripting.executeScript({
|
|
target: { tabId },
|
|
func: () => {
|
|
window.__forceManualFormat = true;
|
|
}
|
|
});
|
|
await chrome.scripting.executeScript({
|
|
target: { tabId },
|
|
files: [HIGHLIGHT_JS, "format.js", "content.js"]
|
|
});
|
|
console.log("[bg] injection done");
|
|
} catch (e) {
|
|
console.error("[bg] injection error", e);
|
|
}
|
|
}
|
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
|
if (info.menuItemId === "open-options") {
|
|
chrome.runtime.openOptionsPage();
|
|
return;
|
|
}
|
|
if (info.menuItemId === "manual-format" && tab?.id) {
|
|
inject(tab.id);
|
|
}
|
|
});
|
|
|
|
chrome.action.onClicked.addListener((tab) => {
|
|
if (tab?.id) inject(tab.id);
|
|
});
|