How to save a specified element using singlefile.getPageData? #36
Answered
by
gildas-lormeau
tangxiaoqi-tangxiao
asked this question in
Q&A
-
chrome.runtime.onMessage.addListener(async message => {
if (message.action === "save-page") {
const pageData = await singlefile.getPageData({
removeHiddenElements: true,
removeUnusedStyles: true,
removeUnusedFonts: true,
removeImports: true,
blockScripts: true,
blockAudios: true,
blockVideos: true,
compressHTML: true,
removeAlternativeFonts: true,
removeAlternativeMedias: true,
removeAlternativeImages: true,
groupDuplicateImages: true
});
console.log(pageData);
const linkElement = document.createElement("a");
linkElement.download = `${pageData.title}.html`;
linkElement.href = URL.createObjectURL(new Blob([pageData.content], { type: "text/html" }));
linkElement.click();
}
}); |
Beta Was this translation helpful? Give feedback.
Answered by
gildas-lormeau
Feb 4, 2025
Replies: 1 comment 1 reply
-
|
You could parse // ...
const doc = new DOMParser().parseFromString(pageData.content, "text/html");
doc.querySelectorAll("div.ads").forEach(element => element.remove()); // remove some elements
pageData.content = getDoctypeString(doc) + doc.documentElement.outerHTML;
linkElement.href = URL.createObjectURL(new Blob([pageData.content], { type: "text/html" }));
// ...
function getDoctypeString(doc) {
const docType = doc.doctype;
let docTypeString = "";
if (docType) {
docTypeString = "<!DOCTYPE " + docType.nodeName;
if (docType.publicId) {
docTypeString += " PUBLIC \"" + docType.publicId + "\"";
if (docType.systemId)
docTypeString += " \"" + docType.systemId + "\"";
} else if (docType.systemId)
docTypeString += " SYSTEM \"" + docType.systemId + "\"";
if (docType.internalSubset)
docTypeString += " [" + docType.internalSubset + "]";
docTypeString += "> ";
}
return docTypeString;
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
tangxiaoqi-tangxiao
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could parse
pageData.contentwith theDOMParserAPI, remove nodes from the resulting document, and serialize it into HTML just before callingnew Blob([pageData.content]). See below.