diff --git a/background.js b/background.js new file mode 100644 index 0000000..584d549 --- /dev/null +++ b/background.js @@ -0,0 +1,138 @@ +/* +Called when the item has been created, or when creation failed due to an error. +We'll just log success/failure here. +*/ +function onCreated(n) { + if (chrome.runtime.lastError) { + console.log("error creating item:" + chrome.runtime.lastError); + } else { + console.log("item created successfully"); + } +} + +/* +Called when the item has been removed, or when there was an error. +We'll just log success or failure here. +*/ +function onRemoved() { + if (chrome.runtime.lastError) { + console.log("error removing item:" + chrome.runtime.lastError); + } else { + console.log("item removed successfully"); + } +} + +/* +Create all the context menu items. +*/ +chrome.contextMenus.create({ + id: "save", + title: 'Save as E-Book', + contexts: ["all"] +}, onCreated); + +// chrome.contextMenus.create({ +// id: "remove-me", +// title: chrome.i18n.getMessage("contextMenuItemRemoveMe"), +// contexts: ["all"] +// }, onCreated); +// +// chrome.contextMenus.create({ +// id: "separator-1", +// type: "separator", +// contexts: ["all"] +// }, onCreated); +// +// chrome.contextMenus.create({ +// id: "greenify", +// type: "radio", +// title: chrome.i18n.getMessage("contextMenuItemGreenify"), +// contexts: ["all"], +// checked: true +// }, onCreated); +// +// chrome.contextMenus.create({ +// id: "bluify", +// type: "radio", +// title: chrome.i18n.getMessage("contextMenuItemBluify"), +// contexts: ["all"], +// checked: false +// }, onCreated); +// +// chrome.contextMenus.create({ +// id: "separator-2", +// type: "separator", +// contexts: ["all"] +// }, onCreated); +// +// var checkedState = true; +// +// chrome.contextMenus.create({ +// id: "check-uncheck", +// type: "checkbox", +// title: chrome.i18n.getMessage("contextMenuItemUncheckMe"), +// contexts: ["all"], +// checked: checkedState +// }, onCreated); + +/* +Set a colored border on the document in the given tab. + +Note that this only work on normal web pages, not special pages +like about:debugging. +*/ +// var blue = 'document.body.style.border = "5px solid blue"'; +// var green = 'document.body.style.border = "5px solid green"'; +// +// function borderify(tabId, color) { +// chrome.tabs.executeScript(tabId, { +// file: 'test.js' +// }); +// } +// +// /* +// Toggle checkedState, and update the menu item's title +// appropriately. +// +// Note that we should not have to maintain checkedState independently like +// this, but have to because Firefox does not currently pass the "checked" +// property into the event listener. +// */ +// function updateCheckUncheck() { +// checkedState = !checkedState; +// if (checkedState) { +// chrome.contextMenus.update("check-uncheck", { +// title: chrome.i18n.getMessage("contextMenuItemUncheckMe"), +// }); +// } else { +// chrome.contextMenus.update("check-uncheck", { +// title: chrome.i18n.getMessage("contextMenuItemCheckMe"), +// }); +// } +// } + +/* +The click event listener, where we perform the appropriate action given the +ID of the menu item that was clicked. +*/ +chrome.contextMenus.onClicked.addListener(function(info, tab) { + switch (info.menuItemId) { + case "save": + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + chrome.tabs.sendMessage(tabs[0].id, {customData: ""}); + }); + break; + case "remove-me": + chrome.contextMenus.remove(info.menuItemId, onRemoved); + break; + case "bluify": + borderify(tab.id, blue); + break; + case "greenify": + borderify(tab.id, green); + break; + case "check-uncheck": + updateCheckUncheck(); + break; + } +}); diff --git a/manifest.json b/manifest.json index 612c14a..b636637 100644 --- a/manifest.json +++ b/manifest.json @@ -1,21 +1,28 @@ { - "manifest_version": 2, - "name": "Test", - "version": "1.0", + "manifest_version": 2, + "name": "Test", + "version": "1.0", - "description": "Adds a solid red border to all webpages matching mozilla.org.", + "description": "Adds a solid red border to all webpages matching mozilla.org.", - "icons": { - "48": "icons/border-48.png" - }, + "icons": { + "48": "icons/border-48.png" + }, - "content_scripts": [ - { - "matches": [""], - "css": ["fonts.css"], - "js": ["jquery.js", "filesaver.js", "jszip.js", "jszip-utils.js", "test.js"] - } - ] + "content_scripts": [{ + "matches": [""], + "css": ["fonts.css"], + "js": ["jquery.js", "filesaver.js", "jszip.js", "jszip-utils.js", "test.js"] + }], + + "background": { + "scripts": ["background.js"] + }, + + "permissions": [ + "contextMenus", + "activeTab" + ] } diff --git a/test.html b/test.html new file mode 100644 index 0000000..9585535 --- /dev/null +++ b/test.html @@ -0,0 +1,37 @@ + + + + + test ebook + + + +
+

p1

+

p2

+

p3

+

p4 google

+

p5

+

p6

+

p7

+
+ + diff --git a/test.js b/test.js index 97309c5..2290173 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,15 @@ // https://stuk.github.io/jszip/ // https://github.com/eligrey/FileSaver.js/ + +chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { + console.log('Start saving...'); + + var pageSrc = document.getElementsByTagName('body')[0].innerHTML; + + buildEbook(pageSrc); +}); + var cssFileName = 'ebook.css'; var pageName = 'ebook.xhtml'; var ebookName = "ebook-" + document.title + ".epub"; @@ -13,13 +22,7 @@ var imageIndex = 0; var allImgSrc = {}; var allExternalLinks = []; -console.log('Hello'); -var pageSrc = document.getElementsByTagName('body')[0].innerHTML; - -console.log(pageSrc); - -buildEbook(pageSrc); // console.log(pageSrc); // @@ -91,6 +94,12 @@ function getSelectedNode() function getString(node) { var tagName = node.tagName.toLowerCase(); var innerText = node.innerText || node.textContent; + var innerText = node.innerHTML; + + innerText = innerText.replace(/<[^>]*>[^<]*<[^>]*>/gi,''); + innerText = innerText.replace(/<[^>]*>/gi,''); + + console.log(innerText); if (tagName === 'img') { allImgSrc[node.src] = 'img-' + (imageIndex++) + '.' + getFileExtension(node.src); @@ -288,7 +297,7 @@ function buildEbook(ebookContent) { '' + '' + '' + - '' + + '' + //properties="remote-resources" getImagesIndex() + getExternalLinksIndex() + '' + @@ -329,6 +338,6 @@ function buildEbook(ebookContent) { .then(function(content) { saveAs(content, ebookName); }); - }, 5000); + }, 50000); }