This commit is contained in:
alexadam 2016-08-16 16:13:43 +03:00
parent d91272b741
commit e72d865c09
5 changed files with 54 additions and 50 deletions

View file

@ -14,7 +14,11 @@ function _getEbookPages() {
}
function _saveEbookPages(pages) {
localStorage.setItem('ebook', JSON.stringify(pages));
try {
localStorage.setItem('ebook', JSON.stringify(pages));
} catch (e) {
alert(e);
}
}
function _removeEbook() {
@ -31,4 +35,5 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type === 'remove') {
_removeEbook();
}
return true;
});

View file

@ -217,9 +217,9 @@ function showEditor() {
});
saveEbookPages(newChapters);
buildEbook();
buildEbookFromChapters();
} catch (e) {
console.log(e);
console.log('Error:', e);
}
}

View file

@ -7,7 +7,8 @@ document.getElementById("editChapters").onclick = function() {
chrome.tabs.query({
currentWindow: true,
active: true
active: true,
highlighted: true
}, function(tab) {
chrome.tabs.executeScript(tab[0].id, {file: '/chapter-editor/jquery.js'});
@ -35,7 +36,8 @@ function dispatch(action, justAddToBuffer) {
}
chrome.tabs.query({
currentWindow: true,
active: true
active: true,
highlighted: true
}, function(tab) {
chrome.tabs.executeScript(tab[0].id, {file: '/chapter-editor/jquery.js'});
@ -51,15 +53,15 @@ function dispatch(action, justAddToBuffer) {
chrome.tabs.sendMessage(tab[0].id, {
type: action
}, function(response) {
getEbookPages(function (allPages) {
allPages.push(response);
saveEbookPages(allPages);
if (!justAddToBuffer) {
buildEbook();
} else {
if (!justAddToBuffer) {
buildEbook([response]);
} else {
getEbookPages(function (allPages) {
allPages.push(response);
saveEbookPages(allPages);
window.close();
}
});
});
}
});
});
});
@ -80,7 +82,3 @@ document.getElementById('pageChapter').onclick = function() {
document.getElementById('selectionChapter').onclick = function() {
dispatch('extract-selection', true);
};
document.getElementById('saveChapters').onclick = function() {
buildEbook();
};

View file

@ -12,10 +12,14 @@ function getExternalLinksIndex() { // TODO ???
}, '');
}
function buildEbook() {
function buildEbookFromChapters() {
getEbookPages(_buildEbook);
}
function buildEbook(allPages) {
_buildEbook(allPages);
}
// http://ebooks.stackexchange.com/questions/1183/what-is-the-minimum-required-content-for-a-valid-epub
function _buildEbook(allPages) {
allPages = allPages.filter(function(page) {

View file

@ -11,17 +11,25 @@ function getImageSrc(srcTxt) {
return '../images/' + allImgSrc[srcTxt];
}
function generateRandomTag() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function force(contentString) {
try {
var tagOpen = '@@@'; //TODO
var tagClose = '###';
var tagOpen = '@@@' + generateRandomTag();
var tagClose = '###' + generateRandomTag();
var inlineElements = ['h1', 'h2', 'h3', 'sup', 'b', 'i', 'em', 'code', 'pre', 'p'];
var replaceElements = [['li', 'p']];
var $content = $(contentString);
// TODO replace li with p
$content.find('img').each(function (index, elem) {
$(elem).replaceWith('<span>' + tagOpen + 'img src="' + getImageSrc($(elem).attr('src')) + '"' + tagClose + tagOpen + '/img' + tagClose + '</span>');
});
@ -30,7 +38,7 @@ function force(contentString) {
$(elem).replaceWith('<span>' + tagOpen + 'a href="' + getHref($(elem).attr('href')) + '"' + tagClose + $(elem).html() + tagOpen + '/a' + tagClose + '</span>');
});
if ($('*').length < maxNrOfElements) { // TODO
if ($('*').length < maxNrOfElements) {
inlineElements.forEach(function (tagName) {
$content.find(tagName).each(function (index, elem) {
var $elem = $(elem);
@ -57,8 +65,8 @@ function force(contentString) {
contentString = contentString.replace(/&/gi, '&amp;');
return contentString;
} catch(e) {
console.log(e);
} catch (e) {
console.log('Error:', e);
}
}
@ -77,21 +85,12 @@ function sanitize(rawContentString) {
dirty = '<div>' + $wdirty.html() + '</div>';
////////////////
return force(dirty);
return force(dirty); // TODO
/// TODO
if ($('*').length < maxNrOfElements) { // TODO
return force(dirty); // TODO
if ($('*').length < maxNrOfElements) {
return force(dirty);
}
// var dirty = '<div>' + document.getElementsByTagName('body')[0].innerHTML + '</div>';
var results = '';
var lastFragment = '';
var lastTag = '';
@ -163,15 +162,14 @@ function sanitize(rawContentString) {
}
});
// results = results.replace(/<([^>]+?)>\s*<\/\1>/gim, '');
results = results.replace(/&[a-z]+;/gim, '');
// results = results.replace(/&[a-z]+;/gim, '');
results = results.replace(/&amp;/gi, '&');
results = results.replace(/&/gi, '&amp;');
return results;
} catch (e) {
console.trace();
console.log(e);
console.log('Error:', e);
return force(dirty);
}
@ -184,7 +182,7 @@ function getContent(htmlContent) {
var dirty = '<div>' + tmp.innerHTML + '</div>';
return sanitize(dirty);
} catch (e) {
console.log(e);
console.log('Error:', e);
return '';
}
}
@ -200,10 +198,10 @@ function getPageTitle(inp) { //TODO
}
function getSelectedNodes() {
if (document.selection) {
// if (document.selection) {
// return document.selection.createRange().parentElement();
return document.selection.createRange();
}
// return document.selection.createRange();
// }
var selection = window.getSelection();
var docfrag = [];
for (var i = 0; i < selection.rangeCount; i++) {
@ -259,8 +257,7 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var tmpDeffered = deferredAddZip(getImgDownloadUrl(imgSrc), allImgSrc[imgSrc]);
imgsPromises.push(tmpDeffered);
} catch (e) {
alert(e);
console.log(e);
console.log('Error:', e);
}
});
@ -273,8 +270,8 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
content: tmpContent
};
sendResponse(result);
}).fail(function(err) {
console.log('ERROR', JSON.stringify(err));
}).fail(function(e) {
console.log('Error:', e);
});
return true;