summaryrefslogtreecommitdiffstats
path: root/src/lib/options.js
blob: 8e6bec74dee05edc816650a4cf53348021b8f394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * This file is part of Privacy Badger <https://www.eff.org/privacybadger>
 * Copyright (C) 2018 Electronic Frontier Foundation
 *
 * Privacy Badger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Privacy Badger is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Privacy Badger.  If not, see <http://www.gnu.org/licenses/>.
 */

require.scopes.optionslib = (function () {

/**
 * Gets array of encountered origins.
 *
 * @param {Object} origins The starting set of domains to be filtered.
 * @param {String} [filter_text] Text to filter origins with.
 * @param {String} [type_filter] Type: user-controlled/DNT-compliant
 * @param {String} [status_filter] Status: blocked/cookieblocked/allowed
 * @param {Boolean} [show_not_yet_blocked] Whether to show domains your Badger
 * hasn't yet learned to block.
 *
 * @return {Array} The array of origins.
 */
function getOriginsArray(origins, filter_text, type_filter, status_filter, show_not_yet_blocked) {
  // Make sure filter_text is lower case for case-insensitive matching.
  if (filter_text) {
    filter_text = filter_text.toLowerCase();
  } else {
    filter_text = "";
  }

  /**
   * @param {String} origin The origin to test.
   * @return {Boolean} Does the origin pass filters?
   */
  function matchesFormFilters(origin) {
    const value = origins[origin];

    if (!show_not_yet_blocked) {
      // hide the not-yet-seen-on-enough-sites potential trackers
      if (value == "allow") {
        return false;
      }
    }

    // filter by type
    if (type_filter) {
      if (type_filter == "user") {
        if (!value.startsWith("user")) {
          return false;
        }
      } else {
        if (value != type_filter) {
          return false;
        }
      }
    }

    // filter by status
    if (status_filter) {
      if (status_filter != value.replace("user_", "") && !(
        status_filter == "allow" && value == "dnt"
      )) {
        return false;
      }
    }

    // filter by search text
    // treat spaces as OR operators
    // treat "-" prefixes as NOT operators
    let textFilters = filter_text.split(" ").filter(i=>i); // remove empties

    // no text filters, we have a match
    if (!textFilters.length) {
      return true;
    }

    let positiveFilters = textFilters.filter(i => i[0] != "-"),
      lorigin = origin.toLowerCase();

    // if we have any positive filters, and we don't match any,
    // don't bother checking negative filters
    if (positiveFilters.length) {
      let result = positiveFilters.some(text => {
        return lorigin.indexOf(text) != -1;
      });
      if (!result) {
        return false;
      }
    }

    // we either matched a positive filter,
    // or we don't have any positive filters

    // if we match any negative filters, discard the match
    return textFilters.every(text => {
      if (text[0] != "-" || text == "-") {
        return true;
      }
      return lorigin.indexOf(text.slice(1)) == -1;
    });
  }

  // Include only origins that match given filters.
  return Object.keys(origins).filter(matchesFormFilters);
}

return {
  getOriginsArray,
};

}()); // end of require.scopes