-
Notifications
You must be signed in to change notification settings - Fork 3
/
fontblocker.logic.js
94 lines (78 loc) · 1.88 KB
/
fontblocker.logic.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
fb = {
REPLACEMENT: 'Arial',
REPLACEMENTS: ['Arial', 'Verdana', 'Courier New', 'Consolas', 'Times New Roman', 'Georgia'],
UNBLOCKABLE: [
'Arial', // REPLACEMENT
'monospace', 'serif', 'sans-serif', 'fantasy', 'cursive', // Generics
'initial', 'inherit', 'unset', // CSS keywords
],
FONTS_PER_ITEM: 100,
storage: chrome.storage.sync || chrome.storage.local,
existsIn: function(font, list) {
for (var i=0; i<list.length; i++) {
if (fb.equals(font, list[i])) {
return list[i];
}
}
return false;
},
exists: function(font, callback) {
fb.get(function(list) {
callback(fb.existsIn(font, list), list);
});
},
get: function(callback) {
fb.storage.get('fonts', function(items) {
var list = items.fonts || [];
list.sort(function(a, b) {
return b.added - a.added;
});
callback(list);
});
},
add: function(font, callback) {
font.added || (font.added = Date.now());
fb.get(function(fonts) {
fonts.unshift(font);
fb.storage.set({fonts: fonts}, function() {
callback && callback();
});
});
},
remove: function(font, callback) {
fb.get(function(list) {
var keep = [];
for (var i = 0; i < list.length; i++) {
if (!fb.equals(font, list[i])) {
keep.push(list[i]);
}
}
fb.storage.set({fonts: keep}, function() {
callback && callback();
});
});
},
host: function(host, m) {
if (m = host.match(/\/\/([^/]+)\//)) {
host = m[1];
}
return host.replace(/^www\./, '');
},
equals: function(a, b) {
return a.host == b.host && a.name == b.name;
},
fontNamesForHost: function(host, callback) {
fb.get(function(list) {
var fonts = [];
var replacements = [];
for (var i=0; i<list.length; i++) {
var font = list[i];
if (font.host == '*' || font.host == host) {
fonts.push(font.name);
replacements.push(font.replacement);
}
}
callback(fonts, replacements);
});
}
};