Browse Source

closes #1 | Add option for allow/blocklist, improved options menu design

master
Rei 1 year ago
parent
commit
a5146e433b
4 changed files with 86 additions and 41 deletions
  1. 13
    0
      .eslintrc.js
  2. 19
    15
      main.js
  3. 16
    10
      settings.html
  4. 38
    16
      settings.js

+ 13
- 0
.eslintrc.js View File

@@ -0,0 +1,13 @@
1
+module.exports = {
2
+  env: { browser: true, es6: true },
3
+  extends: "eslint:recommended",
4
+  rules: {
5
+    indent: ["error", 2],
6
+    "linebreak-style": ["error", "unix"],
7
+    quotes: ["error", "double"],
8
+    semi: ["error", "always"]
9
+  },
10
+  globals: {
11
+    browser: true
12
+  }
13
+};

+ 19
- 15
main.js View File

@@ -1,27 +1,31 @@
1 1
 const classname = "no-caps-addon-activated";
2 2
 
3
-function onError(error) {
4
-  console.log(`Error: ${error}`);
5
-}
6
-
7
-function initialize(item) {
8
-  // If item is empty there is no url blocked
9
-  if (item.urls && item.urls.length) {
10
-    for (let url of item.urls) {
3
+const initialize = ([{ urls }, { listType }]) => {
4
+  let inList = 0;
5
+  // If urls is empty there is no url blocked
6
+  if (urls && urls.length) {
7
+    for (let url of urls) {
11 8
       let expression = new RegExp(url.replace("*", "[^ ]*"));
12 9
       const result = location.host.match(expression);
13 10
 
14
-      // Exit the function if the current host matches one of the patterns
11
+      // Break the loop if the current host matches one of the patterns
15 12
       if (result && result.length) {
16
-        return;
13
+        inList = 1;
14
+        break;
17 15
       }
18 16
     }
19 17
   }
20 18
 
21
-  document.documentElement.classList.add(classname);
19
+  // if blocklist && not in list
20
+  // if allowlist && in list
21
+  if (inList === listType) {
22
+    document.documentElement.classList.add(classname);
22 23
 
23
-  // Set the title to lowercase
24
-  document.title = document.title.toLowerCase();
25
-}
24
+    // Set the title to lowercase
25
+    document.title = document.title.toLowerCase();
26
+  }
27
+};
28
+let urls = browser.storage.local.get("urls");
29
+let listType = browser.storage.local.get("listType");
26 30
 
27
-browser.storage.local.get("urls").then(initialize, onError);
31
+Promise.all([urls, listType]).then(initialize);

+ 16
- 10
settings.html View File

@@ -6,17 +6,23 @@
6 6
   <title>No-Caps settings</title>
7 7
 </head>
8 8
 <body>
9
-  <form>
10
-    <label>Ignored domains <em>(Seperated by newline)</em>
9
+  <form id="settingsform">
10
+    <h2>List Mode</h2>
11
+    <div>
12
+      <input type="radio" name="listType" id="blocklist" value="0" checked>
13
+      <label for="blocklist">Blocklist (Everything but the list)</label>
11 14
       <br/>
12
-      <br/>
13
-      Examples: <br/>
14
-      *google.* <br/>
15
-      github.com <br/>
16
-      nasa.tumblr.com
17
-      <br/>
18
-      <textarea type="text" id="urlList" rows="10" cols="50"></textarea>
19
-    </label>
15
+      <input type="radio" name="listType" id="allowlist" value="1">
16
+      <label for="allowlist">Allowlist (Nothing but the list)</label>
17
+    </div>
18
+
19
+    <h2>Domain list <em>(Seperated by newline)</em></h2>
20
+    Examples: <br/>
21
+    *google.* <br/>
22
+    github.com <br/>
23
+    nasa.tumblr.com
24
+    <br/>
25
+    <textarea type="text" id="urlList" rows="10" cols="50" name="urlList"></textarea>
20 26
     <br/>
21 27
     <button type="submit">Save</button>
22 28
   </form>

+ 38
- 16
settings.js View File

@@ -1,27 +1,49 @@
1
-function saveSettings(e) {
1
+const saveSettings = e => {
2 2
   e.preventDefault();
3
-  let urls = document.querySelector("#urlList").value.split("\n");
3
+  let urls = [];
4
+  // 0 = Blocklist, 1 = allowlist
5
+  let listType = 0;
6
+  let data = new FormData(document.querySelector("form"));
7
+  for (let entry of data) {
8
+    switch (entry[0]) {
9
+    case "urlList":
10
+      urls = entry[1].split("\n");
11
+      break;
12
+    case "listType":
13
+      listType = parseInt(entry[1]);
14
+      break;
15
+    }
16
+  }
4 17
 
5 18
   browser.storage.local.set({
6 19
     // Remove empty lines
7
-    urls: urls.filter(e => e)
20
+    urls: urls.filter(e => e),
21
+    listType
8 22
   });
9
-}
23
+};
10 24
 
11
-function restoreSettings() {
12
-  function setCurrentChoice(result) {
13
-    document.querySelector("#urlList").value = result.urls
14
-      ? result.urls.join("\n")
15
-      : "";
16
-  }
25
+const restoreSettings = () => {
26
+  const setUrls = urls => {
27
+    document.querySelector("#urlList").value = urls ? urls.join("\n") : "";
28
+  };
17 29
 
18
-  function onError(error) {
19
-    console.log(`Error: ${error}`);
20
-  }
30
+  const setListType = listType => {
31
+    // Blocklist is checked by default
32
+    if (listType === 1) {
33
+      document.getElementById("allowlist").checked = true;
34
+    }
35
+  };
21 36
 
22
-  var getting = browser.storage.local.get("urls");
23
-  getting.then(setCurrentChoice, onError);
24
-}
37
+  let urls = browser.storage.local.get("urls");
38
+  let listType = browser.storage.local.get("listType");
39
+  Promise.all([urls, listType]).then(settings => {
40
+    // Set the url-field
41
+    setUrls(settings[0].urls);
42
+    // Check the right box
43
+    setListType(settings[1].listType);
44
+  });
45
+  // getting.then(setCurrentChoice, onError);
46
+};
25 47
 
26 48
 document.addEventListener("DOMContentLoaded", restoreSettings);
27 49
 document.querySelector("form").addEventListener("submit", saveSettings);

Loading…
Cancel
Save