mirror of
https://github.com/alexadam/save-as-ebook.git
synced 2025-09-10 17:34:47 +00:00
misc
This commit is contained in:
parent
a391509e5d
commit
b74dd4e5bf
6 changed files with 199 additions and 35 deletions
34
background.js
Normal file
34
background.js
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
function _getEbookPages() {
|
||||||
|
try {
|
||||||
|
var allPages = localStorage.getItem('ebook');
|
||||||
|
if (!allPages) {
|
||||||
|
allPages = [];
|
||||||
|
} else {
|
||||||
|
allPages = JSON.parse(allPages);
|
||||||
|
}
|
||||||
|
return allPages;
|
||||||
|
} catch (e) {
|
||||||
|
alert(e);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _saveEbookPages(pages) {
|
||||||
|
localStorage.setItem('ebook', JSON.stringify(pages));
|
||||||
|
}
|
||||||
|
|
||||||
|
function _removeEbook() {
|
||||||
|
localStorage.removeItem('ebook');
|
||||||
|
}
|
||||||
|
|
||||||
|
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
||||||
|
if (request.type === 'get') {
|
||||||
|
sendResponse({allPages: _getEbookPages()});
|
||||||
|
}
|
||||||
|
if (request.type === 'set') {
|
||||||
|
_saveEbookPages(request.pages);
|
||||||
|
}
|
||||||
|
if (request.type === 'remove') {
|
||||||
|
_removeEbook();
|
||||||
|
}
|
||||||
|
});
|
67
chapter-editor/inlined.js
Normal file
67
chapter-editor/inlined.js
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
|
||||||
|
var body = document.getElementsByTagName('body')[0];
|
||||||
|
var modalContent = document.createElement('div');
|
||||||
|
modalContent.innerHTML = '<div class="close">x</div>';
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
getEbookPages(createChapterList);
|
||||||
|
|
||||||
|
function createChapterList(allPages) {
|
||||||
|
var list = document.createElement('ul');
|
||||||
|
for (var i = 0; i < allPages.length; i++) {
|
||||||
|
if (!allPages[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var listItem = document.createElement('li');
|
||||||
|
var label = document.createElement('span');
|
||||||
|
label.innerHTML = allPages[i].title;
|
||||||
|
listItem.appendChild(label);
|
||||||
|
list.appendChild(listItem);
|
||||||
|
}
|
||||||
|
modalContent.appendChild(list);
|
||||||
|
}
|
||||||
|
/////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var modal = document.createElement('div');
|
||||||
|
modal.appendChild(modalContent);
|
||||||
|
|
||||||
|
body.appendChild(modal);
|
||||||
|
|
||||||
|
modal.style.display = "none";
|
||||||
|
modal.style.position= 'fixed'; /* Stay in place */
|
||||||
|
modal.style.zIndex= '1'; /* Sit on top */
|
||||||
|
modal.style.left= '0';
|
||||||
|
modal.style.top= '0';
|
||||||
|
modal.style.width= '100%'; /* Full width */
|
||||||
|
modal.style.height= '100%'; /* Full height */
|
||||||
|
modal.style.overflow= 'auto'; /* Enable scroll if needed */
|
||||||
|
modal.style.backgroundColor= 'rgba(0,0,0,0.5)'; /* Fallback color */
|
||||||
|
|
||||||
|
var span = document.getElementsByClassName("close")[0];
|
||||||
|
span.onclick = function() {
|
||||||
|
modal.style.display = "none";
|
||||||
|
modalContent.parentNode.removeChild(modalContent);
|
||||||
|
modal.parentNode.removeChild(modal);
|
||||||
|
};
|
||||||
|
|
||||||
|
modalContent.style.zIndex= '2'; /* Sit on top */
|
||||||
|
modalContent.style.backgroundColor = '#fff';
|
||||||
|
modalContent.style.margin= '15% auto'; /* 15% from the top and centered */
|
||||||
|
modalContent.style.padding= '20px';
|
||||||
|
modalContent.style.border= '1px solid #888';
|
||||||
|
modalContent.style.width= '80%'; /* Could be more or less, depending on screen size */
|
||||||
|
|
||||||
|
// When the user clicks anywhere outside of the modal, close it
|
||||||
|
window.onclick = function(event) {
|
||||||
|
if (event.target == modal) {
|
||||||
|
modal.style.display = "none";
|
||||||
|
modalContent.parentNode.removeChild(modalContent);
|
||||||
|
modal.parentNode.removeChild(modal);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
modal.style.display = "block";
|
|
@ -6,24 +6,58 @@ document.getElementById("editChapters").onclick = function() {
|
||||||
|
|
||||||
// chrome.tabs.create({url:"editor.html"});
|
// chrome.tabs.create({url:"editor.html"});
|
||||||
|
|
||||||
var list = document.getElementById('chapters');
|
// var list = document.getElementById('chapters');
|
||||||
var allPages = getEbookPages();
|
// var allPages = getEbookPages();
|
||||||
|
//
|
||||||
|
// for (var i = 0; i < allPages.length; i++) {
|
||||||
|
// var listItem = document.createElement('li');
|
||||||
|
// var label = document.createElement('span');
|
||||||
|
// label.innerHTML = allPages[i].title;
|
||||||
|
// label.class = 'menu-item-full';
|
||||||
|
// listItem.appendChild(label);
|
||||||
|
// list.appendChild(listItem);
|
||||||
|
// }
|
||||||
|
|
||||||
for (var i = 0; i < allPages.length; i++) {
|
|
||||||
var listItem = document.createElement('li');
|
chrome.tabs.query({
|
||||||
var label = document.createElement('span');
|
currentWindow: true,
|
||||||
label.innerHTML = allPages[i].title;
|
active: true
|
||||||
label.class = 'menu-item-full';
|
}, function(tab) {
|
||||||
listItem.appendChild(label);
|
|
||||||
list.appendChild(listItem);
|
chrome.tabs.executeScript(tab[0].id, {
|
||||||
}
|
file: '/chapter-editor/utils.js'
|
||||||
|
});
|
||||||
|
|
||||||
|
chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
file: '/chapter-editor/inlined.js'
|
||||||
|
});
|
||||||
|
|
||||||
|
// chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
// file: 'extractHtml.js'
|
||||||
|
// }, function() {
|
||||||
|
// // if (chrome.runtime.lastError) {
|
||||||
|
// // alert(JSON.stringify(chrome.runtime.lastError));
|
||||||
|
// // throw Error("Unable to inject script into tab " + tabId);
|
||||||
|
// // }
|
||||||
|
// chrome.tabs.sendMessage(tab[0].id, {
|
||||||
|
// type: action
|
||||||
|
// }, function(response) {
|
||||||
|
// var allPages = getEbookPages();
|
||||||
|
// allPages.push(response);
|
||||||
|
// saveEbookPages(allPages);
|
||||||
|
// if (!justAddToBuffer) {
|
||||||
|
// buildEbook();
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function dispatch(action, justAddToBuffer) {
|
function dispatch(action, justAddToBuffer) {
|
||||||
if (!justAddToBuffer) {
|
if (!justAddToBuffer) {
|
||||||
localStorage.removeItem('ebook');
|
removeEbook();
|
||||||
}
|
}
|
||||||
chrome.tabs.query({
|
chrome.tabs.query({
|
||||||
currentWindow: true,
|
currentWindow: true,
|
||||||
|
@ -63,12 +97,20 @@ function dispatch(action, justAddToBuffer) {
|
||||||
chrome.tabs.sendMessage(tab[0].id, {
|
chrome.tabs.sendMessage(tab[0].id, {
|
||||||
type: action
|
type: action
|
||||||
}, function(response) {
|
}, function(response) {
|
||||||
var allPages = getEbookPages();
|
// var allPages = getEbookPages();
|
||||||
allPages.push(response);
|
// allPages.push(response);
|
||||||
saveEbookPages(allPages);
|
// saveEbookPages(allPages);
|
||||||
if (!justAddToBuffer) {
|
// if (!justAddToBuffer) {
|
||||||
buildEbook();
|
// buildEbook();
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
getEbookPages(function (allPages) {
|
||||||
|
allPages.push(response);
|
||||||
|
saveEbookPages(allPages);
|
||||||
|
if (!justAddToBuffer) {
|
||||||
|
buildEbook();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -71,10 +71,13 @@ function deferredAddZip(url, filename, zip) {
|
||||||
return deferred;
|
return deferred;
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://ebooks.stackexchange.com/questions/1183/what-is-the-minimum-required-content-for-a-valid-epub
|
|
||||||
function buildEbook() {
|
function buildEbook() {
|
||||||
|
getEbookPages(_buildEbook);
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://ebooks.stackexchange.com/questions/1183/what-is-the-minimum-required-content-for-a-valid-epub
|
||||||
|
function _buildEbook(allPages) {
|
||||||
console.log('Prepare Content...');
|
console.log('Prepare Content...');
|
||||||
var allPages = getEbookPages();
|
|
||||||
var zip = new JSZip();
|
var zip = new JSZip();
|
||||||
|
|
||||||
zip.file('mimetype', 'application/epub+zip');
|
zip.file('mimetype', 'application/epub+zip');
|
||||||
|
@ -223,7 +226,7 @@ function buildEbook() {
|
||||||
}, 60000);
|
}, 60000);
|
||||||
|
|
||||||
///////////// clean
|
///////////// clean
|
||||||
localStorage.removeItem('ebook');
|
removeEbook();
|
||||||
imageIndex = 0;
|
imageIndex = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,37 @@
|
||||||
|
|
||||||
function getEbookPages() {
|
// function getEbookPages() {
|
||||||
try {
|
// try {
|
||||||
var allPages = localStorage.getItem('ebook');
|
// // var allPages = localStorage.getItem('ebook');
|
||||||
if (!allPages) {
|
// // chrome.storage.local.get('ebook', function (allPages) {
|
||||||
allPages = [];
|
// //
|
||||||
} else {
|
// // });
|
||||||
allPages = JSON.parse(allPages);
|
// if (!allPages) {
|
||||||
}
|
// allPages = [];
|
||||||
return allPages;
|
// } else {
|
||||||
} catch (e) {
|
// allPages = JSON.parse(allPages);
|
||||||
console.log(e);
|
// }
|
||||||
return [];
|
// return allPages;
|
||||||
}
|
// } catch (e) {
|
||||||
|
// alert(e);
|
||||||
|
// return [];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
function getEbookPages(callback) {
|
||||||
|
chrome.runtime.sendMessage({type: "get"}, function(response) {
|
||||||
|
callback(response.allPages);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveEbookPages(pages) {
|
function saveEbookPages(pages) {
|
||||||
localStorage.setItem('ebook', JSON.stringify(pages));
|
// localStorage.setItem('ebook', JSON.stringify(pages));
|
||||||
|
// chrome.storage.local.set({'ebook': JSON.stringify(pages)});
|
||||||
|
chrome.runtime.sendMessage({type: "set", pages: pages}, function(response) {});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeEbook() {
|
||||||
|
// localStorage.removeItem('ebook');
|
||||||
|
// chrome.storage.local.remove('ebook');
|
||||||
|
|
||||||
|
chrome.runtime.sendMessage({type: "remove"}, function(response) {});
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
}],
|
}],
|
||||||
|
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": []
|
"scripts": ["background.js"]
|
||||||
},
|
},
|
||||||
|
|
||||||
"browser_action": {
|
"browser_action": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue