Mailspring/internal_packages/link-tracking/lib/link-tracking-message-extension.es6
Evan Morikawa 9dd7a9600a fix(tracking): fix link tracking and read receipt plugins
Fix broken links in link tracking and read receipts

Fix bug in email frame where it wouldn't adjust the height even when
content changed

MessageItem bodies automatically clear the MessageBodyProcessor cache when
the message contents (including metadata) change.

Remove unused Account stuff from nylas-observables

Plugins appIds properly read if there's an environment set
2016-02-24 12:30:12 -08:00

48 lines
1.9 KiB
JavaScript

import {MessageViewExtension, RegExpUtils} from 'nylas-exports'
import {PLUGIN_ID} from './link-tracking-constants'
export default class LinkTrackingMessageExtension extends MessageViewExtension {
static formatMessageBody({message}) {
const metadata = message.metadataForPluginId(PLUGIN_ID) || {};
if ((metadata.links || []).length === 0) { return }
const links = {}
for (const link of metadata.links) {
links[link.redirect_url] = link
}
message.body = message.body.replace(RegExpUtils.linkTagRegex(), (match, openTagPrefix, aTagHref, openTagSuffix, content, closingTag) => {
if (links[aTagHref]) {
const openTag = openTagPrefix + aTagHref + openTagSuffix
let title;
let dotSrc;
let newOpenTag;
const titleRe = /title="[^"]*"|title='[^']*'/gi;
if (!content) { return match; }
if (content.search("link-tracking-dot") >= 0) { return match; }
const originalUrl = links[aTagHref].url;
const dotImgSrcPrefix = "nylas://link-tracking/assets/";
const dotStyles = "margin-left: 1px; vertical-align: super; margin-right: 2px; zoom: 0.55;"
if (links[aTagHref].click_count > 0) {
title = ` title="Number of clicks: ${links[aTagHref].click_count} | ${originalUrl}" `;
dotSrc = dotImgSrcPrefix + "ic-tracking-visited@2x.png"
} else {
title = ` title="Never been clicked | ${originalUrl}" `
dotSrc = dotImgSrcPrefix + "ic-tracking-unvisited@2x.png"
}
const dot = `<img class="link-tracking-dot" src="${dotSrc}" style="${dotStyles}" />`
if (titleRe.test(openTag)) {
newOpenTag = openTag.replace(titleRe, title)
} else {
const tagLen = openTag.length
newOpenTag = openTag.slice(0, tagLen - 1) + title + openTag.slice(tagLen - 1, tagLen)
}
return newOpenTag + content + dot + closingTag
}
return match;
})
}
}