-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotbicon.js
More file actions
88 lines (75 loc) · 2.35 KB
/
botbicon.js
File metadata and controls
88 lines (75 loc) · 2.35 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
class BotbIcon {
constructor() {}
/*
* returns all css rules in the botb stylesheet for given search query
* searches all selector names using string.includes
* when nothing found (or on error) returns empty object
*/
getStyle(searchQuery) {
let botbStylesheet = null;
for (let stylesheet of document.styleSheets) {
if (stylesheet.title == "botb") {
botbStylesheet = stylesheet;
}
}
if (!botbStylesheet) {
console.error("botb icons stylesheet could not be loaded");
return {};
}
let cssText = "";
let classes = botbStylesheet.rules || botbStylesheet.cssRules;
for (var x = 0; x < classes.length; x++) {
if (classes[x].selectorText.includes(searchQuery)) {
return classes[x].style;
}
}
return {};
}
/*
* searches botb css for matching icon name, then returns an object with
* width, height, x and y positions in the spritesheet
*/
getIcon(name) {
let style = this.getStyle(name);
return {
width: Math.abs(parseInt(style.width)),
height: Math.abs(parseInt(style.height)),
posX: Math.abs(parseInt(style.backgroundPositionX)),
posY: Math.abs(parseInt(style.backgroundPositionY))
};
}
random16icon() {
let botbStylesheet = null;
for (let stylesheet of document.styleSheets) {
if (stylesheet.title == "botb") {
botbStylesheet = stylesheet;
}
}
if (!botbStylesheet) {
console.error("botb icons stylesheet could not be loaded");
return {};
}
let cssText = "";
let classes = botbStylesheet.rules || botbStylesheet.cssRules;
/* scary infinite loop, but we should eventually land on a random
icon that is 16x16 */
while (true) {
let random = Math.floor(Math.random() * classes.length);
if (
classes[random].style.width === "16px" &&
classes[random].style.height === "16px"
) {
let style = classes[random].style;
let names = classes[random].selectorText.split("-");
return {
width: Math.abs(parseInt(style.width)),
height: Math.abs(parseInt(style.height)),
posX: Math.abs(parseInt(style.backgroundPositionX)),
posY: Math.abs(parseInt(style.backgroundPositionY)),
name: names[names.length - 1]
};
}
}
}
}
var botbIcon = new BotbIcon();