Skip to content

Instantly share code, notes, and snippets.

@ClementParis016
Last active November 29, 2018 15:27
Show Gist options
  • Save ClementParis016/d1e2b4b768765fca1dfe4b8aa110a982 to your computer and use it in GitHub Desktop.
Save ClementParis016/d1e2b4b768765fca1dfe4b8aa110a982 to your computer and use it in GitHub Desktop.
Use original images URLs in Gmail
const REGEX = /https:\/\/.*\.googleusercontent\.com\/.*#(https?:\/\/.*)/i;
// Rewrite img source on <img> tags
Array.from(
document.querySelectorAll('img')
).forEach(img => {
const match = img.src.match(REGEX);
if (!match) {
return;
}
const [, originalURL] = match;
img.src = originalURL;
});
// Rewrite img source on CSS backgrounds
Array.from(
document.querySelectorAll('[style]')
).forEach(element => {
if (!element.style.backgroundImage && !element.style.background) {
return;
}
let match;
let fromBackground = false;
let fromBackgroundImage = false;
if (element.style.backgroundImage) {
match = element.style.backgroundImage.match(REGEX);
fromBackgroundImage = true;
}
if (!match) {
match = element.style.background.match(REGEX);
fromBackground = true;
}
if (!match) {
return;
}
const [input, originalURL] = match;
if (fromBackgroundImage) {
element.style.backgroundImage = element.style.backgroundImage
.replace(input, originalURL);
}
if (fromBackground) {
element.style.background = element.style.background
.replace(input, originalURL);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment