-
Notifications
You must be signed in to change notification settings - Fork 8
/
styleswitcher.js
98 lines (90 loc) · 3.03 KB
/
styleswitcher.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
function LXSH_GetAllStyles() {
var elements = document.getElementsByTagName('link');
var selected = new Array();
for (var i = elements.length - 1; i >= 0; i--)
if (elements[i].getAttribute('rel').indexOf('stylesheet') >= 0) {
var href = elements[i].getAttribute('href');
if (href && href.indexOf('http://peterodding.com/code/lua/lxsh/styles/') == 0)
selected.push(elements[i]);
}
return selected;
}
function LXSH_GetActiveStyle() {
var elements = LXSH_GetAllStyles();
for (var i = 0; i < elements.length; i++)
if (!elements[i].disabled)
return elements[i].getAttribute('title');
}
function LXSH_ChangeStyle(newstyle) {
var head = document.getElementsByTagName('head')[0];
var elements = LXSH_GetAllStyles();
for (var i = elements.length - 1; i >= 0; i--) {
var title = elements[i].getAttribute('title');
elements[i].rel = 'stylesheet';
elements[i].disabled = (title != newstyle);
}
}
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else
expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
window.onDomReady = DomReady;
function DomReady(fn) {
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", fn, false);
else
document.onreadystatechange = function(){readyState(fn)}
}
function readyState(fn) {
if (document.readyState == "interactive") fn();
}
function LXSH_Init() {
var styles = LXSH_GetAllStyles();
var active = readCookie('lxsh_colors');
if (active != null)
LXSH_ChangeStyle(active);
else
active = LXSH_GetActiveStyle();
var elements = document.getElementsByTagName('pre');
for (var i = elements.length - 1; i >= 0; i--)
if (elements[i].getAttribute('class').indexOf('sourcecode') >= 0) {
var select = document.createElement('select');
select.style.cssFloat = 'right';
select.style.padding = '5px';
select.onchange = function() {
LXSH_ChangeStyle(select.value);
createCookie('lxsh_colors', select.value, 365);
}
var group = document.createElement('optgroup');
group.label = 'Colors:';
select.appendChild(group);
for (var j = styles.length - 1; j >= 0; j--) {
var option = document.createElement('option');
var name = styles[j].getAttribute('title');
option.innerHTML = name;
option.title = 'Select the "' + name + '" color scheme';
if (name == active)
option.selected = true;
group.appendChild(option);
}
elements[i].insertBefore(select, elements[i].firstChild);
}
}
window.onDomReady(LXSH_Init);