-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
99 lines (90 loc) · 3.68 KB
/
main.ts
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
///<reference path="typings/jquery/jquery.d.ts" />
///<reference path="typings/jquery.color/jquery.color.d.ts"/>
var transformTextColor = function (color:JQueryColor):JQueryColor {
var color = color.lightness((0.75 / (color.lightness() - 1.5) + 1.5) * 0.5);
return color.saturation(0.8 * color.saturation());
};
var transformColor = function (color:JQueryColor, threshold:number):JQueryColor {
if (color.saturation() > 0.5) {
return color.lightness(threshold + (1 - color.lightness()) * (1 - threshold)).saturation(0)
} else {
return color.lightness(threshold + (1 - color.lightness()) * (1 - threshold));
}
};
var processCSSRule = function (index:number, rule:CSSStyleRule) {
if (rule.style) {
$.each([
'backgroundColor',
'color',
'borderColor',
'borderTopColor',
'borderRightColor',
'borderLeftColor',
'borderBottomColor'
], function (index, attribute) {
if (rule.style[attribute] &&
rule.style[attribute] != 'inherit' &&
rule.style[attribute] != 'transparent' &&
rule.style[attribute] != 'currentColor') {
if (attribute == 'color') {
rule.style[attribute] = transformTextColor($.Color(rule.style[attribute]));
} else {
rule.style[attribute] = transformColor($.Color(rule.style[attribute]), 0.15);
}
}
});
if (rule.style.backgroundImage && (
rule.style.backgroundImage.indexOf('linear-gradient') > -1 ||
rule.style.backgroundImage.indexOf('radial-gradient') > -1 ||
rule.style.backgroundImage.indexOf('repeating-linear-gradient') > -1 ||
rule.style.backgroundImage.indexOf('repeating-radial-gradient') > -1
)) {
var gradient = rule.style.backgroundImage;
var colors = gradient.match(/rgba?\(.*?\)/g);
$.each(colors, function (index, color) {
gradient = gradient.replace(color, transformColor($.Color(color), 0.15));
});
console.log(rule.style.backgroundImage + ' - > ' + gradient);
rule.style.backgroundImage = gradient;
}
}
};
var processCSSStyleSheet = function (styleSheet:CSSStyleSheet) {
if (styleSheet.cssRules && styleSheet.cssRules.length > 0) {
$.each(styleSheet.cssRules, processCSSRule);
} else if (styleSheet.rules && styleSheet.rules.length > 0) {
$.each(styleSheet.rules, processCSSRule);
}
};
var extractDomain = function (url) {
var link = document.createElement('a');
link.href = url;
return link.hostname;
};
var insertStyleSheetAsStyle = function (url:string) {
$.ajax({url: url}).done(function (css) {
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
$('head')[0].appendChild(style);
});
};
new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
$.each(mutation.addedNodes, function (index, addedNode) {
if (addedNode.sheet) {
processCSSStyleSheet(addedNode.sheet);
}
});
});
}).observe($('head')[0], {childList: true});
$(document).ready(function () {
$.each(document.styleSheets, function (index, styleSheet) {
if (!styleSheet.cssRules && !styleSheet.rules && styleSheet.href) {
if (extractDomain(styleSheet.href) !== window.location.host) {
insertStyleSheetAsStyle(styleSheet.href);
}
}
processCSSStyleSheet(styleSheet);
});
});