Skip to content

Commit

Permalink
Add footnotes to bookswithqianya.com
Browse files Browse the repository at this point in the history
See: #1069
  • Loading branch information
dteviot committed Sep 18, 2023
1 parent dc07e4f commit 6dac31d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions plugin/js/parsers/BookswithqianyaParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,48 @@ class BookswithqianyaParser extends WordpressBaseParser{
util.removeChildElementsMatchingCss(element, "button");
super.removeUnwantedElementsFromContentElement(element);
}

preprocessRawDom(webPageDom) {
let content = this.findContent(webPageDom);
let footnotes = this.scriptElementsToFootnotes(webPageDom);
this.moveFootnotes(webPageDom, content, footnotes);
}

scriptElementsToFootnotes(dom) {
let indexedFootnotes = new Map();
[...dom.querySelectorAll("script")]
.map(s => s.textContent)
.filter(s => s.includes("toolTips('.classtoolTips"))
.forEach(s => indexedFootnotes.set(this.getId(s), this.extractFootnoteText(s)));

return this.getIdsUsedOnPage(dom)
.map(id => this.makeSpan(indexedFootnotes.get(id), dom));
}

getIdsUsedOnPage(dom) {
let extractId = (span) => [...span.classList]
.filter(s => s.startsWith("class"))[0];

return [...dom.querySelectorAll("span.tooltipsall")]
.map(extractId);
}

getId(script) {
return this.extractSubstring(script, "toolTips('.", ",");
}

makeSpan(content, dom) {
let span = dom.createElement("span");
span.textContent = content;
return span;
}

extractFootnoteText(content) {
return this.extractSubstring(content, "tt_store_content = \"", "\"; toolTips('");
}

extractSubstring(content, startTag, endTag) {
content = content.substring(content.indexOf(startTag) + startTag.length);
return content.substring(0, content.indexOf(endTag));
}
}

0 comments on commit 6dac31d

Please sign in to comment.