I have a page that load a random image as background image. Something like this:
<!-- some other codes -->
<style>
.random-background {
height: 100px;
width: 100px;
background-image: url("https://server-domain.com/random.php");
}
</style>
<div class="random-background"></div>
<!-- some other codes -->
https://server-domain.com/random.php will return a random image every time it is called, so using AJAX to call the link is not an option. I am writing a Chrome extension, so I have no control on the server behaviour. Is there a way, using JavaScript, I can get the exact image used as background as the user see on screen?
It may seems to be a duplicated with this question. The difference is in this question, the target element is a div
instead of img
, so in the suggested solution to that question,
context.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
won't work. This will be the error message
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'. at :6:9