Created
December 19, 2014 08:12
-
-
Save Illvili/8b3557c880cd325b8624 to your computer and use it in GitHub Desktop.
Fix srcset on Chrome http over TLS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ('https:' == self.location.protocol && /chrome/i.test(window.navigator.userAgent)) { | |
var srcset_elements = document.querySelectorAll('[srcset]'); | |
if (!!window.devicePixelRatio && window.devicePixelRatio != 1) { | |
Array.prototype.forEach.call(srcset_elements, function (el) { | |
var prop = el.getAttribute('srcset'); | |
var src_list = prop.split(/,\s*/); | |
var ratio = 0; | |
var src = ''; | |
src_list.forEach(function(item) { | |
var tmp = /^(.*?)\s*(\d*\.?\d+)x$/.exec(item); | |
if ( | |
!!tmp && 3 == tmp.length && | |
+tmp[2] <= window.devicePixelRatio && ratio <= +tmp[2] | |
) { | |
src = tmp[1]; | |
} | |
}); | |
if (src) { | |
el.setAttribute('src', src); | |
} | |
}); | |
} | |
Array.prototype.forEach.call(srcset_elements, function (el) { | |
el.removeAttribute('srcset'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment