Überarbeitung: HTML-Templates aus Repo entfernt, Plugin verbessert
- HTML-Vorlagen werden separat verteilt (nicht mehr im Plugin-Repo) - background.js: insertText API für korrekte Darstellung im Composer - background.js: automatischer Wechsel zu HTML-Modus bei Plain-Text - Options-Seite: komplett überarbeitetes UI mit Hotel-Branding - Popup: deutsches UI, gestylte Template-Auswahl - Notifications auf Deutsch - Fertige XPI im Repo
This commit is contained in:
157
background.js
157
background.js
@@ -1,61 +1,96 @@
|
||||
// Sluša poruke iz popupa
|
||||
browser.runtime.onMessage.addListener(async msg => {
|
||||
if (msg.action !== 'insertTemplate') return;
|
||||
try {
|
||||
// 1. Nađi aktivni Compose tab
|
||||
const [tab] = await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
windowType: 'messageCompose',
|
||||
});
|
||||
if (!tab) throw new Error('Nije pronađen Compose tab');
|
||||
// 2. Dohvati postojeći sadržaj
|
||||
const details = await browser.compose.getComposeDetails(tab.id);
|
||||
// 3. Spoji stari i novi tekst
|
||||
let newBody;
|
||||
if (details.isPlainText) {
|
||||
// 1. Grab existing text
|
||||
const old = details.plainTextBody || '';
|
||||
|
||||
// 2. Build new body by prepending with two-line break spacer
|
||||
// If there's no old content, just use msg.text
|
||||
newBody = msg.text + (old ? '\n' + old : '');
|
||||
|
||||
// 3. Set the updated plain-text body
|
||||
await browser.compose.setComposeDetails(tab.id, {
|
||||
plainTextBody: newBody,
|
||||
});
|
||||
} else {
|
||||
const old = details.body || '';
|
||||
// If content already contains HTML tags, use as-is; otherwise convert newlines
|
||||
const htmlTpl = msg.text.includes('<') ? msg.text : msg.text.replace(/\n/g, '<br>');
|
||||
const bodyTag = '<body';
|
||||
const bodyIdx = old.indexOf(bodyTag);
|
||||
|
||||
if (bodyIdx !== -1) {
|
||||
const insertAt = old.indexOf('>', bodyIdx) + 1;
|
||||
newBody =
|
||||
old.slice(0, insertAt) + '\n' + htmlTpl + '\n' + old.slice(insertAt);
|
||||
} else {
|
||||
newBody = htmlTpl + '<br>' + old;
|
||||
}
|
||||
|
||||
await browser.compose.setComposeDetails(tab.id, { body: newBody });
|
||||
}
|
||||
// 4. Obavijest korisniku
|
||||
await browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: browser.runtime.getURL('icons/icon.png'),
|
||||
title: 'Template inserted',
|
||||
message: 'Sucessfully',
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('background.js error:', e);
|
||||
browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: browser.runtime.getURL('icons/icon.png'),
|
||||
title: 'Error on inserting!',
|
||||
message: e.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
browser.runtime.onMessage.addListener(async msg => {
|
||||
if (msg.action !== 'insertTemplate') return;
|
||||
try {
|
||||
const [tab] = await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
windowType: 'messageCompose',
|
||||
});
|
||||
if (!tab) throw new Error('Kein Compose-Fenster gefunden');
|
||||
|
||||
const details = await browser.compose.getComposeDetails(tab.id);
|
||||
const isHtmlTemplate = msg.text.includes('<');
|
||||
|
||||
// If compose is plain text but template is HTML, switch to HTML mode first
|
||||
if (details.isPlainText && isHtmlTemplate) {
|
||||
await browser.compose.setComposeDetails(tab.id, { isPlainText: false });
|
||||
}
|
||||
|
||||
if (details.isPlainText && !isHtmlTemplate) {
|
||||
// Plain text template in plain text mode - use old method
|
||||
const old = details.plainTextBody || '';
|
||||
const newBody = msg.text + (old ? '\n' + old : '');
|
||||
await browser.compose.setComposeDetails(tab.id, { plainTextBody: newBody });
|
||||
} else {
|
||||
// HTML mode: use insertText API to go through the editor's rendering pipeline
|
||||
const htmlContent = isHtmlTemplate ? msg.text : msg.text.replace(/\n/g, '<br>');
|
||||
|
||||
// Move cursor to beginning of body first via script injection
|
||||
await browser.tabs.executeScript(tab.id, {
|
||||
code: `
|
||||
const editor = document.getElementById('messageEditor');
|
||||
const editorDoc = editor ? editor.contentDocument : document;
|
||||
const body = editorDoc.body;
|
||||
if (body) {
|
||||
const range = editorDoc.createRange();
|
||||
range.setStart(body, 0);
|
||||
range.collapse(true);
|
||||
const sel = editorDoc.getSelection();
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
}
|
||||
`
|
||||
}).catch(() => {
|
||||
// Fallback: some TB versions structure the editor differently
|
||||
});
|
||||
|
||||
// Insert HTML at cursor position - this goes through the editor's render pipeline
|
||||
await browser.compose.insertText(tab.id, htmlContent + '<br>', { insertAsText: false });
|
||||
}
|
||||
|
||||
await browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: browser.runtime.getURL('icons/icon.png'),
|
||||
title: 'Vorlage eingefügt',
|
||||
message: 'Erfolgreich',
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('background.js error:', e);
|
||||
|
||||
// Fallback: if insertText fails, try setComposeDetails
|
||||
try {
|
||||
const [tab] = await browser.tabs.query({
|
||||
active: true, currentWindow: true, windowType: 'messageCompose',
|
||||
});
|
||||
if (tab) {
|
||||
const details = await browser.compose.getComposeDetails(tab.id);
|
||||
const old = details.body || '';
|
||||
const htmlTpl = msg.text.includes('<') ? msg.text : msg.text.replace(/\n/g, '<br>');
|
||||
const bodyIdx = old.indexOf('<body');
|
||||
let newBody;
|
||||
if (bodyIdx !== -1) {
|
||||
const insertAt = old.indexOf('>', bodyIdx) + 1;
|
||||
newBody = old.slice(0, insertAt) + '\n' + htmlTpl + '\n' + old.slice(insertAt);
|
||||
} else {
|
||||
newBody = htmlTpl + '<br>' + old;
|
||||
}
|
||||
await browser.compose.setComposeDetails(tab.id, { body: newBody });
|
||||
|
||||
await browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: browser.runtime.getURL('icons/icon.png'),
|
||||
title: 'Vorlage eingefügt',
|
||||
message: 'Erfolgreich (Fallback)',
|
||||
});
|
||||
}
|
||||
} catch (e2) {
|
||||
console.error('background.js fallback error:', e2);
|
||||
browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: browser.runtime.getURL('icons/icon.png'),
|
||||
title: 'Fehler beim Einfügen',
|
||||
message: e2.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user