Search code examples
cssimageembed

Embedding First External Image in Webpage


I want to embed the first image within a div of a page, e.g. the first image in .img-comic-container on dilbert.com. I tried this:

fetch("https://cors.now.sh/http://dilbert.com")
.then((res)=> res.text())
.then((res)=> {
  var parser = new DOMParser();
  var doc = parser.parseFromString(res, "text/html");
  var img = doc.querySelector('.img-comic-container .img-comic');
  document.querySelector("#dilbert").src = img.src;
});
<div id="place">
  <img id="dilbert" />
</div>

Without luck, though. Is there an alternative to cors.now.sh?


Solution

  • It seems like cors.now.sh is down or out of serivce. You can use an alternative https://www.scraperapi.com/.