Skip to content

Commit

Permalink
fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
bunglegrind committed Oct 2, 2021
1 parent 12a4cbb commit 2b8edd0
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions web-extension/extractHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var allowedTags = [
'math', 'maction', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot',
'mrow', 'ms', 'mspace', 'msqrt', 'mstyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'msgroup', 'mlongdiv', 'mscarries',
'mscarry', 'mstack', 'semantics'
// TODO ?
// TODO ?
// ,'form', 'button'

// TODO svg support ?
Expand All @@ -38,12 +38,12 @@ var tmpIdsToNewCssSTRING = {};
var supportedCss = [
'background-color',
'border',
'color',
'color',
'font',
'line-height',
'list-style',
'padding',
'text-align',
'text-align',
];
//////

Expand Down Expand Up @@ -73,7 +73,7 @@ function getImageSrc(srcTxt) {
filename: newImgFileName, // TODO name
data: getBase64ImgData(srcTxt)
});
} else {
} else {
allImages.push({
originalUrl: getImgDownloadUrl(srcTxt),
filename: newImgFileName, // TODO name
Expand Down Expand Up @@ -118,28 +118,31 @@ function extractSvgToImg($htmlObject) {

// replaces all iframes by divs with the same innerHTML content
function extractIFrames() {
let allIframes = document.getElementsByTagName('iframe')
let changeIFrames = []
let newDivs = []
for (let iFrame of allIframes) {
if (!iFrame.contentDocument || !iFrame.contentDocument.body) {
continue
}
let bodyContent = iFrame.contentDocument.body.innerHTML
let bbox = iFrame.getBoundingClientRect()
let newDiv = document.createElement('div')
newDiv.style.width = bbox.width
newDiv.style.height = bbox.height
newDiv.innerHTML = bodyContent
changeIFrames.push(iFrame)
newDivs.push(newDiv)
}
for (let i = 0; i < newDivs.length; i++) {
let newDiv = newDivs[i]
let iFrame = changeIFrames[i]
let iframeParent = iFrame.parentNode
iframeParent.replaceChild(newDiv, iFrame)
function editStyle(style, id) {
return style.split("\n").map(function (line) {
if(!/\{/.test(line)) {
return line;
}
return "#" + id + " " + line.replace("body", "");
}).join("\n");
}
let iframes = Array.from(document.querySelectorAll("iframe"));
const divs = iframes.map(function (iframe, index) {
const div = document.createElement("div");
div.id = "save-as-ebook-iframe-" + index;
if (!iframe.contentDocument || !iframe.contentDocument.body) {
return div;
}
let bbox = iframe.getBoundingClientRect();
div.style.width = bbox.width;
div.style.height = bbox.height;
div.innerHTML = iframe.contentDocument.body.innerHTML ?? "";
Array.from(div.querySelectorAll("style")).forEach(function (style) {
style.innerHTML = editStyle(style.innerHTML, div.id);
});
return div;
});
iframes.forEach((iframe, i) => iframe.parentNode.replaceChild(divs[i], iframe));
}

function preProcess($htmlObject) {
Expand Down Expand Up @@ -381,7 +384,7 @@ function extractCss(includeStyle, appliedStyles) {

function deferredAddZip(url, filename) {
let deferred = $.Deferred();
JSZipUtils.getBinaryContent(url, function(err, data) {
JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) {
// deferred.reject(err); TODO
console.log('Error:', err);
Expand All @@ -408,13 +411,13 @@ function deferredAddZip(url, filename) {
}
tmpGlobalContent = tmpGlobalContent.replace(oldFilename, filename)
}

extractedImages.push({
filename: filename,
// TODO - must be JSON serializable
data: base64ArrayBuffer(data)
});

deferred.resolve();
}
});
Expand Down

0 comments on commit 2b8edd0

Please sign in to comment.