/
deCSS3.js
106 lines (85 loc) · 2.95 KB
/
deCSS3.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
99
100
101
102
103
104
105
106
var deCSS3 = {
init: function(){
var appendStyle = document.createElement('style');
appendStyle.id = "deCSS3";
appendStyle.textContent = this.addStyleBlock() + this.overrideRules();
document.body.appendChild(appendStyle);
},
addStyleBlock: function(){
// TODO: background-clip, background-origin, background-size?, animation
var rules = [
"border-radius:0!important;",
"box-shadow:none!important;",
"column-span:all!important;",
"text-shadow:none!important;",
"transform:none!important;",
"transition:none!important;"
];
return '* {' + rules.map(function(v){ return that.addPrefixes(v); }).join("") + '}';
},
addPrefixes: function(rule) {
var prefixes = ['-webkit-','-moz-','-o-', '-ms-', '-khtml-'];
return prefixes.join(rule) + rule;
},
/**
* TODO: write function that detects certain CSS3 rules and emptys out the rule to override
* e.g., rgba(0,0,0,0.4) can be overriden by rgba()
*
* @rules = multiplebg images, mediaqueries, background-size?, @font-face?
*/
overrideRules: function() {
var map = function ( arr, fn ) {
return [].map.call( arr, fn );
};
// Go through each stylesheet and return an array of new rules for each, then convert to string
return map( document.styleSheets, function ( stylesheet ) {
var newRules = "";
// Bail if there are no styles
if ( ! stylesheet.cssRules ) {
return;
}
// Find the rules we want to delete
map( stylesheet.cssRules, function ( rule, idx ) {
var ruleText = rule.cssText,
found = ruleText.match(/\@media|column-count|rgba|hsla|linear-gradient/g);
// Break early if there are no matches
if ( !found ) {
return;
}
if ( ~found.indexOf("@media") ) {
// newRule = newRule + "";
}
else {
newRule = currentRuleText;
if ( ~found.indexOf('column-count') ) {
newRule = newRule.replace(/column-count:(.*?)\;/g, 'column-count: 1;');
}
if ( ~found.indexOf('rgba') ) {
newRule = newRule.replace(/rgba\((.*?)\)\;/g, 'rgba();');
}
if ( ~found.indexOf('hsla') ) {
newRule = newRule.replace(/hsla\((.*?)\)\;/g, 'hsla();');
}
if ( ~found.indexOf('linear-gradient') ) {
newRule = newRule.replace(/linear-gradient\((.*?)\)\;/g, 'linear-gradient();');
}
}
// Add to rule list
newRules += newRule;
// add this rule index to list of rules to be deleted.
return idx;
})
// Reverse this so our indexes keep cool
.reverse()
// loop through and delete them
.forEach(function(element){
stylesheet.deleteRule(element);
});
// Return the set of new rules for this stylesheet
return newRules;
})
// Join all the rules as a string
.join("");
}
}
deCSS3.init();