import { ComposerExtension, FeatureUsageStore } from 'mailspring-exports'; import { PLUGIN_ID, PLUGIN_URL } from './open-tracking-constants'; export default class OpenTrackingComposerExtension extends ComposerExtension { static needsPerRecipientBodies(draft) { return !!draft.metadataForPluginId(PLUGIN_ID); } static applyTransformsForSending({ draftBodyRootNode, draft, recipient }) { // grab message metadata, if any const messageUid = draft.clientId; const metadata = draft.metadataForPluginId(PLUGIN_ID); if (!metadata) { return; } // insert a tracking pixel into the message const q = recipient ? `?recipient=${encodeURIComponent(btoa(recipient.email))}` : ''; const serverUrl = `${PLUGIN_URL}/open/${draft.headerMessageId}${q}`; const imgFragment = document .createRange() .createContextualFragment( `Sent from Mailspring` ); const beforeEl = draftBodyRootNode.querySelector('.gmail_quote'); if (beforeEl) { beforeEl.parentNode.insertBefore(imgFragment, beforeEl); } else { draftBodyRootNode.appendChild(imgFragment); } // save the uid info to draft metadata metadata.uid = messageUid; draft.directlyAttachMetadata(PLUGIN_ID, metadata); } static onSendSuccess(draft) { const metadata = draft.metadataForPluginId(PLUGIN_ID); if (metadata) { FeatureUsageStore.markUsed(PLUGIN_ID); } } }