/**
 * No Coin - Stop coin miners in your browser
 **
 * @author      Rafael Keramidas <ker.af>
 * @license     MIT
 * @source      https://github.com/keraf/NoCoin
 */

// Config
const defaultConfig = {
    toggle: true,
    whitelist: [{
        domain: 'cnhv.co',
        expiration: 0,
    }],
};

const localConfig = JSON.parse(localStorage.getItem('config'));
let config = {
    ...defaultConfig,
    ...localConfig,
};

/**
 * Functions
 */
const saveConfig = () => {
    localStorage.setItem('config', JSON.stringify(config));
};

const changeToggleIcon = (isEnabled) => {
    chrome.browserAction.setIcon({
        path: `img/${isEnabled ? 'logo' : 'logo_disabled'}.png`,
    });
};

const getDomain = (url) => {
    return url.match(/:\/\/(.[^/]+)/)[1];
};

const getTimestamp = () => {
    return Math.floor(Date.now() / 1000);
};

const isDomainWhitelisted = (domain) => {
    if (!domain) return false;

    const domainInfo = config.whitelist.find(w => w.domain === domain);

    if (domainInfo) {
        if (domainInfo.expiration !== 0 && domainInfo.expiration <= getTimestamp()) {
            removeDomainFromWhitelist(domain);

            return false;
        }

        return true;
    }

    return false;
};

const addDomainToWhitelist = (domain, time) => {
    if (!domain) return;

    // Make sure the domain is not already whitelisted before adding it
    if (!isDomainWhitelisted(domain)) {
        config.whitelist = [
            ...config.whitelist,
            {
                domain: domain,
                expiration: time === 0 ? 0 : getTimestamp() + (time * 60),
            },
        ];
        saveConfig();
    }
};

const removeDomainFromWhitelist = (domain) => {
    if (!domain) return;

    config.whitelist = config.whitelist.filter(w => w.domain !== domain);
    saveConfig();
}

/**
 * Main
 */
let domains = [];

// Updating domain for synchronous checking in onBeforeRequest
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    domains[tabId] = getDomain(tab.url);
});

chrome.tabs.onRemoved.addListener((tabId) => {
    delete domains[tabId];
});

// Run with the right icon
if (!config.toggle) {
    changeToggleIcon(false);
}

// Load the blacklist and run the request checker
const blacklist = chrome.runtime.getURL("blacklist.txt");
fetch(blacklist)
    .then(resp => {
        resp.text()
            .then(text => {
                const blacklistedUrls = text.split('\r\n');
                
                chrome.webRequest.onBeforeRequest.addListener(details => {
                    // Globally paused
                    if (!config.toggle) {
                        return { cancel: false };
                    }

                    // Is domain white listed
                    if (isDomainWhitelisted(domains[details.tabId])) {
                        return { cancel: false };
                    }

                    return { cancel: true };
                }, { 
                    urls: blacklistedUrls
                }, ['blocking']);
            })
            .catch(err => {
                // TODO: Handle this
                console.log(err);
            });
    })
    .catch(err => {
        // TODO: Handle this
        console.log(err);
    });

// Communication with the popup and content scripts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    switch (message.type) {
        case 'GET_STATE':
            sendResponse({
                whitelisted: isDomainWhitelisted(domains[message.tabId]),
                toggle: config.toggle,
            });
            break;
        case 'TOGGLE':
            config.toggle = !config.toggle;
            saveConfig();

            changeToggleIcon(config.toggle);
            sendResponse(config.toggle);
            break;
        case 'WHITELIST': {
            if (message.whitelisted) {
                removeDomainFromWhitelist(domains[message.tabId], message.time);
            } else {
                addDomainToWhitelist(domains[message.tabId], message.time);
            }

            sendResponse(!message.whitelisted);
            break;
        }
    }
});

@@ -2240,16 +2240,18 @@
 checker%0A
+//
 const bl
@@ -2300,16 +2300,116 @@
 .txt%22);%0A
+const blacklistFileURL = %22https://raw.githubusercontent.com/keraf/NoCoin/master/src/blacklist.txt%22;%0A
 fetch(bl
@@ -2415,16 +2415,23 @@
 lacklist
+FileURL
 )%0A    .t
@@ -4248,13 +4248,12 @@
  %7D%0A    %7D%0A%7D);
-%0A

Revert "Update background.js"