Last active
February 5, 2024 19:21
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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