mirror of
https://github.com/alexadam/save-as-ebook.git
synced 2025-09-10 09:24:49 +00:00
misc updates WIP
This commit is contained in:
parent
74673b5449
commit
b72d7934dd
2 changed files with 55 additions and 43 deletions
|
@ -10,7 +10,14 @@ var allowedTags = [
|
||||||
'math', 'maction', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot',
|
'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',
|
'mrow', 'ms', 'mspace', 'msqrt', 'mstyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'msgroup', 'mlongdiv', 'mscarries',
|
||||||
'mscarry', 'mstack', 'semantics'
|
'mscarry', 'mstack', 'semantics'
|
||||||
|
|
||||||
|
// TODO ?
|
||||||
|
// ,'form', 'button'
|
||||||
|
|
||||||
|
// TODO svg support ?
|
||||||
|
// , 'svg', 'g', 'path', 'line', 'circle', 'text'
|
||||||
];
|
];
|
||||||
|
// const svgTags = ['svg', 'g', 'path', 'line', 'circle', 'text']
|
||||||
var mathMLTags = [
|
var mathMLTags = [
|
||||||
'math', 'maction', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mmultiscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot',
|
'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',
|
'mrow', 'ms', 'mspace', 'msqrt', 'mstyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'msgroup', 'mlongdiv', 'mscarries',
|
||||||
|
@ -48,11 +55,13 @@ function getImageSrc(srcTxt) {
|
||||||
|
|
||||||
// TODO - convert <imgs> with svg sources to jpeg
|
// TODO - convert <imgs> with svg sources to jpeg
|
||||||
|
|
||||||
|
// TODO https://preview.redd.it/oyj35v4jrri41.png?width=960&crop=smart&auto=webp&s=aa6dac5580038f489faa707093710f427ec83b20
|
||||||
|
|
||||||
var fileExtension = getFileExtension(srcTxt);
|
var fileExtension = getFileExtension(srcTxt);
|
||||||
if (fileExtension === '') {
|
if (fileExtension === '') {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
var newImgFileName = 'img-' + (Math.floor(Math.random()*1000000*Math.random()*100000)) + '.' + fileExtension;
|
var newImgFileName = 'img-' + generateRandomNumber(true) + '.' + fileExtension;
|
||||||
|
|
||||||
var isB64Img = isBase64Img(srcTxt);
|
var isB64Img = isBase64Img(srcTxt);
|
||||||
if (isB64Img) {
|
if (isB64Img) {
|
||||||
|
@ -78,7 +87,6 @@ function extractMathMl($htmlObject) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// tested
|
// tested
|
||||||
// TODO
|
|
||||||
function extractCanvasToImg($htmlObject) {
|
function extractCanvasToImg($htmlObject) {
|
||||||
$htmlObject.find('canvas').each(function (index, elem) {
|
$htmlObject.find('canvas').each(function (index, elem) {
|
||||||
try {
|
try {
|
||||||
|
@ -92,15 +100,15 @@ function extractCanvasToImg($htmlObject) {
|
||||||
|
|
||||||
// tested
|
// tested
|
||||||
function extractSvgToImg($htmlObject) {
|
function extractSvgToImg($htmlObject) {
|
||||||
var serializer = new XMLSerializer();
|
let serializer = new XMLSerializer();
|
||||||
$htmlObject.find('svg').each(function (index, elem) {
|
$htmlObject.find('svg').each(function (index, elem) {
|
||||||
try {
|
// add width & height because the result image was too big
|
||||||
var svgXml = serializer.serializeToString(elem);
|
let bbox = elem.getBoundingClientRect()
|
||||||
var imgSrc = 'data:image/svg+xml;base64,' + window.btoa(svgXml);
|
let newWidth = bbox.width
|
||||||
$(elem).replaceWith('<img src="' + imgSrc + '">' + '</img>');
|
let newHeight = bbox.height
|
||||||
} catch (e) {
|
let svgXml = serializer.serializeToString(elem);
|
||||||
console.log(e)
|
var imgSrc = 'data:image/svg+xml;base64,' + window.btoa(svgXml);
|
||||||
} finally {}
|
$(elem).replaceWith('<img src="' + imgSrc + '" width="'+newWidth+'" height="'+newHeight+'">' + '</img>');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,20 +129,12 @@ function preProcess($htmlObject) {
|
||||||
function parseHTML(rawContentString) {
|
function parseHTML(rawContentString) {
|
||||||
allImages = [];
|
allImages = [];
|
||||||
extractedImages = [];
|
extractedImages = [];
|
||||||
var dirty = null;
|
let results = '';
|
||||||
|
let lastFragment = '';
|
||||||
|
let lastTag = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$wdirty = $(rawContentString);
|
HTMLParser(rawContentString, {
|
||||||
|
|
||||||
preProcess($wdirty);
|
|
||||||
|
|
||||||
dirty = $wdirty.html();
|
|
||||||
|
|
||||||
var results = '';
|
|
||||||
var lastFragment = '';
|
|
||||||
var lastTag = '';
|
|
||||||
|
|
||||||
HTMLParser(dirty, {
|
|
||||||
start: function(tag, attrs, unary) {
|
start: function(tag, attrs, unary) {
|
||||||
lastTag = tag;
|
lastTag = tag;
|
||||||
if (allowedTags.indexOf(tag) < 0) {
|
if (allowedTags.indexOf(tag) < 0) {
|
||||||
|
@ -150,6 +150,12 @@ function parseHTML(rawContentString) {
|
||||||
tmpAttrsTxt += ' src="' + tmpSrc + '"';
|
tmpAttrsTxt += ' src="' + tmpSrc + '"';
|
||||||
} else if (attrs[i].name === 'data-class') {
|
} else if (attrs[i].name === 'data-class') {
|
||||||
tmpAttrsTxt += ' class="' + attrs[i].value + '"';
|
tmpAttrsTxt += ' class="' + attrs[i].value + '"';
|
||||||
|
} else if (attrs[i].name === 'width') {
|
||||||
|
// used when converting svg to img - the result image was too big
|
||||||
|
tmpAttrsTxt += ' width="' + attrs[i].value + '"';
|
||||||
|
} else if (attrs[i].name === 'height') {
|
||||||
|
// used when converting svg to img - the result image was too big
|
||||||
|
tmpAttrsTxt += ' height="' + attrs[i].value + '"';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (tmpSrc === '') {
|
if (tmpSrc === '') {
|
||||||
|
@ -216,19 +222,20 @@ function parseHTML(rawContentString) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO - (re)move
|
||||||
results = results.replace(/ /gi, ' ');
|
results = results.replace(/ /gi, ' ');
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Error:', e);
|
console.log('Error:', e);
|
||||||
return force(dirty, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContent(htmlContent) {
|
function getContent(htmlContent) {
|
||||||
try {
|
try {
|
||||||
|
preProcess($('body'))
|
||||||
var tmp = document.createElement('div');
|
var tmp = document.createElement('div');
|
||||||
tmp.appendChild(htmlContent.cloneNode(true));
|
tmp.appendChild(htmlContent.cloneNode(true));
|
||||||
var tmpHtml = '<div>' + tmp.innerHTML + '</div>';
|
var tmpHtml = '<div>' + tmp.innerHTML + '</div>';
|
||||||
|
@ -256,21 +263,6 @@ function getSelectedNodes() {
|
||||||
|
|
||||||
/////
|
/////
|
||||||
|
|
||||||
function jsonToCss(jsonObj) {
|
|
||||||
var keys = Object.keys(jsonObj);
|
|
||||||
var result = '';
|
|
||||||
for (var i = 0; i < keys.length; i++) {
|
|
||||||
var tmpJsonObj = jsonObj[keys[i]];
|
|
||||||
var tmpKeys = Object.keys(tmpJsonObj);
|
|
||||||
result += '.' + keys[i] + ' {';
|
|
||||||
for (var j = 0; j < tmpKeys.length; j++) {
|
|
||||||
result += tmpKeys[j] + ':' + tmpJsonObj[tmpKeys[j]] + ';';
|
|
||||||
}
|
|
||||||
result += '} ';
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractCss(includeStyle, appliedStyles) {
|
function extractCss(includeStyle, appliedStyles) {
|
||||||
if (includeStyle) {
|
if (includeStyle) {
|
||||||
$('body').find('*').each((i, pre) => {
|
$('body').find('*').each((i, pre) => {
|
||||||
|
@ -288,19 +280,17 @@ function extractCss(includeStyle, appliedStyles) {
|
||||||
if (!classNames) {
|
if (!classNames) {
|
||||||
classNames = pre.getAttribute('id');
|
classNames = pre.getAttribute('id');
|
||||||
if (!classNames) {
|
if (!classNames) {
|
||||||
classNames = pre.tagName + '-' + Math.floor(Math.random()*100000);
|
classNames = pre.tagName + '-' + generateRandomNumber();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let tmpName = cssClassesToTmpIds[classNames];
|
let tmpName = cssClassesToTmpIds[classNames];
|
||||||
let tmpNewCss = tmpIdsToNewCss[tmpName];
|
let tmpNewCss = tmpIdsToNewCss[tmpName];
|
||||||
if (!tmpName) {
|
if (!tmpName) {
|
||||||
// TODO - collision between class names when multiple pages
|
// TODO - collision between class names when multiple pages
|
||||||
// rename 'class-' to 'c'
|
tmpName = generateRandomTag(2) + i
|
||||||
tmpName = 'c' + i // Math.floor(Math.random()*100000);
|
|
||||||
cssClassesToTmpIds[classNames] = tmpName;
|
cssClassesToTmpIds[classNames] = tmpName;
|
||||||
}
|
}
|
||||||
if (!tmpNewCss) {
|
if (!tmpNewCss) {
|
||||||
// var style = window.getComputedStyle(pre);
|
|
||||||
tmpNewCss = {};
|
tmpNewCss = {};
|
||||||
|
|
||||||
for (let cssTagName of supportedCss) {
|
for (let cssTagName of supportedCss) {
|
||||||
|
@ -398,7 +388,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
title: tmpTitle,
|
title: tmpTitle,
|
||||||
baseUrl: getCurrentUrl(),
|
baseUrl: getCurrentUrl(),
|
||||||
styleFileContent: styleFile,
|
styleFileContent: styleFile,
|
||||||
styleFileName: 'style' + Math.floor(Math.random() * 100000) + '.css',
|
styleFileName: 'style' + generateRandomNumber() + '.css',
|
||||||
images: extractedImages,
|
images: extractedImages,
|
||||||
content: tmpContent
|
content: tmpContent
|
||||||
};
|
};
|
||||||
|
|
|
@ -320,6 +320,13 @@ function generateRandomTag(tagLen) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateRandomNumber(bigNr) {
|
||||||
|
if (bigNr) {
|
||||||
|
return Math.floor(Math.random()*1000000*Math.random()*1000000)
|
||||||
|
}
|
||||||
|
return Math.floor(Math.random()*1000000)
|
||||||
|
}
|
||||||
|
|
||||||
function removeSpecialChars(text) {
|
function removeSpecialChars(text) {
|
||||||
// FIXME remove white spaces ?
|
// FIXME remove white spaces ?
|
||||||
return text.replace(/\//g, '-')
|
return text.replace(/\//g, '-')
|
||||||
|
@ -350,4 +357,19 @@ function getPageTitle(title) {
|
||||||
return 'ebook';
|
return 'ebook';
|
||||||
}
|
}
|
||||||
return title;
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonToCss(jsonObj) {
|
||||||
|
var keys = Object.keys(jsonObj);
|
||||||
|
var result = '';
|
||||||
|
for (var i = 0; i < keys.length; i++) {
|
||||||
|
var tmpJsonObj = jsonObj[keys[i]];
|
||||||
|
var tmpKeys = Object.keys(tmpJsonObj);
|
||||||
|
result += '.' + keys[i] + ' {';
|
||||||
|
for (var j = 0; j < tmpKeys.length; j++) {
|
||||||
|
result += tmpKeys[j] + ':' + tmpJsonObj[tmpKeys[j]] + ';';
|
||||||
|
}
|
||||||
|
result += '} ';
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue