Skip to content

Commit

Permalink
Replace innerHTML assignment in GenesiStudioParser
Browse files Browse the repository at this point in the history
Also refactor titleFormJson and findCoverImageUrl
  • Loading branch information
dteviot committed Sep 8, 2024
1 parent de3fc4a commit cf458e0
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions plugin/js/parsers/GenesiStudioParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ class GenesiStudioParser extends Parser{
}

appendContent(newDoc, content) {
var div = document.createElement("div");
div.innerHTML = content;
while (div.children.length > 0) {
newDoc.content.appendChild(div.children[0]);
}
let div = new DOMParser().parseFromString("<div>" + content + "</div>", "text/html")
.querySelector("div");
newDoc.content.append(div);
}

appendElement(newDoc, tag, text) {
Expand All @@ -58,11 +56,7 @@ class GenesiStudioParser extends Parser{
}

titleFromJson(json) {
let title = "";
title += util.isNullOrEmpty(json.nodes[1].data[json.nodes[1].data[1].chapter_title])
? ""
: `${json.nodes[1].data[json.nodes[1].data[1].chapter_title]}`;
return title;
return json.nodes[1].data[json.nodes[1].data[1].chapter_title] ?? "";
}

findContent(dom) {
Expand All @@ -74,12 +68,9 @@ class GenesiStudioParser extends Parser{
}

findCoverImageUrl(dom) {
let imgs = dom.querySelectorAll("img");
for (let i = 0; i < imgs.length; i++) {
if (imgs[i].src.search("https://api.genesistudio.com/storage/v1/render/image/public")==0) {
return imgs[i].src;
}
}
return null;
let img = [...dom.querySelectorAll("img")]
.map(i => i.src)
.filter(i => i.startsWith("https://api.genesistudio.com/storage/v1/render/image/public"))[0];
return img ?? null;
}
}

0 comments on commit cf458e0

Please sign in to comment.