Skip to content

Instantly share code, notes, and snippets.

@valotvince
Last active December 10, 2021 05:11
Show Gist options
  • Save valotvince/06af55fdc8b50eed198e7a0dc1f0df6f to your computer and use it in GitHub Desktop.
Save valotvince/06af55fdc8b50eed198e7a0dc1f0df6f to your computer and use it in GitHub Desktop.
/**
* Override getter and setter for the img DOM element
* so we could change it in our mocked interface
*/
const createElement = document.createElement;
document.createElement = (...args) => {
const element = createElement.apply(document, args);
if (args[0] === 'img') {
let src = null;
Object.defineProperty(element, 'src', {
set: value => {
const parsedSrc = url.parse(value, false, true);
if (config.domains.find(domain => domain === parsedSrc.hostname)) {
window.xhrStore.push(value);
src = '/assets/images/mock/pixel.gif';
} else {
src = value;
}
},
get: () => src
});
}
return element;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment