diff --git a/app/internal_packages/list-unsubscribe/lib/main.tsx b/app/internal_packages/list-unsubscribe/lib/main.tsx index d1790da5f..949d01b89 100644 --- a/app/internal_packages/list-unsubscribe/lib/main.tsx +++ b/app/internal_packages/list-unsubscribe/lib/main.tsx @@ -66,24 +66,30 @@ interface UnsubscribeAction { } function bestUnsubscribeLink(message): string { - const dom = cheerio.load(message.body); - const links = _getLinks(dom); let result = null; - for (const link of links) { - for (const re of regexps) { - if (re.test(link.href)) { - // If the URL contains e.g. "unsubscribe" we assume that we have correctly - // detected the unsubscribe link. - return link.href; - } - if (re.test(link.innerText)) { - // If the URL does not contain "unsubscribe", but the text around the link contains - // it, it is a possible candidate, we we still check all other links for a better match - result = link.href; + // Only check the body if it has been downloaded already + if (message.body) { + + const dom = cheerio.load(message.body); + const links = _getLinks(dom); + + for (const link of links) { + for (const re of regexps) { + if (re.test(link.href)) { + // If the URL contains e.g. "unsubscribe" we assume that we have correctly + // detected the unsubscribe link. + return link.href; + } + if (re.test(link.innerText)) { + // If the URL does not contain "unsubscribe", but the text around the link contains + // it, it is a possible candidate, we we still check all other links for a better match + result = link.href; + } } } + } return result;