'use strict';

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.executeScript(tab.id, { file: "app/generate.js", allFrames: true }, function () {});
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (!request || request.event !== 'countChange') {
        return;
    }

    if (request.overlayCount && 0 !== request.overlayCount) {
        chrome.browserAction.setBadgeText({
            text: '' + request.overlayCount,
            tabId: sender.tab.id
        });
    } else {
        chrome.browserAction.setBadgeText({
            text: '',
            tabId: sender.tab.id
        });
    }
});

chrome.runtime.onInstalled.addListener(function (details) {
    if ('update' === details.reason) {
        chrome.storage.local.get('settings', function (items) {
            var settings = JSON.parse(items.settings), key, isNew = false;

            for (key in DEFAULT_SETTINGS) {
                if (DEFAULT_SETTINGS.hasOwnProperty(key) && !settings.hasOwnProperty(key) ) {
                    settings[key] = DEFAULT_SETTINGS[key];
                    isNew = true;
                }
            }

            if (true === isNew) {
                chrome.storage.local.set({
                    settings: JSON.stringify(settings)
                });
            }
        });
    } else {
        chrome.storage.local.set({
            settings: JSON.stringify(DEFAULT_SETTINGS)
        });
    }
});
'use strict';

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.executeScript(tab.id, { file: "lib/crypto-js-3.1.2.js", allFrames: true }, function () {});
    chrome.tabs.executeScript(tab.id, { file: "lib/vault.js", allFrames: true }, function () {});
    chrome.tabs.executeScript(tab.id, { file: "lib/core.js", allFrames: true }, function () {});
    chrome.tabs.executeScript(tab.id, { file: "app/vault-generator.js", allFrames: true }, function () {});
    chrome.tabs.executeScript(tab.id, { file: "app/generate.js", allFrames: true }, function () {});
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (!request || request.event !== 'countChange') {
        return;
    }

    if (request.overlayCount && 0 !== request.overlayCount) {
        chrome.browserAction.setBadgeText({
            text: '' + request.overlayCount,
            tabId: sender.tab.id
        });
    } else {
        chrome.browserAction.setBadgeText({
            text: '',
            tabId: sender.tab.id
        });
    }
});

chrome.runtime.onInstalled.addListener(function (details) {
    if ('update' === details.reason) {
        chrome.storage.local.get('settings', function (items) {
            var settings = JSON.parse(items.settings), key, isNew = false;

            for (key in DEFAULT_SETTINGS) {
                if (DEFAULT_SETTINGS.hasOwnProperty(key) && !settings.hasOwnProperty(key) ) {
                    settings[key] = DEFAULT_SETTINGS[key];
                    isNew = true;
                }
            }

            if (true === isNew) {
                chrome.storage.local.set({
                    settings: JSON.stringify(settings)
                });
            }
        });
    } else {
        chrome.storage.local.set({
            settings: JSON.stringify(DEFAULT_SETTINGS)
        });
    }
});
make sure all scripts will be reloaded