<commit_before>// settings:
var max_tabs = {};

max_tabs.defaultSettings = {
    // default values
    maxTabs: 5,
    exceedAction: "closeLeftmost",
    countPinned: false,
    closePinned: false
};

max_tabs.notificationId = "numOfTabsExceededWarning";

max_tabs.onTabAddedOrUnpinned = function (wid, tid) {
    chrome.storage.sync.get(["maxTabs", "exceedAction", "closePinned", "countPinned"],
        /**
         * @param {
         * {maxTabs:int},
         * {exceedAction:string},
         * {closePinned:boolean},
         * {countPinned:boolean}
         * } options
         */
        function (options) {
            chrome.tabs.query({
                "windowId": wid
            }, function (tabs) {

                if (!options.countPinned) {
                    tabs = tabs.filter(function (tab) {
                        return !tab.pinned;
                    });
                }

                var numOfTabs = tabs.length;
                if (numOfTabs > options.maxTabs) {
                    tab_stats.addTabStats(tabs[0].windowId, tabs, function (tabs) {

                        // filter the tab that was just opened/unpinned:
                        tabs = tabs.filter(function (tab) {
                            return tab.id !== tid;
                        });

                        if (options.exceedAction === "showWarning") {
                            chrome.notifications.create(max_tabs.notificationId, {
                                type: "basic",
                                title: chrome.i18n.getMessage("warning_title"),
                                iconUrl: "/icons/exclamation.png",
                                message: chrome.i18n.getMessage("warning_msg"),
                                buttons: [{
                                    title: chrome.i18n.getMessage("change_limit"),
                                    iconUrl: "/icons/glyphicons-390-new-window-alt.png"
                                }]
                            });
                            return;
                        }

                        if (options.exceedAction === "preventNewTab") {
                            console.warn("TODO: Not implemented");
                            // TODO
                            return;
                        }

                        var numToClose = numOfTabs - options.maxTabs;
                        var tabsToClose = [];

                        switch (options.exceedAction) {
                            case "closeLeftmost":
                                tabsToClose = tabs.splice(0, numToClose);
                                break;
                            case "closeLRActive":
                                tabsToClose = tabs.sort(function(a,b) {
                                    return b.last_active < a.last_active;
                                }).splice(0, numToClose);
                                break;
                            case "closeLRUpdated":
                                tabsToClose = tabs.sort(function(a,b) {
                                    return b.last_updated < a.last_updated;
                                }).splice(0, numToClose);
                                break;
                            case "closeOldest":
                                tabsToClose = tabs.sort(function(a,b) {
                                    return b.opened_time < a.opened_time;
                                }).splice(0, numToClose);
                                break;
                            case "closeRandom":
                                tabsToClose = tabs.shuffle().splice(0, numToClose);
                                break;<commit_msg>Added default settings on first time install<commit_after>// Set default settings on new installation!
chrome.runtime.onInstalled.addListener(function (details) {
    if (details.reason == "install") {
        chrome.storage.sync.set(max_tabs.defaultSettings);
    }
});

// settings:
var max_tabs = {};

max_tabs.defaultSettings = {
    // default values
    maxTabs: 5,
    exceedAction: "closeLRActive",
    countPinned: false,
    closePinned: false
};

max_tabs.notificationId = "numOfTabsExceededWarning";

max_tabs.onTabAddedOrUnpinned = function (wid, tid) {
    chrome.storage.sync.get(["maxTabs", "exceedAction", "closePinned", "countPinned"],
        /**
         * @param {
         * {maxTabs:int},
         * {exceedAction:string},
         * {closePinned:boolean},
         * {countPinned:boolean}
         * } options
         */
        function (options) {
            chrome.tabs.query({
                "windowId": wid
            }, function (tabs) {

                if (!options.countPinned) {
                    tabs = tabs.filter(function (tab) {
                        return !tab.pinned;
                    });
                }

                var numOfTabs = tabs.length;
                if (numOfTabs > options.maxTabs) {
                    tab_stats.addTabStats(tabs[0].windowId, tabs, function (tabs) {

                        // filter the tab that was just opened/unpinned:
                        tabs = tabs.filter(function (tab) {
                            return tab.id !== tid;
                        });

                        if (options.exceedAction === "showWarning") {
                            chrome.notifications.create(max_tabs.notificationId, {
                                type: "basic",
                                title: chrome.i18n.getMessage("warning_title"),
                                iconUrl: "/icons/exclamation.png",
                                message: chrome.i18n.getMessage("warning_msg"),
                                buttons: [{
                                    title: chrome.i18n.getMessage("change_limit"),
                                    iconUrl: "/icons/glyphicons-390-new-window-alt.png"
                                }]
                            });
                            return;
                        }

                        if (options.exceedAction === "preventNewTab") {
                            console.warn("TODO: Not implemented");
                            // TODO
                            return;
                        }

                        var numToClose = numOfTabs - options.maxTabs;
                        var tabsToClose = [];

                        switch (options.exceedAction) {
                            case "closeLeftmost":
                                tabsToClose = tabs.splice(0, numToClose);
                                break;
                            case "closeLRActive":
                                tabsToClose = tabs.sort(function (a, b) {
                                    return b.last_active < a.last_active;
                                }).splice(0, numToClose);
                                break;
                            case "closeLRUpdated":
                                tabsToClose = tabs.sort(function (a, b) {
                                    return b.last_updated < a.last_updated;
                                }).splice(0, numToClose);
                                break;
                            case "closeOldest":
                                tabsToClose = tabs.sort(function (a, b) {
                                    return b.opened_time < a.opened_time;
                                }).splice(0, numToClose);
                                break;
                            case "closeRandom":
                                tabsToClose = tabs.shuffle().splice(0, numToClose);
                                break;
{'fraction_non_alphanumeric': 0.09359091502414828, 'fraction_numerical': 0.004177000391593787, 'mean_word_length': 0.9466463414634146, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '204377', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1745, 'n_words': 311}