]>
Commit | Line | Data |
---|---|---|
6293bf85 MF |
1 | // Written by Mike Frysinger <vapier@gmail.com>. |
2 | // Released into the public domain. | |
3 | ||
4 | /* | |
5 | * This code is all very simple/dumb. If we want to ever support more | |
6 | * complicated theme logic, then it should be thrown away entirely. | |
7 | */ | |
8 | ||
9 | var curr_theme; | |
10 | ||
11 | function _set_theme(txt, fg, bg, a) { | |
12 | var b = $$('body'); | |
13 | b.style.color = fg; | |
14 | b.style.backgroundColor = bg; | |
15 | ||
16 | // This gets a bit tricky as we want to update the style sheet | |
17 | // to quickly apply to all <a> tags. | |
18 | var s, sheet, sheets, r, rule, rules; | |
19 | sheets = document.styleSheets; | |
20 | for (s = 0; s < sheets.length; ++s) { | |
21 | sheet = sheets[s]; | |
22 | rules = sheet.cssRules; | |
23 | for (r = 0; r < rules.length; ++r) { | |
24 | rule = rules[r]; | |
25 | if (rule.selectorText == 'a') { | |
26 | rule.style.color = a; | |
27 | break; | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
32 | // We can't set UTF8 text, or set HTML entities directly. Ugh. | |
33 | var span = document.createElement('span'); | |
34 | span.innerHTML = txt; | |
35 | $$('input[name=theme]').value = span.innerText; | |
36 | } | |
37 | ||
38 | function set_theme(name) { | |
39 | var themes = { | |
40 | 'light': ['☀', 'black', 'white', 'black'], | |
41 | 'dark': ['☼', 'white', 'black', 'grey'] | |
42 | }; | |
43 | curr_theme = name; | |
44 | _set_theme.apply(this, themes[name]); | |
45 | chrome.storage.local.set({'theme': curr_theme}); | |
46 | } | |
47 | ||
48 | function toggle_theme() { | |
49 | if (curr_theme == 'light') | |
50 | set_theme('dark'); | |
51 | else | |
52 | set_theme('light'); | |
53 | } |