-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
98 lines (86 loc) · 2.35 KB
/
popup.js
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
let noMoHistoryData = JSON.parse(localStorage.getItem('noMoHistory')) || [];
let enabled = JSON.parse(localStorage.getItem('enabled'));
const domainSection = $('.domain-section');
let dataSet = new Set(noMoHistoryData);
let numInputs = 0;
let hidden = true;
var port = chrome.extension.connect({
name: 'Switch'
});
if (noMoHistoryData.length) {
dataSet.forEach(val => {
createInput(val);
});
}
if (enabled === true) {
$('#switch').attr('checked', true);
}
// toggle between eye icons and hidden text input
$('.eye').click(() => {
if (hidden) {
$('.eye > i')
.removeClass('fa-eye-slash')
.addClass('fa-eye');
$('.text-hidden').removeClass('text-hidden');
hidden = false;
} else {
$('.eye > i')
.removeClass('fa-eye')
.addClass('fa-eye-slash');
$('.input').addClass('text-hidden');
hidden = true;
}
});
$('#save-btn').click(() => {
const inputs = $('.input').toArray();
inputs.forEach(input => {
const value = $(input).val();
if (isValidDomain(value)) {
dataSet.add(value);
} else {
console.log('not a valid domain');
}
});
localStorage.setItem('noMoHistory', JSON.stringify([...dataSet.values()]));
window.close();
});
$('#add-btn').click(() => {
createInput();
});
$('#switch').change(event => {
const message = event.target.checked ? 'ENABLE' : 'DISABLE';
port.postMessage(message);
localStorage.setItem('enabled', event.target.checked);
});
function createInput(value = '') {
domainSection.prepend(`
<div class="row input-row">
<label class="label" for="">Domain:</label>
<input id="input-${numInputs}" class="${
hidden ? 'text-hidden' : ''
} input" placeholder="domain.com" value="${value}">
<div class="trash">
<i id="trash-${numInputs}" class="fas fa-trash fa-lg delete"></i>
</div>
</div>
`);
addDeleteListener(`#trash-${numInputs}`, `#input-${numInputs}`);
numInputs++;
}
function deleteInput(value, inputId) {
dataSet.delete(value);
$(inputId)
.closest('.input-row')
.remove();
}
function addDeleteListener(trashId, inputId) {
$(trashId).click(() => {
const inputValue = $(inputId).val();
deleteInput(inputValue, inputId);
});
}
function isValidDomain(domain) {
if (!domain) return false;
var re = /^(?!:\/\/)([a-zA-Z0-9-]+\.){0,5}[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z]{2,64}?$/gi;
return re.test(domain);
}