<commit_before>/* global browser:readonly */

"use strict";

function parseReceivedHeader(headerStr, regexp) {
    const capturedSubstr = headerStr.match(new RegExp(regexp));
    if (capturedSubstr === null || typeof capturedSubstr[1] === "undefined")
        return null;
    return capturedSubstr[1];
}

function parseReceivedHeaders(headers, regexp) {
    const received = [];
    headers.forEach(function (header) {
        const parsed = parseReceivedHeader(header, regexp);
        if (parsed !== null)
            received.push(parsed);
    });
    return received;
}

browser.storage.local.get().then((res) => {
    if (!res.regexp) {
        browser.storage.local.set({regexp: "(.*)"});
    }
});

browser.messageDisplay.onMessageDisplayed.addListener((tab, message) => {
    browser.messages.getFull(message.id).then((messagepart) => {
        const headers = messagepart.headers.received;
        if (headers) {
            browser.storage.local.get().then((res) => {
                const parsed = parseReceivedHeaders(headers, res.regexp);
                browser.displayReceivedHeader.setReceivedHeaderValue(tab.id, parsed);
                browser.displayReceivedHeader.setReceivedHeaderHidden(tab.id, false);
            });
        } else {
            browser.displayReceivedHeader.setReceivedHeaderHidden(tab.id, true);
        }
    });
});

browser.displayReceivedHeader.init();
<commit_msg>Hide header if no matches found<commit_after>/* global browser:readonly */

"use strict";

function parseReceivedHeader(headerStr, regexp) {
    const capturedSubstr = headerStr.match(new RegExp(regexp));
    if (capturedSubstr === null || typeof capturedSubstr[1] === "undefined")
        return null;
    return capturedSubstr[1];
}

function parseReceivedHeaders(headers, regexp) {
    const received = [];
    headers.forEach(function (header) {
        const parsed = parseReceivedHeader(header, regexp);
        if (parsed !== null)
            received.push(parsed);
    });
    return received;
}

browser.storage.local.get().then((res) => {
    if (!res.regexp) {
        browser.storage.local.set({regexp: "(.*)"});
    }
});

browser.messageDisplay.onMessageDisplayed.addListener((tab, message) => {
    browser.messages.getFull(message.id).then((messagepart) => {
        const headers = messagepart.headers.received;
        if (headers) {
            browser.storage.local.get().then((res) => {
                const parsed = parseReceivedHeaders(headers, res.regexp);
                browser.displayReceivedHeader.setReceivedHeaderValue(tab.id, parsed);
                browser.displayReceivedHeader.setReceivedHeaderHidden(tab.id, !parsed.length);
            });
        } else {
            browser.displayReceivedHeader.setReceivedHeaderHidden(tab.id, true);
        }
    });
});

browser.displayReceivedHeader.init();

{'fraction_non_alphanumeric': 0.13679410733076114, 'fraction_numerical': 0.0014030164854437039, 'mean_word_length': 3.045390070921986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676871', 'n_tokens_mistral': 793, 'n_tokens_neox': 783, 'n_words': 133}