-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.js
106 lines (84 loc) · 2.26 KB
/
options.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
99
100
101
102
103
104
105
106
$(function(){
var arDomains = [];
var addURL = function(){
var patterns = {
exact: /[^a-zA-Z0-9\-\.]|^[\.\-]|[\.\-]$/,
starts: /[^a-zA-Z0-9\-\.]|^[\.\-]/,
contains: /[^a-zA-Z0-9\-\.]/,
ends: /[^a-zA-Z0-9\-\.]|[\.\-]$/
};
var match = $('input[name=match]:checked').val();
var domain = $('#inpAddSite').val();
if(domain.trim().length == 0)
return;
if(domain.search(patterns[match]) > -1){
$('#dvMessage').text("Invalid characters in domain name.");
$('#dvMessage').show();
$('#dvMessage').fadeOut({duration: 2400});
return;
}
arDomains.push({match: match, domain: domain});
arDomains.sort(function(a, b){
return a.domain > b.domain ? 1 : -1;
});
$('#selSites').empty();
loadDomains();
$('#inpAddSite').val("");
saveOptions();
};
var loadDomains = function(){
$('#selSites').append($('<option>').val(".w3schools.com").data("match", "ends").text("*.w3schools.com").prop("disabled", true));
$(arDomains).each(function(){
var displayURL = this.domain;
switch(this.match){
case "starts":
displayURL = displayURL + "*";
break;
case "contains":
displayURL = "*" + displayURL + "*";
break;
case "ends":
displayURL = "*" + displayURL;
break;
}
$('#selSites').append($('<option>').val(this.domain).data("match", this.match).text(displayURL));
});
};
var removeURL = function(){
$('#selSites').children(":selected").each(function(){
$option = $(this);
arDomains = $.grep(arDomains, function(item){
return item.match != $option.data("match") || item.domain != $option.val();
});
});
$('#selSites').children(":selected").remove();
saveOptions();
};
var saveOptions = function(){
localStorage["blockedDomains"] = JSON.stringify(arDomains);
};
var getOptions = function(){
if(localStorage["blockedDomains"] == null){
loadDomains();
return;
}
try{
arDomains = JSON.parse(localStorage["blockedDomains"]);
}catch(e){
arDomains = [];
}
loadDomains();
};
//Event handlers
$("#frmOptions").submit(function(){
event.preventDefault();
event.stopPropagation();
});
$("#btnAdd").click(addURL);
$('#inpAddSite').keypress(function(e){
if(e.which == 13)
addURL();
});
$("#btnRemove").click(removeURL);
getOptions();
});