Skip to content

Instantly share code, notes, and snippets.

@bvodola
Created December 17, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvodola/642a18789fe075634d23d9b9e0f1f920 to your computer and use it in GitHub Desktop.
Save bvodola/642a18789fe075634d23d9b9e0f1f920 to your computer and use it in GitHub Desktop.
Get Recipe Schema from Website
function getRecipeSchema(schema) {
if(!schema) {
const nodes = document.querySelectorAll('script[type="application/ld+json"]');
let returnData;
nodes.forEach(n => {
const childSchema = JSON.parse(n.innerText);
const data = getRecipeSchema(childSchema);
if(data) returnData = data;
});
return returnData;
}
if(schema['@type'] && schema['@type'] === 'Recipe') return schema;
if(schema['@graph']) {
const recipeSchema = schema['@graph'].find(s => s['@type'] === 'Recipe');
if(recipeSchema) return getRecipeSchema(recipeSchema);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment