Permalink
Browse files

Add ability to add custom white listed domains

  • Loading branch information...
1 parent c691e6d commit bd065cff436c0246bdf16ec29f4bd6720e6695f0 @pthulin pthulin committed Dec 5, 2011
Showing with 109 additions and 24 deletions.
  1. +17 −24 options.html
  2. +92 −0 options.js
View
@@ -2,40 +2,33 @@
<html>
<head>
<style>
- textarea {
- display: block;
- width: 300px;
- height: 400px;
+ ul {
+ padding: 0;
}
- </style>
- <script>
- function updateWhiteList() {
- var checkbox = document.getElementById("enableWhiteList"),
- textarea = document.getElementById("whiteListDomains"),
- domains = textarea.value.split('\n');
-
- textarea.disabled = !checkbox.checked;
- localStorage['enableWhiteList'] = JSON.stringify(checkbox.checked);
- localStorage['whiteListDomains'] = JSON.stringify(domains);
+ li {
+ list-style-type: none;
}
- </script>
+ </style>
+ <script src="options.js"></script>
</head>
<body>
<h1>Fast Flattr Options</h1>
<div id="whiteList">
- <input type="checkbox" onchange="updateWhiteList()" id="enableWhiteList" name="enableWhiteList" />
- <label for="enableWhiteList">Enable only for these websites (one address per line)</label>
- <textarea id="whiteListDomains" onkeyup="updateWhiteList()"></textarea>
+ <div>
+ <input type="checkbox" onchange="toggleWhiteList(this)" id="enableWhiteList" name="enableWhiteList" />
+ <label for="enableWhiteList">Enable only for these websites (one address per line)</label>
+ </div>
+ <ul id="whiteListDomains">
+ </ul>
+ <div id="addForm">
+ <input type="text" placeholder="flattr.com"/>
+ <button onclick="add()">Add</button>
+ </div>
</div>
</body>
<script>
- var checkbox = document.getElementById("enableWhiteList"),
- textarea = document.getElementById("whiteListDomains"),
- domains = JSON.parse(localStorage["whiteListDomains"] || "[]");
-
- checkbox.checked = JSON.parse(localStorage["enableWhiteList"] || "false");
- textarea.value = domains.join("\n");
+ main();
</script>
</html>
View
@@ -0,0 +1,92 @@
+var POPULAR_DOMAINS = [
+ "youtube.com",
+ "twitter.com",
+ "soundcloud.com",
+ "youtify.com",
+ "github.com",
+ "flattr.com"
+];
+
+function arrayContains(a, v) {
+ var i;
+
+ for (i = 0; i < a.length; i += 1) {
+ if (a[i] === v) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+function excludeFromArray(a, v) {
+ var i,
+ ret = [];
+
+ for (i = 0; i < a.length; i += 1) {
+ if (a[i] === v) {
+ continue;
+ }
+ ret.push(a[i]);
+ }
+
+ return ret;
+}
+
+function add() {
+ var elem = document.querySelector("#addForm input"),
+ domains = JSON.parse(localStorage["whiteListDomains"] || JSON.stringify(POPULAR_DOMAINS));
+
+ domains.push(elem.value);
+ localStorage['whiteListDomains'] = JSON.stringify(domains);
+
+ elem.value = '';
+
+ reloadList();
+}
+
+function toggleWhiteList(elem) {
+ localStorage['enableWhiteList'] = JSON.stringify(elem.checked);
+}
+
+function reloadList() {
+ document.getElementById("whiteListDomains").innerHTML = '';
+ renderDomains();
+}
+
+function removeFromWhiteList(domain) {
+ var domains = JSON.parse(localStorage['whiteListDomains'] || '[]');
+ domains = excludeFromArray(domains, domain);
+ localStorage['whiteListDomains'] = JSON.stringify(domains);
+ reloadList();
+}
+
+function createLi(domain, whiteListDomains) {
+ var li = document.createElement('li'),
+ removeButton = document.createElement('button'),
+ span = document.createElement('span');
+
+ removeButton.innerHTML = 'Remove';
+ removeButton.setAttribute('onclick', 'removeFromWhiteList("' + domain + '")');
+ span.innerHTML = domain;
+
+ li.appendChild(removeButton);
+ li.appendChild(span);
+
+ return li;
+}
+
+function renderDomains() {
+ var ul = document.getElementById("whiteListDomains"),
+ domains = JSON.parse(localStorage["whiteListDomains"] || JSON.stringify(POPULAR_DOMAINS)),
+ i;
+
+ for (i = 0; i < domains.length; i += 1) {
+ ul.appendChild(createLi(domains[i], domains));
+ }
+}
+
+function main() {
+ var checkbox = document.getElementById("enableWhiteList");
+ renderDomains();
+}

0 comments on commit bd065cf

Please sign in to comment.