-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbadger.js
More file actions
149 lines (125 loc) · 4.4 KB
/
badger.js
File metadata and controls
149 lines (125 loc) · 4.4 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// There's probably a better way for this but I cbf
// http://cwestblog.com/2013/02/26/javascript-string-prototype-matchall/
// Thanks Chris West.
String.prototype.matchAll = function(regexp) {
var matches = [];
this.replace(regexp, function() {
var arr = ([]).slice.call(arguments, 0);
var extras = arr.splice(-2);
arr.index = extras[0];
arr.input = extras[1];
matches.push(arr);
});
return matches.length ? matches : null;
};
loadFontAwesome = function() {
link = document.createElement( "link" );
link.href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css";
link.type = "text/css";
link.rel = "stylesheet";
link.media = "screen,print";
document.getElementsByTagName( "head" )[0].appendChild( link );
};
awesomeIcon = function(iconName) {
icon = document.createElement("i");
icon.classList.add("fa");
icon.classList.add("fa-" + iconName);
return icon;
};
newBadge = function(fieldName, text, icon, fontColor, badgeColor, fontSize, fontWeight) {
badge = document.createElement("div");
badge.classList.add("pointy-badge");
badge.classList.add("badge");
badge.style.color = fontColor;
if (badgeColor === "func") {
var hue = 0;
var md5hash = md5(text);
for (var i = 0; i < 5; i++) { hue += md5hash.charCodeAt(i) - 65; }
hue = hue % 360;
sat = hue % 32;
lit = hue % 28;
badge.style.backgroundColor = ("hsl(" + hue.toString() + ", " + (sat + 38).toString() + "%, " + (lit + 45).toString() + "%)");
} else {
badge.style.backgroundColor = badgeColor;
}
// Dirty Hack to test JS
if (fieldName === "dependency") {
console.log('Adding event listeners for badge with content ' + text);
badge.zIndex = 100000;
var target = document.getElementById(text);
if (target) {
badge.addEventListener("mouseenter", function(event) {
target = document.getElementById(text);
target.style.boxShadow = "0px 0px 10em 2em #e73030";
});
badge.addEventListener("mouseleave", function(event) {
target = document.getElementById(text);
target.style.zIndex = "0";
target.style.boxShadow = "";
});
} else {
badge.style.backgroundColor = '#55AA55';
}
}
badge.style.fontSize = fontSize;
badge.style.fontWeight = fontWeight;
badge.innerHTML = awesomeIcon(icon).outerHTML;
badge.innerHTML += " " + text;
return badge;
};
refreshCycle = function(meta, config) {
for (i = 0; i < meta.lists.length; i++) {
meta.lists[i].refresh(config);
}
setTimeout(function() {
refreshCycle(meta);
}, 2000);
};
loadMeta = function(meta) {
meta.lists = [];
var lists = [];
var listNodes = Array.prototype.slice.call(document.getElementsByClassName("list"));
listNodes.forEach(function(listNode) {
if (listNode.classList.contains("add-list")) {
return;
}
var list = new List(0, listNode, []);
var cardNodes = Array.prototype.slice.call(list.node.getElementsByClassName("list-card"));
cardNodes.forEach(function(cardNode) {
list.cards.push(new Card(cardNode));
});
meta.lists.push(list);
});
};
start = function(config) {
loadFontAwesome();
var meta = {
lists: []
};
loadMeta(meta);
for (i = 0; i < meta.lists.length; i++) {
meta.lists[i].process(config);
}
refreshCycle(meta, config);
};
storageCallback = function(result) {
if (result.badgerConfig) {
// Calling it here messes up the console output for errors... :\
config = result.badgerConfig;
config = JSON.parse(config);
for (var i = 0; i < config.listBadges.length; i++) {
// TODO: Perhaps there is a way to gain similar functionality without eval.
config.listBadges[i].reduce = eval("(" + config.listBadges[i].reduce + ")");
}
for (i = 0; i < config.cardBadges.length; i++) {
config.cardBadges[i].regex = new RegExp(config.cardBadges[i].regex, "g");
}
start(config);
}
return;
};
loadConfig = function() {
console.log("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
chrome.storage.sync.get("badgerConfig", storageCallback);
};
window.onload = loadConfig;