<commit_before>chrome.tabs.onActivated.addListener(function(info) {
    var storage = chrome.storage.local;
    var tabId = info.tabId + '';
    storage.get(tabId, function(items) {
        if (items[tabId]) {
            rebuildMenu(items[tabId]);
        } else {
            var newItem = {};
            newItem[tabId] = 0;
            storage.set(newItem);
            rebuildMenu();
        }
    });
});

chrome.tabs.onRemoved.addListener(function(tabId) {
    chrome.storage.local.remove(tabId + '');
});

function rebuildMenu(interval) {
    chrome.contextMenus.removeAll(function() {
        buildMenu(interval)
    });
}

var intervals = [1, 5, 30];

function buildMenu(interval) {
    interval = interval || '0';
    chrome.contextMenus.create({
        id: 'autoReload',
        title: chrome.i18n.getMessage('parentMenu')
    });

    intervals.forEach(function(val) {
        chrome.contextMenus.create({
            id: 'interval_' + val,
            parentId: 'autoReload',
            type: 'radio',
            checked: (val == interval),
            title: val + ' ' + chrome.i18n.getMessage('shortMinute')
        })
    });

    chrome.contextMenus.create({
        id: 'interval_0',
        parentId: 'autoReload',
        type: 'radio',
        checked: (interval == '0'),
        title: chrome.i18n.getMessage('stopReload')
    });
}

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if (info.menuItemId.indexOf('interval_') === 0 && !info.wasChecked) {
        // update reload time for tab
        var newItem = {},
            newInterval = parseFloat(info.menuItemId.split('_')[1]);
        newItem[tab.id + ''] = newInterval;
        chrome.storage.local.set(newItem);

        // set alarm
        var alarmName = 'alarm_' + tab.id;
        chrome.alarms.clear(alarmName);
        if (newInterval > 0.1) {
            chrome.alarms.create(alarmName, {
                delayInMinutes: newInterval,
                periodInMinutes: newInterval
            })
        }
    }
});

chrome.alarms.onAlarm.addListener(function(alarm) {
    var tabId = parseInt(alarm.name.split('_')[1]),
        now = new Date(),
        strTime = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    console.log(strTime + ' - Reloading tab ' + tabId + ', period ' + alarm.periodInMinutes + ' minutes');
    chrome.tabs.reload(tabId);<commit_msg>clear alarm when tab removed or error thrown during reload<commit_after>chrome.tabs.onActivated.addListener(function(info) {
    var storage = chrome.storage.local;
    var tabId = info.tabId + '';
    storage.get(tabId, function(items) {
        if (items[tabId]) {
            rebuildMenu(items[tabId]);
        } else {
            var newItem = {};
            newItem[tabId] = 0;
            storage.set(newItem);
            rebuildMenu();
        }
    });
});

chrome.tabs.onRemoved.addListener(function(tabId) {
    chrome.storage.local.remove(tabId + '');
    chrome.alarms.clear(getAlarmName(tabId));
});

function rebuildMenu(interval) {
    chrome.contextMenus.removeAll(function() {
        buildMenu(interval)
    });
}

var intervals = [1, 5, 30];

function buildMenu(interval) {
    interval = interval || '0';
    chrome.contextMenus.create({
        id: 'autoReload',
        title: chrome.i18n.getMessage('parentMenu')
    });

    intervals.forEach(function(val) {
        chrome.contextMenus.create({
            id: 'interval_' + val,
            parentId: 'autoReload',
            type: 'radio',
            checked: (val == interval),
            title: val + ' ' + chrome.i18n.getMessage('shortMinute')
        })
    });

    chrome.contextMenus.create({
        id: 'interval_0',
        parentId: 'autoReload',
        type: 'radio',
        checked: (interval == '0'),
        title: chrome.i18n.getMessage('stopReload')
    });
}

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if (info.menuItemId.indexOf('interval_') === 0 && !info.wasChecked) {
        // update reload time for tab
        var newItem = {},
            newInterval = parseFloat(info.menuItemId.split('_')[1]);
        newItem[tab.id + ''] = newInterval;
        chrome.storage.local.set(newItem);

        // set alarm
        var alarmName = getAlarmName(tab.id);
        chrome.alarms.clear(alarmName);
        if (newInterval > 0.1) {
            chrome.alarms.create(alarmName, {
                delayInMinutes: newInterval,
                periodInMinutes: newInterval
            })
        }
    }
});

function getAlarmName(tabId) {
    return 'alarm_' + tabId;
}

chrome.alarms.onAlarm.addListener(function(alarm) {
    var tabId = parseInt(alarm.name.split('_')[1]),
        now = new Date(),
        strTime = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    console.log(strTime + ' - Reloading tab ' + tabId + ', period ' + alarm.periodInMinutes + ' minutes');
    chrome.tabs.reload(tabId, function() {
        if (chrome.extension.lastError) {
            console.log("Error during reload: " + chrome.extension.lastError.message);
            chrome.alarms.clear(alarm.name)
        }
    });
{'fraction_non_alphanumeric': 0.1487747957992999, 'fraction_numerical': 0.007390120575651498, 'mean_word_length': 2.3116548615582744, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '315419', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1599, 'n_words': 259}