itty-bitty/docs/render/parse.js

43 lines
990 B
JavaScript
Raw Normal View History

2022-05-12 05:35:27 +00:00
// let title = params.title
2022-05-17 15:37:07 +00:00
parent.postMessage({title:"Parsing Content..."}, "*");
2022-06-09 05:53:37 +00:00
document.body.appendChild(document.createTextNode("• • •"))
2022-05-17 15:37:07 +00:00
2022-05-12 05:35:27 +00:00
const parser = new DOMParser();
const doc = parser.parseFromString(params.body, "text/html");
2022-06-09 05:53:37 +00:00
2022-05-17 15:37:07 +00:00
let ldjson = doc.querySelector('script[type="application/ld+json"]')
if (ldjson) {
2022-06-09 05:53:37 +00:00
cleanRecipe(ldjson)
2022-05-17 15:37:07 +00:00
} else {
alert("No recipe found")
2022-06-09 05:53:37 +00:00
}
function cleanRecipe(ldjson) {
try {
var json = JSON.parse(ldjson.innerText); //JSON.parse(data);
if (json["@type"] != "Recipe") {
json = (json["@graph"] ?? json).find((item) => item["@type"] == "Recipe")
}
delete json.review;
delete json.video;
let f = new FileReader();
f.onload = function(e) {
parent.postMessage({replaceURL:e.target.result, compressURL:"true"}, "*");
};
f.readAsDataURL(new Blob([JSON.stringify(json)],{type : 'application/ld+json;charset=utf-8'}));
} catch (e) {
console.debug("Data", e, {ldjson});
}
}