-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoggle-styles.js
64 lines (53 loc) · 1.75 KB
/
toggle-styles.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
(function() {
'use strict';
var attributeStorage = 'data-css-storage';
var attributeDisabled = 'data-css-disabled';
var elements = [];
var disabled = areStylesDisabled();
if (!disabled) {
// disable styles
elements = document.querySelectorAll('[style], link, style');
[].slice.call(elements).forEach(function(el) {
if (el.tagName === 'STYLE') {
saveToStorage(el, el.innerHTML);
el.innerHTML = '';
} else if (el.tagName === 'LINK') {
saveToStorage(el, '');
el.disabled = true;
} else {
saveToStorage(el, el.style.cssText);
el.style.cssText = '';
}
});
} else {
// re-enable styles
elements = document.querySelectorAll('[' + attributeStorage + ']');
[].slice.call(elements).forEach(function(el) {
if (el.tagName === 'STYLE') {
el.innerHTML = getFromStorage(el);
} else if (el.tagName === 'LINK') {
el.disabled = false;
} else {
el.style.cssText = getFromStorage(el);
}
});
}
function saveToStorage(el, data) {
el.setAttribute(attributeStorage, data);
}
function getFromStorage(el) {
var data = el.getAttribute(attributeStorage);
el.removeAttribute(attributeStorage);
return data;
}
function areStylesDisabled() {
var el = document.body;
var disabled = el.hasAttribute(attributeDisabled);
if (disabled) {
el.removeAttribute(attributeDisabled);
} else {
el.setAttribute(attributeDisabled, '');
}
return disabled;
}
}());