Upload via GUI (23 Dateien)
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
// Hintergrunddienst: prüft jede Minute auf neue Tickets/Antworten, zeigt den Zähler am Symbol und Benachrichtigungen,
|
||||
// bietet „Als DeskHub-Ticket anlegen“ im Rechtsklick-Menü an.
|
||||
// Hintergrunddienst: prüft jede Minute auf Neues, zeigt Zähler und Benachrichtigungen, Rechtsklick-Menü,
|
||||
// Tastenkürzel und die Adressleisten-Suche („dh“ + Leertaste).
|
||||
import { apiCall, getSession, getSettings, truncate } from './lib/core.js';
|
||||
|
||||
const ALARM = 'deskhub-poll';
|
||||
const MENU = 'deskhub-create';
|
||||
const APP = 'app.html';
|
||||
|
||||
async function setBadge(count, color = '#0f766e') {
|
||||
await chrome.action.setBadgeBackgroundColor({ color });
|
||||
await chrome.action.setBadgeText({ text: count > 0 ? (count > 99 ? '99+' : String(count)) : '' });
|
||||
}
|
||||
|
||||
async function ensureSetup() {
|
||||
async function ensureAlarm() {
|
||||
if (!(await chrome.alarms.get(ALARM))) chrome.alarms.create(ALARM, { periodInMinutes: 1, delayInMinutes: 0.1 });
|
||||
}
|
||||
async function ensureSetup() {
|
||||
await ensureAlarm();
|
||||
chrome.contextMenus.removeAll(() => {
|
||||
chrome.contextMenus.create({ id: MENU, title: 'Als DeskHub-Ticket anlegen', contexts: ['selection', 'page'] });
|
||||
});
|
||||
}
|
||||
// Auch wenn der Hintergrunddienst nur neu gestartet wurde (kein Installations- oder Start-Ereignis): Die Abfrage-Uhr muss laufen
|
||||
ensureAlarm();
|
||||
|
||||
chrome.runtime.onInstalled.addListener(ensureSetup);
|
||||
chrome.runtime.onStartup.addListener(async () => { await ensureSetup(); poll(); });
|
||||
@@ -27,14 +33,17 @@ export async function poll() {
|
||||
const { lastSeen, badgeCount = 0 } = await chrome.storage.local.get(['lastSeen', 'badgeCount']);
|
||||
try {
|
||||
const { data } = await apiCall('activity.php', { query: lastSeen ? { since: lastSeen } : {} });
|
||||
const fresh = (data.newReplies || 0) + (data.newUnassigned || 0);
|
||||
const settings = await getSettings();
|
||||
const unassigned = settings.notifyUnassigned ? data.newUnassigned || 0 : 0;
|
||||
const replies = settings.notifyMine ? data.newReplies || 0 : 0;
|
||||
const fresh = unassigned + replies;
|
||||
const total = badgeCount + fresh;
|
||||
await chrome.storage.local.set({ lastSeen: data.serverTime, badgeCount: total });
|
||||
await setBadge(total);
|
||||
if (fresh > 0 && (await getSettings()).notifications) {
|
||||
if (fresh > 0 && settings.notifications) {
|
||||
const parts = [];
|
||||
if (data.newUnassigned) parts.push(`${data.newUnassigned} neu und nicht zugewiesen`);
|
||||
if (data.newReplies) parts.push(`${data.newReplies} Aktualisierung${data.newReplies === 1 ? '' : 'en'} bei deinen Tickets`);
|
||||
if (unassigned) parts.push(`${unassigned} neu und nicht zugewiesen`);
|
||||
if (replies) parts.push(`${replies} Aktualisierung${replies === 1 ? '' : 'en'} bei deinen Tickets`);
|
||||
chrome.notifications.create('deskhub-new', { type: 'basic', iconUrl: 'icons/icon128.png', title: 'DeskHub', message: parts.join(', '), priority: 1 });
|
||||
}
|
||||
return { fresh, total };
|
||||
@@ -51,9 +60,7 @@ export async function poll() {
|
||||
|
||||
chrome.alarms.onAlarm.addListener((alarm) => { if (alarm.name === ALARM) poll(); });
|
||||
|
||||
async function openCreateWindow() {
|
||||
return chrome.windows.create({ url: chrome.runtime.getURL('popup.html?window=1&create=1'), type: 'popup', width: 440, height: 680 });
|
||||
}
|
||||
const openWindow = (query = '') => chrome.windows.create({ url: chrome.runtime.getURL(`${APP}?mode=window${query}`), type: 'popup', width: 460, height: 760 });
|
||||
|
||||
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||
if (info.menuItemId !== MENU) return;
|
||||
@@ -61,20 +68,41 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||
const selection = (info.selectionText || '').trim();
|
||||
const source = tab?.url && /^https?:/i.test(tab.url) ? `\n\nQuelle: ${tab.url}` : '';
|
||||
await chrome.storage.local.set({ prefill: { subject, message: (selection + source).trim() } });
|
||||
await openCreateWindow();
|
||||
await openWindow('&create=1');
|
||||
});
|
||||
|
||||
chrome.commands?.onCommand.addListener(async (command, tab) => {
|
||||
if (command === 'quick-create') await openWindow('&create=1');
|
||||
else if (command === 'open-panel') {
|
||||
try { await chrome.sidePanel.open({ windowId: tab?.windowId ?? (await chrome.windows.getCurrent()).id }); } catch { await openWindow(); }
|
||||
}
|
||||
});
|
||||
|
||||
// Adressleiste: „dh 261031-0299“ öffnet das Ticket, „dh Drucker“ sucht danach.
|
||||
const looksLikeTicket = (s) => /^#?(\d{4,}-\d+|\d{5,}|[A-Za-z]{2,6}-\d+)$/.test(s.trim());
|
||||
chrome.omnibox?.onInputChanged.addListener((text, suggest) => {
|
||||
const t = text.trim();
|
||||
if (!t) return suggest([]);
|
||||
suggest(looksLikeTicket(t) ? [{ content: t, description: `Ticket ${t.replace(/^#/, '')} öffnen` }] : [{ content: t, description: `Tickets suchen: ${t.replace(/[<>&]/g, '')}` }]);
|
||||
});
|
||||
chrome.omnibox?.onInputEntered.addListener(async (text) => {
|
||||
const t = text.trim();
|
||||
if (!t) return;
|
||||
await openWindow(looksLikeTicket(t) ? `&ticket=${encodeURIComponent(t.replace(/^#/, ''))}` : `&q=${encodeURIComponent(t)}`);
|
||||
});
|
||||
|
||||
chrome.notifications.onClicked.addListener(async (id) => {
|
||||
chrome.notifications.clear(id);
|
||||
try { await chrome.action.openPopup(); } catch { await chrome.windows.create({ url: chrome.runtime.getURL('popup.html?window=1'), type: 'popup', width: 440, height: 680 }); }
|
||||
try { await chrome.action.openPopup(); } catch { await openWindow(); }
|
||||
});
|
||||
|
||||
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
|
||||
(async () => {
|
||||
if (msg?.type === 'reset-badge') { await chrome.storage.local.set({ badgeCount: 0 }); await setBadge(0); sendResponse({ ok: true }); }
|
||||
else if (msg?.type === 'poll-now') sendResponse(await poll());
|
||||
else if (msg?.type === 'session-changed') { await chrome.storage.local.remove(['lastSeen', 'badgeCount']); await setBadge(0); sendResponse(await poll()); }
|
||||
else if (msg?.type === 'presence') { await apiCall('presence.php', { method: 'POST', body: {} }).catch(() => {}); sendResponse({ ok: true }); }
|
||||
else if (msg?.type === 'session-changed') { await ensureAlarm(); await chrome.storage.local.remove(['lastSeen', 'badgeCount']); await setBadge(0); sendResponse(await poll()); }
|
||||
else sendResponse({ ok: false });
|
||||
})();
|
||||
})().catch((e) => { try { sendResponse({ error: (e && e.code) || 'ERROR' }); } catch { /* Fenster schon zu */ } });
|
||||
return true; // Antwort kommt asynchron
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user