{"size": 2192, "ext": "js", "max_stars_repo_path": "backgrounds/main.js", "max_stars_repo_name": "eryw/chrome-simple-blocker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backgrounds/main.js", "max_issues_repo_name": "eryw/chrome-simple-blocker", "max_issues_repo_issues_event_min_datetime": "2020-12-02T12:25:42.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-12T01:04:24.000Z", "max_forks_repo_path": "backgrounds/main.js", "max_forks_repo_name": "eryw/chrome-simple-blocker", "max_forks_repo_forks_event_min_datetime": "2019-06-21T16:55:05.000Z", "max_forks_repo_forks_event_max_datetime": "2019-06-21T16:55:05.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 22.3673469388, "max_line_length": 69, "alphanum_fraction": 0.5369525547}
"use strict";

var debug = false;
var mode = 0;

let regexes = [];

function normalizeData(data) {
    if (!data.simpleBlocker) {
        data = {
            simpleBlocker: {
                regexes: '',
                debug: debug,
                mode: mode
            }
        };
    }

    return data;
}

function updateCacheVar(strRegexArray, lmode, ldebug) {
    mode = lmode;
    debug = ldebug;
    regexes = [];
    for (let r of strRegexArray) {
        if (r) {
            try {
              regexes.push(new RegExp(r));
            } catch(e) {
              // silent
            }
        }
    }
}

function save(stringRegexes, lmode, ldebug) {
    chrome.storage.local.get("simpleBlocker").then((data) => {
        data = normalizeData(data);
        data.simpleBlocker.regexes = stringRegexes;
        data.simpleBlocker.mode = lmode;
        data.simpleBlocker.debug = ldebug;
        chrome.storage.local.set(data);
        updateCacheVar(
          stringRegexes.split(/\r?\n/),
          lmode,
          ldebug
          );
    });
}

function retrieve() {
    return chrome.storage.local.get("simpleBlocker").then((data) => {
        data = normalizeData(data);
        updateCacheVar(
          data.simpleBlocker.regexes.split(/\r?\n/),
          data.simpleBlocker.mode,
          data.simpleBlocker.debug
          );

        return data.simpleBlocker;
    });
}

function filterRequest(request) {
    let cancel = false;
    for (let regex of regexes) {
      if ((mode == '0' && regex.test(request.url)) ||
          (mode == '1' && !regex.test(request.url))
      ) {
        if (debug == '1') {
          console.log("Canceled request: " + request.url);
        }
        cancel = true;
        break;
      }
    }

    return { cancel: cancel };
}

chrome.storage.local.get("simpleBlocker").then((data) => {
    data = normalizeData(data);
    updateCacheVar(
      data.simpleBlocker.regexes.split(/\r?\n/),
      data.simpleBlocker.mode,
      data.simpleBlocker.debug
      );

    //chrome.storage.local.remove("simpleBlocker")
});

chrome.webRequest.onBeforeRequest.addListener(
    filterRequest,
    {urls: ["<all_urls>"]},
    ["blocking"]
);
