Browse Source

Touched up options page, now selected subreddits can be removed

Chris Konstad 3 years ago
parent
commit
0874cda07b
3 changed files with 74 additions and 2 deletions
  1. 1 1
      README.md
  2. 11 0
      options.html
  3. 62 1
      options.js

+ 1 - 1
README.md

@ -5,7 +5,7 @@
5 5
* [ ] add fuzzy matching
6 6
* [ ] don't save a subreddit if it doesn't exist (URL is a search page)
7 7
* [x] don't save data when in incognito, per google guidelines
8
* [ ] be able to remove select subreddits from list
8
* [x] be able to remove select subreddits from list
9 9
* [x] be able to clear data
10 10
* [x] make options page nicer
11 11
* [ ] reset the counts on a schedule (weekly?), but keep the subreddit listed

+ 11 - 0
options.html

@ -2,10 +2,21 @@
2 2
<html>
3 3
<head>
4 4
  <title>r/ Options</title>
5
  <style>
6
    .subreddit_item {
7
      display: block;
8
      margin-top: 5px;
9
    }
10
  </style>
5 11
</head>
6 12
<body>
13
  <h1>Clear All Stored Subreddits</h1>
7 14
  <button id="clear_subreddits">Clear Subreddits</button>
8 15

													
16
  <h1>Remove Selected Subreddits</h1>
17
  <button id="remove_subreddits">Remove Selected Subreddits</button>
18
  <div id="subreddit_list"></div>
19

													
9 20
  <script src="options.js"></script>
10 21
</body>
11 22
</html>

+ 62 - 1
options.js

@ -1,6 +1,67 @@
1 1
function clear_subreddits() {
2
  chrome.storage.sync.clear();
2
  // TODO ask for user confirmation, but confirm doesn't work on Options2
3
  chrome.storage.sync.clear(function() { loadData(); });
4
}
5

													
6
function remove_subreddits() {
7
  var items = document.getElementById("subreddit_list").childNodes;
8
  var toRemove = [];
9

													
10
  for(var i=0; i< items.length; i++) {
11
    var cb = items[i].childNodes[0];
12
    if(cb.checked) {
13
      toRemove.push(cb.value);
14
    }
15
  }
16

													
17
  chrome.storage.sync.remove(toRemove);
18

													
19
  // Refresh the listing
20
  loadData();
3 21
}
4 22

													
5 23
document.getElementById("clear_subreddits").addEventListener("click",
6 24
  clear_subreddits);
25

													
26
document.getElementById("remove_subreddits").addEventListener("click",
27
  remove_subreddits);
28

													
29
function loadData() {
30
  var div = document.getElementById("subreddit_list");
31

													
32
  // Empty the div
33
  while(div.firstChild) {
34
    div.removeChild(div.firstChild);
35
  }
36

													
37
  // Load the stored subreddits
38
  chrome.storage.sync.get(null, function(items) {
39
    var subs = [];
40
    for(var key in items) {
41
      subs.push(key);
42
    }
43

													
44
    subs.sort();
45

													
46
    subs.forEach(function(key) {
47
      var item = document.createElement("div");
48
      item.className = "subreddit_item";
49
      var id = "subreddit_" + key;
50
      var cb = document.createElement("input");
51
      cb.type = "checkbox";
52
      cb.name = "subreddit";
53
      cb.value = key;
54
      cb.id = id;
55

													
56
      var label = document.createElement("label");
57
      label.htmlFor = id;
58
      label.appendChild(document.createTextNode(key));
59

													
60
      item.appendChild(cb);
61
      item.appendChild(label);
62
      div.appendChild(item);
63
    });
64
  });
65
}
66

													
67
loadData();