Skip to content

Instantly share code, notes, and snippets.

@hhkaos
Last active February 5, 2024 19:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhkaos/250bdde8ac443d03f791625623ab5e5e to your computer and use it in GitHub Desktop.
Save hhkaos/250bdde8ac443d03f791625623ab5e5e to your computer and use it in GitHub Desktop.
This script can be executed using the "Code by Zapier" app to get an image from a page - https://zapier.com/zapbook/code/
/*
You need to set an inputData param called 'link'
with the URL field you want to parse.
More info: https://zapier.com/help/code/#data-variables
*/
fetch(inputData.link)
.then(function(res) {
return res.text();
})
.then(function(body) {
var match = /<meta property=['"]og:image['"] content=['"](.*)['"] \/>/.exec(body);
var image = null;
image = match? match[1]: null;
if(!image){
match = /<link href=['"](.*)['"] rel=['"]image_src['"]\/>/.exec(body);
image = match? match[1]: null;
}
if(!image){
match = /<img src=['"](.*)['"] (.*)\>/.exec(body);
image = match? match[1]: null;
}
if(!image){
console.log("No image available");
image = 'http://geodevelopers.org/images/no-image.png';
}else{
if(image[0] === "/"){
var arr = inputData.link.split("/");
arr.splice(3);
image = arr.join("/") + image;
}
console.log(image);
}
var output = {image: image, rawHTML: body};
callback(null, output);
})
.catch(callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment