Skip to content

Instantly share code, notes, and snippets.

@Illvili
Created December 19, 2014 08:12
Show Gist options
  • Save Illvili/8b3557c880cd325b8624 to your computer and use it in GitHub Desktop.
Save Illvili/8b3557c880cd325b8624 to your computer and use it in GitHub Desktop.
Fix srcset on Chrome http over TLS
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