Skip to content

Instantly share code, notes, and snippets.

@SukkaW
Last active January 6, 2019 05:02
Show Gist options
  • Save SukkaW/75a38e665fa6920ed712f8dd5aa586a1 to your computer and use it in GitHub Desktop.
Save SukkaW/75a38e665fa6920ed712f8dd5aa586a1 to your computer and use it in GitHub Desktop.
var isSupportWebp = !![].map && document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0;
var WebpName = "!/format/webp";
if (isSupportWebp) {
for (var i = 0; i < document.querySelectorAll("div[data-bg]").length; i += 1) {
var originSrc = document.querySelectorAll("div[data-bg]")[i].getAttribute('data-bg'),
newSrc = originSrc.replace(/((.png)|(.jpg)|(.jpeg))/g, '$1' + WebpName);
document.querySelectorAll("div[data-bg]")[i].setAttribute('data-bg', newSrc)
}
for (var i = 0; i < document.querySelectorAll("div[data-src]").length; i += 1) {
var originSrc = document.querySelectorAll("div[data-src]")[i].getAttribute('data-src'),
newSrc = originSrc.replace(/((.png)|(.jpg)|(.jpeg))/g, '$1' + WebpName);
document.querySelectorAll("div[data-bg]")[i].setAttribute('data-bg', newSrc)
}
for (var i = 0; i < document.querySelectorAll("img[data-src]").length; i += 1) {
var originSrc = document.querySelectorAll("img[data-src]")[i].getAttribute('data-src'),
newSrc = originSrc.replace(/((.png)|(.jpg)|(.jpeg))/g, '$1' + WebpName);
document.querySelectorAll("img[data-src]")[i].setAttribute('data-src', newSrc)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment