<commit_before>/**
 * This is the main script for the background "page". Initiates all background staff like toolbar button, page actions
 * or notifications.
 * @requires Options.js, RemotePontoon.js, RemoteLinks.js, ToolbarButton.js, PageAction.js, Notifications.js
 */
'use strict';

const options = new Options();
const pontoonBaseUrlOptionKey = 'pontoon_base_url';
const localeTeamOptionKey = 'locale_team';
const teamsListDataKey = 'teamsList';
const projectsListDataKey = 'projectsList';

// Register onInstalled event listener to store its details in case the event fires before all the async stuff below are ready.
let newInstallationDetails;
let onInstallFunction = (details) => newInstallationDetails = details;
browser.runtime.onInstalled.addListener((details) => {
    onInstallFunction(details);
});

/**
 * Create selection context menus on projects related websites to report a bug or search selected text in Pontoon. If
 * the menus already exist, removes and creates them again with the new data.
 * @param projects list of projects in Pontoon
 * @param team locale team selected by the user in options
 * @param remotePontoon RemotePontoon instance used in background
 * @param remoteLinks RemoteLinks instance used in background
 */
function createContextMenus(projects, team, remotePontoon, remoteLinks) {
    // Create website patterns for all projects in Pontoon.
    const mozillaWebsitesUrlPatterns = [];
    Object.values(projects).forEach((project) =>
        project.domains.forEach((domain) => mozillaWebsitesUrlPatterns.push(`https://${domain}/*`))
    );

    // Recreate the selection context menus (report l10n bug & search in project)
    browser.contextMenus.remove('page-context-menu-parent');
    const mozillaPageContextMenuParent = browser.contextMenus.create({
        id: 'page-context-menu-parent',
        title: 'Pontoon Tools',
        documentUrlPatterns: mozillaWebsitesUrlPatterns,
        contexts: ['selection'],
    });
    browser.contextMenus.remove('page-context-menu-report-l10n-bug');
    browser.contextMenus.create({
        id: 'page-context-menu-report-l10n-bug',
        title: 'Report l10n bug for "%s"',
        documentUrlPatterns: mozillaWebsitesUrlPatterns,
        contexts: ['selection'],
        parentId: mozillaPageContextMenuParent,
        onclick: (info, tab) => {
            browser.tabs.create({url: remoteLinks.getBugzillaReportUrlForSelectedTextOnPage(info.selectionText, tab.url, team.code, team.bz_component)});
        },
    });
    Object.values(projects).forEach((project) => {
        project.domains.forEach((domain) => {
            const projectSearchMenuId = `page-context-menu-search-${project.slug}-${domain}`;
            browser.contextMenus.remove(projectSearchMenuId);
            browser.contextMenus.create({
                id: projectSearchMenuId,
                title: `Search for "%s" in Pontoon (${project.name})`,
                documentUrlPatterns: [`https://${domain}/*`],
                contexts: ['selection'],
                parentId: mozillaPageContextMenuParent,
                onclick: (info, tab) => browser.tabs.create({url: remotePontoon.getSearchInProjectUrl(project.slug, info.selectionText)}),
            });

            const allProjectsSearchMenuId = `page-context-menu-search-all-${domain}`;
            browser.contextMenus.remove(allProjectsSearchMenuId);
            browser.contextMenus.create({
                id: allProjectsSearchMenuId,
                title: `Search for "%s" in Pontoon (all projects)`,
                documentUrlPatterns: [`https://${domain}/*`],
                contexts: ['selection'],
                parentId: mozillaPageContextMenuParent,
                onclick: (info, tab) => browser.tabs.create({url: remotePontoon.getSearchInAllProjectsUrl(info.selectionText)}),
            });
        });
    });
}

/**
 * With the necessary options and data from storage, create all parts of the UI and objects handling data updates.
 */
Promise.all([
    options.get([pontoonBaseUrlOptionKey, localeTeamOptionKey]),
    browser.storage.local.get([teamsListDataKey, projectsListDataKey]),
]).then(([
    optionsItems,
    storageItems<commit_msg>Fix eslint error<commit_after>/**
 * This is the main script for the background "page". Initiates all background staff like toolbar button, page actions
 * or notifications.
 * @requires Options.js, RemotePontoon.js, RemoteLinks.js, ToolbarButton.js, PageAction.js, Notifications.js
 */
'use strict';

const options = new Options();
const pontoonBaseUrlOptionKey = 'pontoon_base_url';
const localeTeamOptionKey = 'locale_team';
const teamsListDataKey = 'teamsList';
const projectsListDataKey = 'projectsList';

// Register onInstalled event listener to store its details in case the event fires before all the async stuff below are ready.
let newInstallationDetails;
let onInstallFunction = (details) => newInstallationDetails = details;
browser.runtime.onInstalled.addListener((details) => {
    onInstallFunction(details);
});

/**
 * Create selection context menus on projects related websites to report a bug or search selected text in Pontoon. If
 * the menus already exist, removes and creates them again with the new data.
 * @param projects list of projects in Pontoon
 * @param team locale team selected by the user in options
 * @param remotePontoon RemotePontoon instance used in background
 * @param remoteLinks RemoteLinks instance used in background
 */
function createContextMenus(projects, team, remotePontoon, remoteLinks) {
    // Create website patterns for all projects in Pontoon.
    const mozillaWebsitesUrlPatterns = [];
    Object.values(projects).forEach((project) =>
        project.domains.forEach((domain) => mozillaWebsitesUrlPatterns.push(`https://${domain}/*`))
    );

    // Recreate the selection context menus (report l10n bug & search in project)
    browser.contextMenus.remove('page-context-menu-parent');
    const mozillaPageContextMenuParent = browser.contextMenus.create({
        id: 'page-context-menu-parent',
        title: 'Pontoon Tools',
        documentUrlPatterns: mozillaWebsitesUrlPatterns,
        contexts: ['selection'],
    });
    browser.contextMenus.remove('page-context-menu-report-l10n-bug');
    browser.contextMenus.create({
        id: 'page-context-menu-report-l10n-bug',
        title: 'Report l10n bug for "%s"',
        documentUrlPatterns: mozillaWebsitesUrlPatterns,
        contexts: ['selection'],
        parentId: mozillaPageContextMenuParent,
        onclick: (info, tab) => {
            browser.tabs.create({url: remoteLinks.getBugzillaReportUrlForSelectedTextOnPage(info.selectionText, tab.url, team.code, team.bz_component)});
        },
    });
    Object.values(projects).forEach((project) => {
        project.domains.forEach((domain) => {
            const projectSearchMenuId = `page-context-menu-search-${project.slug}-${domain}`;
            browser.contextMenus.remove(projectSearchMenuId);
            browser.contextMenus.create({
                id: projectSearchMenuId,
                title: `Search for "%s" in Pontoon (${project.name})`,
                documentUrlPatterns: [`https://${domain}/*`],
                contexts: ['selection'],
                parentId: mozillaPageContextMenuParent,
                onclick: (info, tab) => browser.tabs.create({url: remotePontoon.getSearchInProjectUrl(project.slug, info.selectionText)}),
            });

            const allProjectsSearchMenuId = `page-context-menu-search-all-${domain}`;
            browser.contextMenus.remove(allProjectsSearchMenuId);
            browser.contextMenus.create({
                id: allProjectsSearchMenuId,
                title: 'Search for "%s" in Pontoon (all projects)',
                documentUrlPatterns: [`https://${domain}/*`],
                contexts: ['selection'],
                parentId: mozillaPageContextMenuParent,
                onclick: (info, tab) => browser.tabs.create({url: remotePontoon.getSearchInAllProjectsUrl(info.selectionText)}),
            });
        });
    });
}

/**
 * With the necessary options and data from storage, create all parts of the UI and objects handling data updates.
 */
Promise.all([
    options.get([pontoonBaseUrlOptionKey, localeTeamOptionKey]),
    browser.storage.local.get([teamsListDataKey, projectsListDataKey]),
]).then(([
    optionsItems,
    storageItems
{'fraction_non_alphanumeric': 0.11260564218545412, 'fraction_numerical': 0.001904535174384002, 'mean_word_length': 3.392054364871929, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 6, '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': '6590154', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2244, 'n_words': 573}