mirror of
https://github.com/alexadam/save-as-ebook.git
synced 2025-09-11 09:54:41 +00:00
misc changes WIP
This commit is contained in:
parent
5d5e35021e
commit
0faa8655d9
4 changed files with 276 additions and 122 deletions
|
@ -122,11 +122,28 @@ display: none;
|
||||||
chrome.commands.onCommand.addListener(function(command) {
|
chrome.commands.onCommand.addListener(function(command) {
|
||||||
// alert(isBusy)
|
// alert(isBusy)
|
||||||
if (isBusy) {
|
if (isBusy) {
|
||||||
alert('BUSY')
|
// alert('BUSY')
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (command === 'save-page') {
|
if (command === 'save-page') {
|
||||||
alert('gigi')
|
// alert('gigi')
|
||||||
|
console.log('test str');
|
||||||
|
// FIXME
|
||||||
|
// chrome.runtime.sendMessage({'shortcut': 'save-page'});
|
||||||
|
// chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||||
|
// console.log('test str 2', tabs[0]);
|
||||||
|
// chrome.tabs.sendMessage(tabs[0].id, {'shortcut': 'save-page'}, function(response) {
|
||||||
|
// console.log(response);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||||
|
// console.log('test str 2', tabs[0]);
|
||||||
|
// chrome.tabs.executeScript(tabs[0].id, {file: '/menu.js'});
|
||||||
|
// console.log('test str 3');
|
||||||
|
// chrome.tabs.sendMessage(tabs[0].id, {'shortcut': 'save-page'}, function(response) {
|
||||||
|
// console.log(response);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
dispatch('extract-page', false, []);
|
dispatch('extract-page', false, []);
|
||||||
isBusy = true;
|
isBusy = true;
|
||||||
} else if (command === 'save-selection') {
|
} else if (command === 'save-selection') {
|
||||||
|
@ -152,86 +169,7 @@ function dispatch(action, justAddToBuffer, appliedStyles) {
|
||||||
currentWindow: true,
|
currentWindow: true,
|
||||||
active: true
|
active: true
|
||||||
}, (tab) => {
|
}, (tab) => {
|
||||||
chrome.tabs.sendMessage(tab[0].id, {
|
|
||||||
type: 'echo'
|
|
||||||
}, (response) => {
|
|
||||||
if (!response) {
|
|
||||||
chrome.tabs.executeScript(tab[0].id, {file: '/jquery.js'});
|
|
||||||
chrome.tabs.executeScript(tab[0].id, {file: '/pure-parser.js'});
|
|
||||||
chrome.tabs.executeScript(tab[0].id, {file: '/cssjson.js'});
|
|
||||||
|
|
||||||
chrome.storage.local.get('styles', (data) => {
|
|
||||||
let styles = defaultStyles;
|
|
||||||
if (data && data.styles) {
|
|
||||||
styles = data.styles;
|
|
||||||
}
|
|
||||||
// let currentUrl = tabs[0].url;
|
|
||||||
// let currentStyle = null;
|
|
||||||
//
|
|
||||||
// if (!styles || styles.length === 0) {
|
|
||||||
// chrome.tabs.executeScript(tab[0].id, {
|
|
||||||
// file: 'extractHtml.js'
|
|
||||||
// }, function() {
|
|
||||||
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
|
||||||
// });
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// let allMatchingStyles = [];
|
|
||||||
//
|
|
||||||
// for (let i = 0; i < styles.length; i++) {
|
|
||||||
// currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
|
|
||||||
// var styleUrl = styles[i].url;
|
|
||||||
// var styleUrlRegex = null;
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// styleUrlRegex = new RegExp(styleUrl, 'i');
|
|
||||||
// } catch (e) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (styleUrlRegex && styleUrlRegex.test(currentUrl)) {
|
|
||||||
// allMatchingStyles.push({
|
|
||||||
// index: i,
|
|
||||||
// length: styleUrl.length
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (allMatchingStyles.length >= 1) {
|
|
||||||
// allMatchingStyles.sort(function (a, b) {
|
|
||||||
// return b.length - a.length;
|
|
||||||
// });
|
|
||||||
// var selStyle = allMatchingStyles[0];
|
|
||||||
// currentStyle = styles[selStyle.index];
|
|
||||||
// // setCurrentStyle(currentStyle);
|
|
||||||
//
|
|
||||||
// chrome.tabs.query({
|
|
||||||
// currentWindow: true,
|
|
||||||
// active: true
|
|
||||||
// }, (tab) => {
|
|
||||||
// if (!currentStyle || !currentStyle.style) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// chrome.tabs.insertCSS(tab[0].id, {code: currentStyle.style});
|
|
||||||
// appliedStyles.push(currentStyle);
|
|
||||||
//
|
|
||||||
// alert('HAU')
|
|
||||||
//
|
|
||||||
// chrome.tabs.executeScript(tab[0].id, {
|
|
||||||
// file: 'extractHtml.js'
|
|
||||||
// }, function() {
|
|
||||||
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
});
|
|
||||||
|
|
||||||
// chrome.tabs.executeScript(tab[0].id, {
|
|
||||||
// file: 'extractHtml.js'
|
|
||||||
// }, function() {
|
|
||||||
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
|
||||||
// });
|
|
||||||
} else if (response.echo) {
|
|
||||||
chrome.storage.local.get('styles', (data) => {
|
chrome.storage.local.get('styles', (data) => {
|
||||||
let styles = defaultStyles;
|
let styles = defaultStyles;
|
||||||
if (data && data.styles) {
|
if (data && data.styles) {
|
||||||
|
@ -276,19 +214,14 @@ function dispatch(action, justAddToBuffer, appliedStyles) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
console.log('OoOoooooooolasdlasldkaldklakdlsk');
|
||||||
});
|
// chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
// file: 'extractHtml.js'
|
||||||
|
// }, function() {
|
||||||
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
}
|
// });
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
|
chrome.tabs.sendMessage(tab[0].id, {
|
||||||
|
|
||||||
chrome.tabs.sendMessage(tabId, {
|
|
||||||
type: action,
|
type: action,
|
||||||
appliedStyles: appliedStyles
|
appliedStyles: appliedStyles
|
||||||
}, function(response) {
|
}, function(response) {
|
||||||
|
@ -304,7 +237,23 @@ function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!justAddToBuffer) {
|
if (!justAddToBuffer) {
|
||||||
buildEbook([response]);
|
console.log("build ebook done", response);
|
||||||
|
|
||||||
|
// FIXME - does not work
|
||||||
|
// buildEbook([response]);
|
||||||
|
|
||||||
|
// chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
// file: 'extractHtml.js'
|
||||||
|
// }, function() {
|
||||||
|
// console.log('aoidadoaidoiaodiao');
|
||||||
|
// chrome.tabs.sendMessage(tab[0].id, {'shortcut': 'build-ebook', response: [response]}, (r) => {
|
||||||
|
// console.log(r);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// FIXME
|
||||||
|
chrome.tabs.sendMessage(tab[0].id, {'shortcut': 'build-ebook', response: [response]}, (r) => {
|
||||||
|
console.log(r);
|
||||||
|
});
|
||||||
isBusy = false;
|
isBusy = false;
|
||||||
chrome.browserAction.setBadgeText({text: ""});
|
chrome.browserAction.setBadgeText({text: ""});
|
||||||
} else {
|
} else {
|
||||||
|
@ -320,8 +269,187 @@ function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// chrome.tabs.sendMessage(tab[0].id, {
|
||||||
|
// type: 'echo'
|
||||||
|
// }, (response) => {
|
||||||
|
// if (!response) {
|
||||||
|
// console.log(' NOT RESPONSE');
|
||||||
|
// // chrome.tabs.executeScript(tab[0].id, {file: '/jquery.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: '/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.storage.local.get('styles', (data) => {
|
||||||
|
// // let styles = defaultStyles;
|
||||||
|
// // if (data && data.styles) {
|
||||||
|
// // styles = data.styles;
|
||||||
|
// // }
|
||||||
|
// // let currentUrl = tab[0].url;
|
||||||
|
// // let currentStyle = null;
|
||||||
|
// //
|
||||||
|
// // if (styles && styles.length > 0) {
|
||||||
|
// // let allMatchingStyles = [];
|
||||||
|
// //
|
||||||
|
// // for (let i = 0; i < styles.length; i++) {
|
||||||
|
// // currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
|
||||||
|
// // let styleUrl = styles[i].url;
|
||||||
|
// // let styleUrlRegex = null;
|
||||||
|
// //
|
||||||
|
// // try {
|
||||||
|
// // styleUrlRegex = new RegExp(styleUrl, 'i');
|
||||||
|
// // } catch (e) {
|
||||||
|
// // }
|
||||||
|
// //
|
||||||
|
// // if (styleUrlRegex && styleUrlRegex.test(currentUrl)) {
|
||||||
|
// // allMatchingStyles.push({
|
||||||
|
// // index: i,
|
||||||
|
// // length: styleUrl.length
|
||||||
|
// // });
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// //
|
||||||
|
// // if (allMatchingStyles.length >= 1) {
|
||||||
|
// // allMatchingStyles.sort(function (a, b) {
|
||||||
|
// // return b.length - a.length;
|
||||||
|
// // });
|
||||||
|
// // let selStyle = allMatchingStyles[0];
|
||||||
|
// // currentStyle = styles[selStyle.index];
|
||||||
|
// // // setCurrentStyle(currentStyle);
|
||||||
|
// //
|
||||||
|
// // if (currentStyle && currentStyle.style) {
|
||||||
|
// // chrome.tabs.insertCSS(tab[0].id, {code: currentStyle.style});
|
||||||
|
// // appliedStyles.push(currentStyle);
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// //
|
||||||
|
// // sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
// // });
|
||||||
|
//
|
||||||
|
// // chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
// // file: 'extractHtml.js'
|
||||||
|
// // }, function() {
|
||||||
|
// // console.log('aoidadoaidoiaodiao');
|
||||||
|
// // // sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
// // });
|
||||||
|
// }
|
||||||
|
// // else if (response.echo) {
|
||||||
|
// //
|
||||||
|
// //
|
||||||
|
// //
|
||||||
|
// // // sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
// // }
|
||||||
|
//
|
||||||
|
// console.log('baba lulu iii 33');
|
||||||
|
//
|
||||||
|
// 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: '/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.storage.local.get('styles', (data) => {
|
||||||
|
// let styles = defaultStyles;
|
||||||
|
// if (data && data.styles) {
|
||||||
|
// styles = data.styles;
|
||||||
|
// }
|
||||||
|
// let currentUrl = tab[0].url;
|
||||||
|
// let currentStyle = null;
|
||||||
|
//
|
||||||
|
// if (styles && styles.length > 0) {
|
||||||
|
// let allMatchingStyles = [];
|
||||||
|
//
|
||||||
|
// for (let i = 0; i < styles.length; i++) {
|
||||||
|
// currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
|
||||||
|
// let styleUrl = styles[i].url;
|
||||||
|
// let styleUrlRegex = null;
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// styleUrlRegex = new RegExp(styleUrl, 'i');
|
||||||
|
// } catch (e) {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (styleUrlRegex && styleUrlRegex.test(currentUrl)) {
|
||||||
|
// allMatchingStyles.push({
|
||||||
|
// index: i,
|
||||||
|
// length: styleUrl.length
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (allMatchingStyles.length >= 1) {
|
||||||
|
// allMatchingStyles.sort(function (a, b) {
|
||||||
|
// return b.length - a.length;
|
||||||
|
// });
|
||||||
|
// let selStyle = allMatchingStyles[0];
|
||||||
|
// currentStyle = styles[selStyle.index];
|
||||||
|
// // setCurrentStyle(currentStyle);
|
||||||
|
//
|
||||||
|
// if (currentStyle && currentStyle.style) {
|
||||||
|
// chrome.tabs.insertCSS(tab[0].id, {code: currentStyle.style});
|
||||||
|
// appliedStyles.push(currentStyle);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// console.log('OoOoooooooolasdlasldkaldklakdlsk');
|
||||||
|
// chrome.tabs.executeScript(tab[0].id, {
|
||||||
|
// file: 'extractHtml.js'
|
||||||
|
// }, function() {
|
||||||
|
// sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
// });
|
||||||
|
// // sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
|
||||||
|
//
|
||||||
|
// chrome.tabs.sendMessage(tabId, {
|
||||||
|
// type: action,
|
||||||
|
// appliedStyles: appliedStyles
|
||||||
|
// }, function(response) {
|
||||||
|
//
|
||||||
|
// if (response.length === 0) {
|
||||||
|
// if (justAddToBuffer) {
|
||||||
|
// alert('Cannot add an empty selection as chapter!');
|
||||||
|
// } else {
|
||||||
|
// alert('Cannot generate the eBook from an empty selection!');
|
||||||
|
// }
|
||||||
|
// isBusy = false;
|
||||||
|
// chrome.browserAction.setBadgeText({text: ""});
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (!justAddToBuffer) {
|
||||||
|
// buildEbook([response]);
|
||||||
|
// isBusy = false;
|
||||||
|
// chrome.browserAction.setBadgeText({text: ""});
|
||||||
|
// } else {
|
||||||
|
// chrome.storage.local.get('allPages', function (data) {
|
||||||
|
// if (!data || !data.allPages) {
|
||||||
|
// data.allPages = [];
|
||||||
|
// }
|
||||||
|
// data.allPages.push(response);
|
||||||
|
// chrome.storage.local.set({'allPages': data.allPages});
|
||||||
|
// isBusy = false;
|
||||||
|
// chrome.browserAction.setBadgeText({text: ""});
|
||||||
|
// alert('Page or selection added as chapter!')
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
},
|
},
|
||||||
"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"]
|
"js": ["jquery.js", "jszip.js", "jszip-utils.js", "utils.js", "pure-parser.js", "cssjson.js", "extractHtml.js", "saveEbook.js", "menu.js" ]
|
||||||
}],
|
}],
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": ["background.js", "saveEbook.js", "jszip.js", "jszip-utils.js", "filesaver.js", "utils.js"]
|
"scripts": ["background.js", "saveEbook.js", "jszip.js", "jszip-utils.js", "filesaver.js", "utils.js"]
|
||||||
|
|
|
@ -2,6 +2,14 @@ var allStyles = [];
|
||||||
var currentStyle = null;
|
var currentStyle = null;
|
||||||
var appliedStyles = [];
|
var appliedStyles = [];
|
||||||
|
|
||||||
|
// FIXME
|
||||||
|
chrome.runtime.onMessage.addListener((obj) => {
|
||||||
|
console.log('new MEESSSAAAGGGEE', obj);
|
||||||
|
if (obj.shortcut === 'build-ebook') {
|
||||||
|
// dispatch('extract-page', false);
|
||||||
|
buildEbook(obj.response);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// create menu labels
|
// create menu labels
|
||||||
document.getElementById('menuTitle').innerHTML = chrome.i18n.getMessage('extName');
|
document.getElementById('menuTitle').innerHTML = chrome.i18n.getMessage('extName');
|
||||||
|
|
|
@ -170,13 +170,31 @@ function _buildEbook(allPages) {
|
||||||
|
|
||||||
var done = false;
|
var done = false;
|
||||||
|
|
||||||
|
|
||||||
|
// FIXME
|
||||||
|
var saveData = (function () {
|
||||||
|
var a = document.createElement("a");
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.style = "display: none";
|
||||||
|
return function (data, fileName) {
|
||||||
|
var blob = new Blob([data], {type: "octet/stream"}),
|
||||||
|
url = window.URL.createObjectURL(blob);
|
||||||
|
a.href = url;
|
||||||
|
a.download = fileName;
|
||||||
|
a.click();
|
||||||
|
window.URL.revokeObjectURL(url);
|
||||||
|
};
|
||||||
|
}());
|
||||||
|
|
||||||
zip.generateAsync({
|
zip.generateAsync({
|
||||||
type: "blob"
|
type: "blob"
|
||||||
})
|
})
|
||||||
.then(function(content) {
|
.then(function(content) {
|
||||||
done = true;
|
done = true;
|
||||||
console.log("done !");
|
console.log("done !");
|
||||||
saveAs(content, ebookFileName);
|
// saveAs(content, ebookFileName);
|
||||||
|
// FIXME
|
||||||
|
saveData(content, ebookFileName);
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue