Mailspring/app/internal_packages/open-tracking/lib/open-tracking-composer-extension.es6

61 lines
2.1 KiB
Text
Raw Normal View History

2017-09-27 02:33:08 +08:00
import { ComposerExtension } from 'nylas-exports';
import { PLUGIN_ID, PLUGIN_URL } from './open-tracking-constants';
export default class OpenTrackingComposerExtension extends ComposerExtension {
/**
* This inserts a placeholder image tag to serve as our open tracking
* pixel.
*
* See cloud-api/routes/open-tracking
*
* This image tag is NOT complete at this stage. It requires substantial
* post processing just before send. This happens in iso-core since
* sending can happen immediately or later in cloud-workers.
*
* See isomorphic-core tracking-utils.es6
*
* We don't add a `src` parameter here since we don't want the tracking
* pixel to prematurely load with an incorrect url.
*
* We also need to add individualized recipients to each tracking pixel
* for each message sent to each person.
*
* We finally need to remove the tracking pixel from the message that
* ends up in the users's sent folder. This ensures the sender doesn't
* trip their own open track.
*/
2017-09-27 02:33:08 +08:00
static applyTransformsForSending({ draftBodyRootNode, draft }) {
// grab message metadata, if any
const messageUid = draft.clientId;
const metadata = draft.metadataForPluginId(PLUGIN_ID);
if (!metadata) {
return;
}
// insert a tracking pixel <img> into the message
2017-09-27 02:33:08 +08:00
const serverUrl = `${PLUGIN_URL}/open/${draft.headerMessageId}`;
const imgFragment = document
.createRange()
.createContextualFragment(
`<img class="n1-open" width="0" height="0" style="border:0; width:0; height:0;" data-open-tracking-src="${serverUrl}">`
);
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);
}
2017-09-27 02:33:08 +08:00
static unapplyTransformsForSending({ draftBodyRootNode }) {
const imgEl = draftBodyRootNode.querySelector('.n1-open');
if (imgEl) {
imgEl.parentNode.removeChild(imgEl);
}
}
}