forked from joemiller/Brief
-
Notifications
You must be signed in to change notification settings - Fork 1
/
retina.js
90 lines (82 loc) · 2.13 KB
/
retina.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
/**
* @fileoverview Skype Retina
* @author Thibault Martin-Lagardette
* @version 1.0
*/
function changeSKCGPropertyToUseRetina(el, property, isPropertyCSS, useRetina)
{
var src;
if (isPropertyCSS === false) {
src = el[property];
}
else {
src = $(el).css(property);
}
if (typeof src === "undefined") {
console.log("Could not find property: " + property);
return ;
}
var newSrc = null;
if (src.search("skcg") !== -1) {
oldRegexp = new RegExp("retina=" + (useRetina === 1 ? 0 : 1));
if (src.search(oldRegexp) !== -1) {
newSrc = src.replace(oldRegexp, "retina=" + useRetina);
}
else {
console.log("Could not find retina in: " + src);
}
}
else {
if (useRetina === 1 && src.search("@2x.png") === -1) {
newSrc = src.replace(/\.png/, "@2x.png");
}
else if (useRetina === 0 && src.search("@2x.png") !== -1) {
newSrc = src.replace(/@2x\.png/, ".png");
}
else {
console.log("Could not find how to switch retinaness of: " + src);
}
}
if (newSrc !== null) {
if (isPropertyCSS === true) {
$(el).css(property, newSrc);
}
else {
el[property] = newSrc;
}
}
}
function updateImagesForCurrentScaleFactor(scaleFactor) {
scaleFactor = scaleFactor || window.devicePixelRatio;
if (scaleFactor >= 2) {
$("img.avatar").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 1);
});
$("img.icon").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 1);
});
$(".flag").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 1);
});
$(".emoticon").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "background-image", true, 1);
});
}
else {
$("img.avatar").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 0);
});
$("img.icon").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 0);
});
$(".flag").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "src", false, 0);
});
$(".emoticon").each(function(idx, el) {
changeSKCGPropertyToUseRetina(el, "background-image", true, 0);
});
}
}
$(function() {
updateImagesForCurrentScaleFactor();
});