misc bug fixes

This commit is contained in:
Alex Adam 2018-03-12 19:52:10 +02:00
parent bc8dfab9b3
commit 9237935f69
6 changed files with 105 additions and 34 deletions

View file

@ -200,10 +200,27 @@ function dispatch(action, justAddToBuffer, appliedStyles) {
} }
} }
chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/jszip-utils.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/saveEbook.js'});
chrome.tabs.sendMessage(tab[0].id, { chrome.tabs.sendMessage(tab[0].id, {
type: action, type: action,
appliedStyles: appliedStyles appliedStyles: appliedStyles
}, (response) => { }, (response) => {
if (!response) {
isBusy = false;
chrome.browserAction.setBadgeText({text: ""});
chrome.tabs.sendMessage(tab[0].id, {'alert': 'Save as eBook does not work on this web site!'}, (r) => {
console.log(r);
});
return;
}
if (response.length === 0) { if (response.length === 0) {
if (justAddToBuffer) { if (justAddToBuffer) {
// FIXME does not work on chrome ? // FIXME does not work on chrome ?
@ -240,6 +257,9 @@ function dispatch(action, justAddToBuffer, appliedStyles) {
}) })
} }
}); });
// FIXME set timeout
// isBusy = false;
// chrome.browserAction.setBadgeText({text: ""});
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles); // sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
}); });
}); });
@ -436,5 +456,8 @@ footer, canvas {
if (request.type === 'is busy?') { if (request.type === 'is busy?') {
sendResponse({isBusy: isBusy}) sendResponse({isBusy: isBusy})
} }
if (request.type === 'set is busy') {
isBusy = request.isBusy
}
return true; return true;
}); });

View file

@ -81,9 +81,13 @@ function extractCanvasToImg($htmlObject) {
function extractSvgToImg($htmlObject) { function extractSvgToImg($htmlObject) {
var serializer = new XMLSerializer(); var serializer = new XMLSerializer();
$htmlObject.find('svg').each(function (index, elem) { $htmlObject.find('svg').each(function (index, elem) {
try {
var svgXml = serializer.serializeToString(elem); var svgXml = serializer.serializeToString(elem);
var imgSrc = 'data:image/svg+xml;base64,' + window.btoa(svgXml); var imgSrc = 'data:image/svg+xml;base64,' + window.btoa(svgXml);
$(elem).replaceWith('<img src="' + imgSrc + '">' + '</img>'); $(elem).replaceWith('<img src="' + imgSrc + '">' + '</img>');
} catch (e) {
console.log(e)
} finally {}
}); });
} }
@ -324,7 +328,7 @@ function extractCss(appliedStyles, callback) {
if (!$pre.is(':visible')) { if (!$pre.is(':visible')) {
$pre.replaceWith(''); $pre.replaceWith('');
} else { } else {
if (pre.tagName.roLowerCase() === 'svg') return; if (pre.tagName.toLowerCase() === 'svg') return;
var classNames = pre.getAttribute('class'); var classNames = pre.getAttribute('class');
if (!classNames) { if (!classNames) {

View file

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "__MSG_extName__", "name": "__MSG_extName__",
"version": "1.3", "version": "1.3.1",
"default_locale": "en", "default_locale": "en",
"author": "Alex Adam", "author": "Alex Adam",
"homepage_url": "https://github.com/alexadam/save-as-ebook", "homepage_url": "https://github.com/alexadam/save-as-ebook",
@ -11,10 +11,10 @@
}, },
"content_scripts": [{ "content_scripts": [{
"matches": ["<all_urls>"], "matches": ["<all_urls>"],
"js": ["jquery.js", "jszip.js", "jszip-utils.js", "utils.js", "pure-parser.js", "cssjson.js", "extractHtml.js", "saveEbook.js" ] "js": ["jquery.js", "jszip.js", "jszip-utils.js", "utils.js", "pure-parser.js", "cssjson.js", "extractHtml.js"]
}], }],
"background": { "background": {
"scripts": ["background.js", "saveEbook.js", "jszip.js", "jszip-utils.js", "filesaver.js", "utils.js"] "scripts": ["background.js", "utils.js"]
}, },
"browser_action": { "browser_action": {
"default_icon": "icons/book32.png", "default_icon": "icons/book32.png",

View file

@ -158,7 +158,15 @@ function dispatch(action, justAddToBuffer) {
} }
if (!response) { if (!response) {
chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'}); // when first invoked, response will be undefined because extractHtml.js
// was not executed yet
chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'},
function (result) {
if (!result) {
alert('Save as eBook does not work on this web site!');
setIsBusy(false)
document.getElementById('busy').style.display = 'none';
} else {
chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'}); chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'}); chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'});
chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'}); chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'});
@ -171,6 +179,21 @@ function dispatch(action, justAddToBuffer) {
}, function() { }, function() {
sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles); sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
}); });
}
});
// FIXME
// chrome.tabs.executeScript(tab[0].id, {file: '/utils.js'});
// chrome.tabs.executeScript(tab[0].id, {file: '/filesaver.js'});
// chrome.tabs.executeScript(tab[0].id, {file: '/jszip.js'});
// chrome.tabs.executeScript(tab[0].id, {file: '/jszip-utils.js'});
// chrome.tabs.executeScript(tab[0].id, {file: '/pure-parser.js'});
// chrome.tabs.executeScript(tab[0].id, {file: '/cssjson.js'});
//
// chrome.tabs.executeScript(tab[0].id, {
// file: 'extractHtml.js'
// }, function() {
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
// });
} else if (response.echo) { } else if (response.echo) {
sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles); sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
} }
@ -183,6 +206,12 @@ function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
type: action, type: action,
appliedStyles: appliedStyles appliedStyles: appliedStyles
}, function(response) { }, function(response) {
if (!response) {
alert('Save as eBook does not work on this web site!');
setIsBusy(false)
document.getElementById('busy').style.display = 'none';
return
}
if (response.length === 0) { if (response.length === 0) {
if (justAddToBuffer) { if (justAddToBuffer) {
alert('Cannot add an empty selection as chapter!'); alert('Cannot add an empty selection as chapter!');

View file

@ -181,19 +181,25 @@ function _buildEbook(allPages) {
// FIXME // FIXME
var saveData = (function () { // var saveData = (function () {
var a = document.createElement("a"); // // alert('in save data')
document.body.appendChild(a); // var a = document.createElement("a");
a.style = "display: none"; // document.body.appendChild(a);
return function (data, fileName) { // a.style = "display: none";
var blob = new Blob([data], {type: "octet/stream"}), // return function (data, fileName) {
url = window.URL.createObjectURL(blob); // var blob = new Blob([data], {type: "octet/stream"}),
a.href = url; // url = window.URL.createObjectURL(blob);
a.download = fileName; // // alert(url)
a.click(); // // console.log(url);
window.URL.revokeObjectURL(url); //
}; // // blob:chrome-extension://odnciljldfihdbbpbpdeehpejonicnmh/12789e3f-4ee2-4287-8203-69bc719c2ce8
}()); // // blob:https://uk.godaddy.com/d9cbd1a4-427d-4e55-a30a-80b9bea6547a
// a.href = url;
// a.download = fileName;
// a.click();
// window.URL.revokeObjectURL(url);
// };
// }());
zip.generateAsync({ zip.generateAsync({
type: "blob" type: "blob"
@ -201,9 +207,9 @@ function _buildEbook(allPages) {
.then(function(content) { .then(function(content) {
done = true; done = true;
console.log("done !"); console.log("done !");
// saveAs(content, ebookFileName); saveAs(content, ebookFileName);
// FIXME // FIXME
saveData(content, ebookFileName); // saveData(content, ebookFileName);
}); });
setTimeout(function() { setTimeout(function() {

View file

@ -91,6 +91,13 @@ function checkIfBusy(callback) {
}); });
} }
function setIsBusy(isBusy) {
chrome.runtime.sendMessage({
type: "set is busy",
isBusy: isBusy
}, function(response) {});
}
///// /////
function getCurrentUrl() { function getCurrentUrl() {
var url = window.location.href; var url = window.location.href;
@ -261,6 +268,8 @@ function getBase64ImgData(srcTxt) {
} }
function getXPath(elm) { function getXPath(elm) {
if (!elm) return ''
var allNodes = document.getElementsByTagName('*'); var allNodes = document.getElementsByTagName('*');
for (var segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) { for (var segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) {
if (elm.hasAttribute('id')) { if (elm.hasAttribute('id')) {