diff --git a/background.js b/background.js
index 39a2360..fe2a1c8 100644
--- a/background.js
+++ b/background.js
@@ -276,30 +276,49 @@ async function handleInsertTemplate(msg) {
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
+ // HTML mode: wrap the template in a marker element (id=hps-vorlage) and
+ // replace any previously inserted one — so durchswitchen ersetzt statt stapelt.
const htmlContent = isHtmlTemplate ? msg.text : msg.text.replace(/\n/g, '
');
- // Move cursor to beginning of body first via script injection
- await browser.tabs.executeScript(tab.id, {
+ // Directly manipulate the editor DOM: remove the old template block (if any),
+ // then insert the new one at the top of the body. Returns true on success.
+ const injected = await browser.tabs.executeScript(tab.id, {
code: `
- const editor = document.getElementById('messageEditor');
- const editorDoc = editor ? editor.contentDocument : document;
- const body = editorDoc.body;
- if (body) {
+ (function () {
+ const editor = document.getElementById('messageEditor');
+ const editorDoc = editor ? editor.contentDocument : document;
+ const body = editorDoc && editorDoc.body;
+ if (!body) return false;
+
+ // Remove a previously inserted template block, so switching replaces it
+ const prev = editorDoc.getElementById('hps-vorlage');
+ if (prev) prev.remove();
+
+ // Wrap the new template in a marker element with a stable id
+ const wrapper = editorDoc.createElement('div');
+ wrapper.id = 'hps-vorlage';
+ wrapper.innerHTML = ${JSON.stringify(htmlContent)};
+
+ // Insert at the very top of the body (above quote/signature/typed text)
+ body.insertBefore(wrapper, body.firstChild);
+
+ // Place the cursor right after the inserted block
const range = editorDoc.createRange();
- range.setStart(body, 0);
+ range.setStartAfter(wrapper);
range.collapse(true);
const sel = editorDoc.getSelection();
sel.removeAllRanges();
sel.addRange(range);
- }
+ return true;
+ })();
`
- }).catch(() => {
- // Fallback: some TB versions structure the editor differently
- });
+ }).then(r => (Array.isArray(r) ? r[0] : r)).catch(() => false);
- // Insert HTML at cursor position - this goes through the editor's render pipeline
- await browser.compose.insertText(tab.id, htmlContent + '
', { insertAsText: false });
+ if (!injected) {
+ // Fallback: editor DOM not reachable (TB version differences) — use the
+ // insertText pipeline. Note: this variant cannot replace, only prepend.
+ await browser.compose.insertText(tab.id, htmlContent + '
', { insertAsText: false });
+ }
}
} catch (e) {
diff --git a/manifest.json b/manifest.json
index 76d078e..4458f07 100644
--- a/manifest.json
+++ b/manifest.json
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "HPS Vorlagen & Signaturen",
- "version": "2.5.0",
+ "version": "2.6.0",
"description": "Vorlagen- und Signaturverwaltung für Hotel Park Soltau mit Git-Sync",
"browser_specific_settings": {
"gecko": {
diff --git a/templates-reply-hotel.xpi b/templates-reply-hotel.xpi
index 614f2a4..7c41787 100644
Binary files a/templates-reply-hotel.xpi and b/templates-reply-hotel.xpi differ