<commit_before>var contextMenu = require("sdk/context-menu");
var self = require("sdk/self");
var notifications = require("sdk/notifications");
var data = self.data;
var addOnPrefs = require("sdk/simple-prefs").prefs;
var tabs = require("sdk/tabs");
var requestHelper = require("./request-helper");
var storage = require("./storage");
var tracking = require("./tracking");

exports.useContextMenu = useContextMenu;
exports.cleanUpTab = cleanUpTab;

var contextMenus = {};

/**
 * Turns on the context menu with the datawake.
 * @param tab The tab to add the context to.
 */
function useContextMenu(tab) {
    if (addOnPrefs.useContextMenu) {
        var url = tab.url;
        var tabId = tab.id;
        destroyPreviousContextMenu(tabId);
        var datawakeInfo = storage.getDatawakeInfo(tabId);
        contextMenus[tabId] = contextMenu.Menu({
            label: "Datawake Menu: " + datawakeInfo.domain.name,
            contentScriptFile: data.url("js/datawake/selections.js"),
            context: contextMenu.URLContext(url),
            items: [
                contextMenu.Item({ label: "Capture Selection", data: "selection", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Report Extraction Error", data: "feedback", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Trail Based Entity", data: "add-trail-entity", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Irrelevant Trail Based Entity", data: "add-irrelevant-trail-entity", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Custom Trail Based Entity", data: "add-trail-entity-custom"}),
                contextMenu.Item({ label: "Add Custom Irrelevant Trail Based Entity", data: "add-irrelevant-trail-entity-custom"}),
                contextMenu.Separator(),
                contextMenu.Item({ label: "Hide Selections", data: "hide"}),
                contextMenu.Item({ label: "Show Selections", data: "highlight"}),
                contextMenu.Separator(),
                contextMenu.Item({ label: "Show Trail Based Selections", data: "show-trail"}),
                contextMenu.Item({ label: "Hide Trail Based Selections", data: "hide-trail"})

            ],
            onMessage: function (message) {
                var tabId = tabs.activeTab.id;
                var datawakeInfo = storage.getDatawakeInfo(tabId);
                switch (message.intent) {
                    case "highlight":
                        highlightAllTextOnPage(tabId, datawakeInfo);
                        break;
                    case "selection":
                        saveWindowSelection(datawakeInfo, tabs.activeTab.url, message.text);
                        break;
                    case "feedback":
                        saveFeedback(message.text, tabs.activeTab.url, datawakeInfo.domain.name);
                        break;
                    case "hide":
                        hideSelections("selections");
                        break;
                    case "hide-trail":
                        hideSelections("trailentities");
                        break;
                    case "add-trail-entity-custom":
                        addCustomTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-irrelevant-trail-entity-custom":
                        addCustomIrrelevantTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-trail-entity":
                        addTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-irrelevant-trail-entity":
                        addIrrelevantTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "show-trail":
                        showTrailEntities(datawakeInfo.domain.name, datawakeInfo.trail.name);
                        break;
                }
            }
        });
    }
}

function hideSelections(className) {
    tracking.hideSelections(className);
}

function showTrailEntities(domain, trail) {
    var post_obj = JSON.stringify({domain: domain, trail: trail});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/entities", post_obj, function (response) {
        var entities = [];
        for (var entity in response.json.entities)
            entities.push({entity: entity});
        tracking.highlightTrailEntities(entities);
    });
}

function addCustomTrailEntity(domain, trail, entity) {
    tracking.promptTrailBasedEntity(entity, function (text) {
        addTrailEntity(domain, trail, text);
    });
}

function addCustomIrrelevantTrailEntity(domain, trail, entity) {
    tracking.promptIrrelevantTrailBasedEntity(entity, function (text) {
        addIrrelevantTrailEntity(domain, trail, text);
    });
}
function addTrailEntity(domain, trail, entity) {
    var post_obj = JSON.stringify({domain: domain, trail: trail, entity: entity});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/entity", post_obj, function (response) {
        var myIconURL = data.url("img/waveicon38.png");
        notifications.notify({
            text: "Successfully added " + entity + " as an entity!",
            iconURL: myIconURL
        });
    });
}

function addIrrelevantTrailEntity(domain, trail, entity) {
    var post_obj = JSON.stringify({domain: domain, trail: trail, entity: entity});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/irrelevant", post_obj, function (response) {
        var myIconURL = data.url("img/waveicon38.png");
        notifications.notify({
            text: "Successfully added " + entity + " as an irrelevant entity!",
            iconURL: myIconURL
        });
    });
}

/**
 * Saves extractor feedback
 * @param raw_text raw_text that was highlighted
 * @param url the url it occurred on
 * @param domain domain it was apart of.
 */
function saveFeedback(raw_text, url, domain) {

    tracking.promptForExtractedFeedback(raw_text, function (type, extractedValue) {
        var post_obj = {};
        post_obj.raw_text = raw_text;<commit_msg>Added a title to the Notifications<commit_after>var contextMenu = require("sdk/context-menu");
var self = require("sdk/self");
var notifications = require("sdk/notifications");
var data = self.data;
var addOnPrefs = require("sdk/simple-prefs").prefs;
var tabs = require("sdk/tabs");
var requestHelper = require("./request-helper");
var storage = require("./storage");
var tracking = require("./tracking");

exports.useContextMenu = useContextMenu;
exports.cleanUpTab = cleanUpTab;

var contextMenus = {};

/**
 * Turns on the context menu with the datawake.
 * @param tab The tab to add the context to.
 */
function useContextMenu(tab) {
    if (addOnPrefs.useContextMenu) {
        var url = tab.url;
        var tabId = tab.id;
        destroyPreviousContextMenu(tabId);
        var datawakeInfo = storage.getDatawakeInfo(tabId);
        contextMenus[tabId] = contextMenu.Menu({
            label: "Datawake Menu: " + datawakeInfo.domain.name,
            contentScriptFile: data.url("js/datawake/selections.js"),
            context: contextMenu.URLContext(url),
            items: [
                contextMenu.Item({ label: "Capture Selection", data: "selection", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Report Extraction Error", data: "feedback", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Trail Based Entity", data: "add-trail-entity", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Irrelevant Trail Based Entity", data: "add-irrelevant-trail-entity", context: contextMenu.SelectionContext()}),
                contextMenu.Item({ label: "Add Custom Trail Based Entity", data: "add-trail-entity-custom"}),
                contextMenu.Item({ label: "Add Custom Irrelevant Trail Based Entity", data: "add-irrelevant-trail-entity-custom"}),
                contextMenu.Separator(),
                contextMenu.Item({ label: "Hide Selections", data: "hide"}),
                contextMenu.Item({ label: "Show Selections", data: "highlight"}),
                contextMenu.Separator(),
                contextMenu.Item({ label: "Show Trail Based Selections", data: "show-trail"}),
                contextMenu.Item({ label: "Hide Trail Based Selections", data: "hide-trail"})

            ],
            onMessage: function (message) {
                var tabId = tabs.activeTab.id;
                var datawakeInfo = storage.getDatawakeInfo(tabId);
                switch (message.intent) {
                    case "highlight":
                        highlightAllTextOnPage(tabId, datawakeInfo);
                        break;
                    case "selection":
                        saveWindowSelection(datawakeInfo, tabs.activeTab.url, message.text);
                        break;
                    case "feedback":
                        saveFeedback(message.text, tabs.activeTab.url, datawakeInfo.domain.name);
                        break;
                    case "hide":
                        hideSelections("selections");
                        break;
                    case "hide-trail":
                        hideSelections("trailentities");
                        break;
                    case "add-trail-entity-custom":
                        addCustomTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-irrelevant-trail-entity-custom":
                        addCustomIrrelevantTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-trail-entity":
                        addTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "add-irrelevant-trail-entity":
                        addIrrelevantTrailEntity(datawakeInfo.domain.name, datawakeInfo.trail.name, message.text);
                        break;
                    case "show-trail":
                        showTrailEntities(datawakeInfo.domain.name, datawakeInfo.trail.name);
                        break;
                }
            }
        });
    }
}

function hideSelections(className) {
    tracking.hideSelections(className);
}

function showTrailEntities(domain, trail) {
    var post_obj = JSON.stringify({domain: domain, trail: trail});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/entities", post_obj, function (response) {
        var entities = [];
        for (var entity in response.json.entities)
            entities.push({entity: entity});
        tracking.highlightTrailEntities(entities);
    });
}

function addCustomTrailEntity(domain, trail, entity) {
    tracking.promptTrailBasedEntity(entity, function (text) {
        addTrailEntity(domain, trail, text);
    });
}

function addCustomIrrelevantTrailEntity(domain, trail, entity) {
    tracking.promptIrrelevantTrailBasedEntity(entity, function (text) {
        addIrrelevantTrailEntity(domain, trail, text);
    });
}
function addTrailEntity(domain, trail, entity) {
    var post_obj = JSON.stringify({domain: domain, trail: trail, entity: entity});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/entity", post_obj, function (response) {
        var myIconURL = data.url("img/waveicon38.png");
        notifications.notify({
            text: "Successfully added " + entity + " as an entity!",
            title: "Datawake",
            iconURL: myIconURL
        });
    });
}

function addIrrelevantTrailEntity(domain, trail, entity) {
    var post_obj = JSON.stringify({domain: domain, trail: trail, entity: entity});
    requestHelper.post(addOnPrefs.datawakeDeploymentUrl + "/trails/irrelevant", post_obj, function (response) {
        var myIconURL = data.url("img/waveicon38.png");
        notifications.notify({
            text: "Successfully added " + entity + " as an irrelevant entity!",
            title: "Datawake",
            iconURL: myIconURL
        });
    });
}

/**
 * Saves extractor feedback
 * @param raw_text raw_text that was highlighted
 * @param url the url it occurred on
 * @param domain domain it was apart of.
 */
function saveFeedback(raw_text, url, domain) {

    tracking.promptForExtractedFeedback(raw_text, function (type, extractedValue) {
        var post_obj = {};
        post_obj.raw_text = raw_text;
{'fraction_non_alphanumeric': 0.11292209311393589, 'fraction_numerical': 0.0006238789674803088, 'mean_word_length': 2.3000514668039114, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, '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': '5933004', 'n_tokens_mistral': 3465, 'n_tokens_neox': 3359, 'n_words': 748}