diff --git a/background.js b/background.js
new file mode 100644
index 0000000..335a13a
--- /dev/null
+++ b/background.js
@@ -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();
+ }
+});
diff --git a/chapter-editor/inlined.js b/chapter-editor/inlined.js
new file mode 100644
index 0000000..81a0ef1
--- /dev/null
+++ b/chapter-editor/inlined.js
@@ -0,0 +1,67 @@
+
+var body = document.getElementsByTagName('body')[0];
+var modalContent = document.createElement('div');
+modalContent.innerHTML = '
x
';
+
+
+/////////////////////
+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";
diff --git a/chapter-editor/menu.js b/chapter-editor/menu.js
index eef0629..690be04 100644
--- a/chapter-editor/menu.js
+++ b/chapter-editor/menu.js
@@ -6,24 +6,58 @@ document.getElementById("editChapters").onclick = function() {
// chrome.tabs.create({url:"editor.html"});
- var list = document.getElementById('chapters');
- var allPages = getEbookPages();
+ // var list = document.getElementById('chapters');
+ // 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');
- var label = document.createElement('span');
- label.innerHTML = allPages[i].title;
- label.class = 'menu-item-full';
- listItem.appendChild(label);
- list.appendChild(listItem);
- }
+
+ chrome.tabs.query({
+ currentWindow: true,
+ active: true
+ }, function(tab) {
+
+ 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) {
if (!justAddToBuffer) {
- localStorage.removeItem('ebook');
+ removeEbook();
}
chrome.tabs.query({
currentWindow: true,
@@ -63,12 +97,20 @@ function dispatch(action, justAddToBuffer) {
chrome.tabs.sendMessage(tab[0].id, {
type: action
}, function(response) {
- var allPages = getEbookPages();
- allPages.push(response);
- saveEbookPages(allPages);
- if (!justAddToBuffer) {
- buildEbook();
- }
+ // var allPages = getEbookPages();
+ // allPages.push(response);
+ // saveEbookPages(allPages);
+ // if (!justAddToBuffer) {
+ // buildEbook();
+ // }
+
+ getEbookPages(function (allPages) {
+ allPages.push(response);
+ saveEbookPages(allPages);
+ if (!justAddToBuffer) {
+ buildEbook();
+ }
+ });
});
});
});
diff --git a/chapter-editor/saveEbook.js b/chapter-editor/saveEbook.js
index 90b4849..0fad123 100644
--- a/chapter-editor/saveEbook.js
+++ b/chapter-editor/saveEbook.js
@@ -71,10 +71,13 @@ function deferredAddZip(url, filename, zip) {
return deferred;
}
-// http://ebooks.stackexchange.com/questions/1183/what-is-the-minimum-required-content-for-a-valid-epub
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...');
- var allPages = getEbookPages();
var zip = new JSZip();
zip.file('mimetype', 'application/epub+zip');
@@ -223,7 +226,7 @@ function buildEbook() {
}, 60000);
///////////// clean
- localStorage.removeItem('ebook');
+ removeEbook();
imageIndex = 0;
}
diff --git a/chapter-editor/utils.js b/chapter-editor/utils.js
index 29d06c6..1fa587c 100644
--- a/chapter-editor/utils.js
+++ b/chapter-editor/utils.js
@@ -1,19 +1,37 @@
-function getEbookPages() {
- try {
- var allPages = localStorage.getItem('ebook');
- if (!allPages) {
- allPages = [];
- } else {
- allPages = JSON.parse(allPages);
- }
- return allPages;
- } catch (e) {
- console.log(e);
- return [];
- }
+// function getEbookPages() {
+// try {
+// // var allPages = localStorage.getItem('ebook');
+// // chrome.storage.local.get('ebook', function (allPages) {
+// //
+// // });
+// if (!allPages) {
+// allPages = [];
+// } else {
+// allPages = JSON.parse(allPages);
+// }
+// return allPages;
+// } catch (e) {
+// alert(e);
+// return [];
+// }
+// }
+
+function getEbookPages(callback) {
+ chrome.runtime.sendMessage({type: "get"}, function(response) {
+ callback(response.allPages);
+ });
}
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) {});
}
diff --git a/manifest.json b/manifest.json
index 9b3b7f7..dd8d5d2 100644
--- a/manifest.json
+++ b/manifest.json
@@ -16,7 +16,7 @@
}],
"background": {
- "scripts": []
+ "scripts": ["background.js"]
},
"browser_action": {