Browse Source

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

master
Rei 2 years 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 @@
module.exports = {
env: { browser: true, es6: true },
extends: "eslint:recommended",
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"]
},
globals: {
browser: true
}
};

+ 19
- 15
main.js View File

@@ -1,27 +1,31 @@
const classname = "no-caps-addon-activated";

function onError(error) {
console.log(`Error: ${error}`);
}

function initialize(item) {
// If item is empty there is no url blocked
if (item.urls && item.urls.length) {
for (let url of item.urls) {
const initialize = ([{ urls }, { listType }]) => {
let inList = 0;
// If urls is empty there is no url blocked
if (urls && urls.length) {
for (let url of urls) {
let expression = new RegExp(url.replace("*", "[^ ]*"));
const result = location.host.match(expression);

// Exit the function if the current host matches one of the patterns
// Break the loop if the current host matches one of the patterns
if (result && result.length) {
return;
inList = 1;
break;
}
}
}

document.documentElement.classList.add(classname);
// if blocklist && not in list
// if allowlist && in list
if (inList === listType) {
document.documentElement.classList.add(classname);

// Set the title to lowercase
document.title = document.title.toLowerCase();
}
// Set the title to lowercase
document.title = document.title.toLowerCase();
}
};
let urls = browser.storage.local.get("urls");
let listType = browser.storage.local.get("listType");

browser.storage.local.get("urls").then(initialize, onError);
Promise.all([urls, listType]).then(initialize);

+ 16
- 10
settings.html View File

@@ -6,17 +6,23 @@
<title>No-Caps settings</title>
</head>
<body>
<form>
<label>Ignored domains <em>(Seperated by newline)</em>
<form id="settingsform">
<h2>List Mode</h2>
<div>
<input type="radio" name="listType" id="blocklist" value="0" checked>
<label for="blocklist">Blocklist (Everything but the list)</label>
<br/>
<br/>
Examples: <br/>
*google.* <br/>
github.com <br/>
nasa.tumblr.com
<br/>
<textarea type="text" id="urlList" rows="10" cols="50"></textarea>
</label>
<input type="radio" name="listType" id="allowlist" value="1">
<label for="allowlist">Allowlist (Nothing but the list)</label>
</div>

<h2>Domain list <em>(Seperated by newline)</em></h2>
Examples: <br/>
*google.* <br/>
github.com <br/>
nasa.tumblr.com
<br/>
<textarea type="text" id="urlList" rows="10" cols="50" name="urlList"></textarea>
<br/>
<button type="submit">Save</button>
</form>

+ 38
- 16
settings.js View File

@@ -1,27 +1,49 @@
function saveSettings(e) {
const saveSettings = e => {
e.preventDefault();
let urls = document.querySelector("#urlList").value.split("\n");
let urls = [];
// 0 = Blocklist, 1 = allowlist
let listType = 0;
let data = new FormData(document.querySelector("form"));
for (let entry of data) {
switch (entry[0]) {
case "urlList":
urls = entry[1].split("\n");
break;
case "listType":
listType = parseInt(entry[1]);
break;
}
}

browser.storage.local.set({
// Remove empty lines
urls: urls.filter(e => e)
urls: urls.filter(e => e),
listType
});
}
};

function restoreSettings() {
function setCurrentChoice(result) {
document.querySelector("#urlList").value = result.urls
? result.urls.join("\n")
: "";
}
const restoreSettings = () => {
const setUrls = urls => {
document.querySelector("#urlList").value = urls ? urls.join("\n") : "";
};

function onError(error) {
console.log(`Error: ${error}`);
}
const setListType = listType => {
// Blocklist is checked by default
if (listType === 1) {
document.getElementById("allowlist").checked = true;
}
};

var getting = browser.storage.local.get("urls");
getting.then(setCurrentChoice, onError);
}
let urls = browser.storage.local.get("urls");
let listType = browser.storage.local.get("listType");
Promise.all([urls, listType]).then(settings => {
// Set the url-field
setUrls(settings[0].urls);
// Check the right box
setListType(settings[1].listType);
});
// getting.then(setCurrentChoice, onError);
};

document.addEventListener("DOMContentLoaded", restoreSettings);
document.querySelector("form").addEventListener("submit", saveSettings);

Loading…
Cancel
Save