mirror of
https://github.com/alexadam/save-as-ebook.git
synced 2025-09-11 09:54:41 +00:00
88 lines
2.2 KiB
JavaScript
88 lines
2.2 KiB
JavaScript
var win = null;
|
|
|
|
document.getElementById("editChapters").onclick = function() {
|
|
win = window.open(chrome.extension.getURL('chapter-editor/editor.html'), '_blank');
|
|
win.focus();
|
|
};
|
|
|
|
|
|
function getEbookPages() { // TODO add as utils
|
|
try {
|
|
var allPages = localStorage.getItem('ebook');
|
|
if (!allPages) {
|
|
allPages = [];
|
|
} else {
|
|
allPages = JSON.parse(allPages);
|
|
}
|
|
return allPages;
|
|
} catch (e) {
|
|
console.log(e);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function saveEbookPages(pages) {
|
|
localStorage.setItem('ebook', JSON.stringify(pages));
|
|
}
|
|
|
|
document.getElementById('savePage').onclick = function() {
|
|
localStorage.removeItem('ebook');
|
|
chrome.tabs.query({
|
|
currentWindow: true,
|
|
active: true
|
|
}, function(tab) {
|
|
chrome.tabs.sendMessage(
|
|
tab[0].id,
|
|
{
|
|
type: 'extract-page'
|
|
},
|
|
function (response) {
|
|
var allPages = getEbookPages();
|
|
allPages.push(response);
|
|
saveEbookPages(allPages);
|
|
buildEbook();
|
|
}
|
|
);
|
|
});
|
|
};
|
|
|
|
document.getElementById('saveSelection').onclick = function() {
|
|
localStorage.removeItem('ebook');
|
|
chrome.tabs.query({
|
|
currentWindow: true,
|
|
active: true
|
|
}, function(tab) {
|
|
chrome.tabs.sendMessage(
|
|
tab[0].id,
|
|
{
|
|
type: 'extract-selection'
|
|
},
|
|
function (response) {
|
|
console.log('Selection EXTRAcTED', response);
|
|
}
|
|
);
|
|
});
|
|
};
|
|
|
|
document.getElementById('title').onclick = function() {
|
|
chrome.tabs.query({
|
|
currentWindow: true,
|
|
active: true
|
|
}, function(tab) {
|
|
chrome.tabs.sendMessage(
|
|
tab[0].id, {
|
|
type: 'get-title'
|
|
},
|
|
function(response) {
|
|
var title = localStorage.getItem('title');
|
|
if (title === null) {
|
|
title = [];
|
|
} else {
|
|
title = JSON.parse(title);
|
|
}
|
|
title.push(response);
|
|
localStorage.setItem('title', JSON.stringify(title));
|
|
}
|
|
);
|
|
});
|
|
};
|