mirror of
https://github.com/alexadam/save-as-ebook.git
synced 2025-09-14 11:19:43 +00:00
misc
This commit is contained in:
parent
3d0f80a159
commit
eac16c0088
4 changed files with 213 additions and 22 deletions
138
background.js
Normal file
138
background.js
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
});
|
|
@ -1,21 +1,28 @@
|
||||||
{
|
{
|
||||||
|
|
||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "Test",
|
"name": "Test",
|
||||||
"version": "1.0",
|
"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": {
|
"icons": {
|
||||||
"48": "icons/border-48.png"
|
"48": "icons/border-48.png"
|
||||||
},
|
},
|
||||||
|
|
||||||
"content_scripts": [
|
"content_scripts": [{
|
||||||
{
|
"matches": ["<all_urls>"],
|
||||||
"matches": ["<all_urls>"],
|
"css": ["fonts.css"],
|
||||||
"css": ["fonts.css"],
|
"js": ["jquery.js", "filesaver.js", "jszip.js", "jszip-utils.js", "test.js"]
|
||||||
"js": ["jquery.js", "filesaver.js", "jszip.js", "jszip-utils.js", "test.js"]
|
}],
|
||||||
}
|
|
||||||
]
|
"background": {
|
||||||
|
"scripts": ["background.js"]
|
||||||
|
},
|
||||||
|
|
||||||
|
"permissions": [
|
||||||
|
"contextMenus",
|
||||||
|
"activeTab"
|
||||||
|
]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
37
test.html
Normal file
37
test.html
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>test ebook</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- <div class="contect">
|
||||||
|
<p>
|
||||||
|
Lorem ipsum dolor sit amet, sit ne eius falli clita. An eum viderer dolorem, et clita mnesarchum mei. Nec dico congue ea, cum eu enim similique. Denique oporteat at nam, pri saperet pericula conceptam eu, per summo omnium suavitate ad. Aliquip accumsan detracto id vel, eu habeo eripuit salutatus has, his laoreet vituperata at.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Lorem ipsum dolor sit amet, sit ne eius falli clita. An eum viderer dolorem, et clita mnesarchum mei. Nec dico congue ea, cum eu enim similique. Denique oporteat at nam, pri saperet pericula conceptam eu, per summo omnium suavitate ad. Aliquip accumsan detracto id vel, eu habeo eripuit salutatus has, his laoreet vituperata at.
|
||||||
|
Lorem ipsum dolor sit amet, sit ne eius falli clita. An eum viderer dolorem, et clita mnesarchum mei. Nec dico congue ea, cum eu enim similique. Denique oporteat at nam, pri saperet pericula conceptam eu, per summo omnium suavitate ad. Aliquip accumsan detracto id vel, eu habeo eripuit salutatus has, his laoreet vituperata at.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Lorem ipsum dolor sit amet, <a href="https://www.google.com">google</a> sit ne eius falli clita. An eum viderer dolorem, et clita mnesarchum mei. Nec dico congue ea, cum eu enim similique. Denique oporteat at nam, pri saperet pericula conceptam eu, per summo omnium suavitate ad. Aliquip accumsan detracto id vel, eu habeo eripuit salutatus has, his laoreet vituperata at.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Lorem ipsum dolor sit amet, sit ne eius falli clita. An eum viderer dolorem, et clita mnesarchum mei.
|
||||||
|
Nec dico congue ea, cum eu enim similique.
|
||||||
|
<img src="http://i.imgur.com/khaB4fN.png" alt="" />
|
||||||
|
Denique oporteat at nam, pri saperet pericula conceptam eu, per summo omnium suavitate ad.
|
||||||
|
Aliquip accumsan detracto id vel, eu habeo eripuit salutatus has, his laoreet vituperata at.
|
||||||
|
</p>
|
||||||
|
</div> -->
|
||||||
|
<div class="content">
|
||||||
|
<p>p1</p>
|
||||||
|
<p>p2</p>
|
||||||
|
<p>p3</p>
|
||||||
|
<p>p4 <a href="https://www.google.com">google</a></p>
|
||||||
|
<p>p5</p>
|
||||||
|
<p>p6 <img src="http://i.imgur.com/khaB4fN.png" alt="" /></p>
|
||||||
|
<p>p7</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
25
test.js
25
test.js
|
@ -4,6 +4,15 @@
|
||||||
// https://stuk.github.io/jszip/
|
// https://stuk.github.io/jszip/
|
||||||
// https://github.com/eligrey/FileSaver.js/
|
// 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 cssFileName = 'ebook.css';
|
||||||
var pageName = 'ebook.xhtml';
|
var pageName = 'ebook.xhtml';
|
||||||
var ebookName = "ebook-" + document.title + ".epub";
|
var ebookName = "ebook-" + document.title + ".epub";
|
||||||
|
@ -13,13 +22,7 @@ var imageIndex = 0;
|
||||||
var allImgSrc = {};
|
var allImgSrc = {};
|
||||||
var allExternalLinks = [];
|
var allExternalLinks = [];
|
||||||
|
|
||||||
console.log('Hello');
|
|
||||||
|
|
||||||
var pageSrc = document.getElementsByTagName('body')[0].innerHTML;
|
|
||||||
|
|
||||||
console.log(pageSrc);
|
|
||||||
|
|
||||||
buildEbook(pageSrc);
|
|
||||||
|
|
||||||
// console.log(pageSrc);
|
// console.log(pageSrc);
|
||||||
//
|
//
|
||||||
|
@ -91,6 +94,12 @@ function getSelectedNode()
|
||||||
function getString(node) {
|
function getString(node) {
|
||||||
var tagName = node.tagName.toLowerCase();
|
var tagName = node.tagName.toLowerCase();
|
||||||
var innerText = node.innerText || node.textContent;
|
var innerText = node.innerText || node.textContent;
|
||||||
|
var innerText = node.innerHTML;
|
||||||
|
|
||||||
|
innerText = innerText.replace(/<[^>]*>[^<]*<[^>]*>/gi,'');
|
||||||
|
innerText = innerText.replace(/<[^>]*>/gi,'');
|
||||||
|
|
||||||
|
console.log(innerText);
|
||||||
|
|
||||||
if (tagName === 'img') {
|
if (tagName === 'img') {
|
||||||
allImgSrc[node.src] = 'img-' + (imageIndex++) + '.' + getFileExtension(node.src);
|
allImgSrc[node.src] = 'img-' + (imageIndex++) + '.' + getFileExtension(node.src);
|
||||||
|
@ -288,7 +297,7 @@ function buildEbook(ebookContent) {
|
||||||
'<item id="toc" properties="nav" href="toc.xhtml" media-type="application/xhtml+xml" />' +
|
'<item id="toc" properties="nav" href="toc.xhtml" media-type="application/xhtml+xml" />' +
|
||||||
'<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />' +
|
'<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />' +
|
||||||
'<item id="template_css" href="' + cssFileName + '" media-type="text/css" />' +
|
'<item id="template_css" href="' + cssFileName + '" media-type="text/css" />' +
|
||||||
'<item id="ebook" href="' + pageName + '" media-type="application/xhtml+xml" properties="remote-resources"/>' +
|
'<item id="ebook" href="' + pageName + '" media-type="application/xhtml+xml" />' + //properties="remote-resources"
|
||||||
getImagesIndex() +
|
getImagesIndex() +
|
||||||
getExternalLinksIndex() +
|
getExternalLinksIndex() +
|
||||||
'</manifest>' +
|
'</manifest>' +
|
||||||
|
@ -329,6 +338,6 @@ function buildEbook(ebookContent) {
|
||||||
.then(function(content) {
|
.then(function(content) {
|
||||||
saveAs(content, ebookName);
|
saveAs(content, ebookName);
|
||||||
});
|
});
|
||||||
}, 5000);
|
}, 50000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue