misc bug fixes

This commit is contained in:
Alex Adam 2018-03-24 20:45:41 +02:00
parent 4d14fd5baf
commit d3ea23f3bf
7 changed files with 126 additions and 321 deletions

View file

@ -120,6 +120,10 @@ display: none;
]; ];
chrome.commands.onCommand.addListener((command) => { chrome.commands.onCommand.addListener((command) => {
executeCommand({type: command})
});
function executeCommand(command) {
if (isBusy) { if (isBusy) {
chrome.tabs.query({ chrome.tabs.query({
currentWindow: true, currentWindow: true,
@ -131,24 +135,24 @@ chrome.commands.onCommand.addListener((command) => {
}) })
return; return;
} }
if (command === 'save-page') { if (command.type === 'save-page') {
dispatch('extract-page', false, []); dispatch('extract-page', false, []);
isBusy = true; isBusy = true;
} else if (command === 'save-selection') { } else if (command.type === 'save-selection') {
dispatch('extract-selection', false, []); dispatch('extract-selection', false, []);
isBusy = true; isBusy = true;
} else if (command === 'add-page') { } else if (command.type === 'add-page') {
dispatch('extract-page', true, []); dispatch('extract-page', true, []);
isBusy = true; isBusy = true;
} else if (command === 'add-selection') { } else if (command.type === 'add-selection') {
dispatch('extract-selection', true, []); dispatch('extract-selection', true, []);
isBusy = true; isBusy = true;
} }
}); }
function dispatch(action, justAddToBuffer, appliedStyles) { function dispatch(action, justAddToBuffer, appliedStyles) {
if (!justAddToBuffer) { if (!justAddToBuffer) {
removeEbook(); _execRequest({type: 'remove'});
} }
chrome.browserAction.setBadgeBackgroundColor({color:"red"}); chrome.browserAction.setBadgeBackgroundColor({color:"red"});
chrome.browserAction.setBadgeText({text: "Busy"}); chrome.browserAction.setBadgeText({text: "Busy"});
@ -200,13 +204,6 @@ 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
@ -265,7 +262,9 @@ function dispatch(action, justAddToBuffer, appliedStyles) {
}); });
} }
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { chrome.runtime.onMessage.addListener(_execRequest);
function _execRequest(request, sender, sendResponse) {
if (request.type === 'get') { if (request.type === 'get') {
chrome.storage.local.get('allPages', function (data) { chrome.storage.local.get('allPages', function (data) {
if (!data || !data.allPages) { if (!data || !data.allPages) {
@ -296,130 +295,6 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type === 'get styles') { if (request.type === 'get styles') {
chrome.storage.local.get('styles', function (data) { chrome.storage.local.get('styles', function (data) {
if (!data || !data.styles) { if (!data || !data.styles) {
// TODO move defaultStyles in a different file/location ?
var defaultStyles = [
{
title: 'Reddit Comments',
url: 'reddit\\.com\\/r\\/[^\\/]+\\/comments',
style: `.side {
display: none;
}
#header {
display: none;
}
.arrow, .expand, .score, .live-timestamp, .flat-list, .buttons, .morecomments, .footer-parent, .icon {
display: none !important;
}
`
},{
title: 'Wikipedia Article',
url: 'wikipedia\\.org\\/wiki\\/',
style: `#mw-navigation {
display: none;
}
#footer {
display: none;
}
#mw-panel {
display: none;
}
#mw-head {
display: none;
}
`
},{
title: 'YCombinator News Comments',
url: 'news\\.ycombinator\\.com\\/item\\?id=[0-9]+',
style: `#hnmain > tbody > tr:nth-child(1) > td > table {
display: none;
}
* {
background-color: white;
}
.title, .storylink {
text-align: left;
font-weight: bold;
font-size: 20px;
}
.score {
display: none;
}
.age {
display: none;
}
.hnpast {
display: none;
}
.togg {
display: none;
}
.votelinks, .rank {
display: none;
}
.votearrow {
display: none;
}
.yclinks {
display: none;
}
form {
display: none;
}
a.hnuser {
font-weight: bold;
color: black !important;
padding: 3px;
}
.subtext > span, .subtext > a:not(:nth-child(2)) {
display: none;
}
`
},{
title: 'Medium Article',
url: 'medium\\.com',
style: `.metabar {
display: none !important;
}
header.container {
display: none;
}
.js-postShareWidget {
display: none;
}
footer, canvas {
display: none !important;
}
.u-fixed, .u-bottom0 {
display: none;
}
`
},{
title: 'Twitter',
url: 'twitter\\.com\\/.+',
style: `.topbar {
display: none !important;
}
.ProfileCanopy, .ProfileCanopy-inner {
display: none;
}
.ProfileSidebar {
display: none;
}
.ProfileHeading {
display: none !important;
}
.ProfileTweet-actionList {
display: none;
}
`
}
/*
*/
];
sendResponse({styles: defaultStyles}); sendResponse({styles: defaultStyles});
} else { } else {
sendResponse({styles: data.styles}); sendResponse({styles: data.styles});
@ -459,5 +334,9 @@ footer, canvas {
if (request.type === 'set is busy') { if (request.type === 'set is busy') {
isBusy = request.isBusy isBusy = request.isBusy
} }
if (request.type === 'save-page' || request.type === 'save-selection' ||
request.type === 'add-page' || request.type === 'add-selection') {
executeCommand({type: request.type})
}
return true; return true;
}); }

View file

@ -321,54 +321,57 @@ function jsonToCss(jsonObj) {
} }
function extractCss(appliedStyles, callback) { function extractCss(appliedStyles, callback) {
getIncludeStyle(function (result) {
if (result) {
$('body').find('*').each(function (i, pre) {
var $pre = $(pre);
if (!$pre.is(':visible')) {
$pre.replaceWith('');
} else {
if (pre.tagName.toLowerCase() === 'svg') return;
var classNames = pre.getAttribute('class'); chrome.runtime.sendMessage({
if (!classNames) { type: "get include style"
classNames = pre.getAttribute('id'); }, function(response) {
if (response.includeStyle) {
$('body').find('*').each(function (i, pre) {
var $pre = $(pre);
if (!$pre.is(':visible')) {
$pre.replaceWith('');
} else {
if (pre.tagName.toLowerCase() === 'svg') return;
var classNames = pre.getAttribute('class');
if (!classNames) { if (!classNames) {
classNames = pre.tagName + '-' + Math.floor(Math.random()*100000); classNames = pre.getAttribute('id');
} if (!classNames) {
} classNames = pre.tagName + '-' + Math.floor(Math.random()*100000);
var tmpName = cssClassesToTmpIds[classNames];
var tmpNewCss = tmpIdsToNewCss[tmpName];
if (!tmpName) {
tmpName = 'class-' + Math.floor(Math.random()*100000);
cssClassesToTmpIds[classNames] = tmpName;
}
if (!tmpNewCss) {
// var style = window.getComputedStyle(pre);
tmpNewCss = {};
for (var cssTagName of supportedCss) {
var cssValue = $pre.css(cssTagName);
if (cssValue && cssValue.length > 0) {
tmpNewCss[cssTagName] = cssValue;
} }
} }
tmpIdsToNewCss[tmpName] = tmpNewCss; var tmpName = cssClassesToTmpIds[classNames];
var tmpNewCss = tmpIdsToNewCss[tmpName];
if (!tmpName) {
tmpName = 'class-' + Math.floor(Math.random()*100000);
cssClassesToTmpIds[classNames] = tmpName;
}
if (!tmpNewCss) {
// var style = window.getComputedStyle(pre);
tmpNewCss = {};
for (var cssTagName of supportedCss) {
var cssValue = $pre.css(cssTagName);
if (cssValue && cssValue.length > 0) {
tmpNewCss[cssTagName] = cssValue;
}
}
tmpIdsToNewCss[tmpName] = tmpNewCss;
}
pre.setAttribute('data-class', tmpName);
} }
pre.setAttribute('data-class', tmpName); });
callback(jsonToCss(tmpIdsToNewCss));
} else {
var mergedCss = '';
if (appliedStyles && appliedStyles.length > 0) {
for (var i = 0; i < appliedStyles.length; i++) {
mergedCss += appliedStyles[i].style;
}
callback(mergedCss);
return;
} }
}); callback();
callback(jsonToCss(tmpIdsToNewCss));
} else {
var mergedCss = '';
if (appliedStyles && appliedStyles.length > 0) {
for (var i = 0; i < appliedStyles.length; i++) {
mergedCss += appliedStyles[i].style;
}
callback(mergedCss);
return;
} }
callback();
}
}); });
} }

View file

@ -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"] "js": ["jquery.js", "jszip.js", "jszip-utils.js", "pure-parser.js", "cssjson.js", "filesaver.js", "saveEbook.js", "extractHtml.js", "utils.js"]
}], }],
"background": { "background": {
"scripts": ["background.js", "utils.js"] "scripts": ["background.js"]
}, },
"browser_action": { "browser_action": {
"default_icon": "icons/book32.png", "default_icon": "icons/book32.png",

View file

@ -157,12 +157,6 @@
<div id="waitMessage" class="wait-message"></div> <div id="waitMessage" class="wait-message"></div>
</div> </div>
<script src="jquery.js" charset="utf-8"></script>
<script src="filesaver.js" charset="utf-8"></script>
<script src="jszip.js" charset="utf-8"></script>
<script src="jszip-utils.js" charset="utf-8"></script>
<script src="utils.js" charset="utf-8"></script>
<script src="saveEbook.js" charset="utf-8"></script>
<script src="menu.js" charset="utf-8"></script> <script src="menu.js" charset="utf-8"></script>
</body> </body>

View file

@ -13,15 +13,27 @@ document.getElementById('selectionChapterLabel').innerHTML = chrome.i18n.getMess
document.getElementById('editChapters').innerHTML = chrome.i18n.getMessage('editChapters'); document.getElementById('editChapters').innerHTML = chrome.i18n.getMessage('editChapters');
document.getElementById('waitMessage').innerHTML = chrome.i18n.getMessage('waitMessage'); document.getElementById('waitMessage').innerHTML = chrome.i18n.getMessage('waitMessage');
checkIfBusy((result) => { function removeEbook() {
if (result.isBusy) { chrome.runtime.sendMessage({
type: "remove"
}, function(response) {});
}
chrome.runtime.sendMessage({
type: "is busy?"
}, function(response) {
if (response.isBusy) {
document.getElementById('busy').style.display = 'block'; document.getElementById('busy').style.display = 'block';
} else { } else {
document.getElementById('busy').style.display = 'none'; document.getElementById('busy').style.display = 'none';
} }
}) });
getStyles(createStyleList); chrome.runtime.sendMessage({
type: "get styles"
}, function(response) {
createStyleList(response.styles);
});
function createStyleList(styles) { function createStyleList(styles) {
allStyles = styles; allStyles = styles;
@ -67,7 +79,12 @@ function createStyleList(styles) {
}); });
var selStyle = allMatchingStyles[0]; var selStyle = allMatchingStyles[0];
currentStyle = styles[selStyle.index]; currentStyle = styles[selStyle.index];
setCurrentStyle(currentStyle);
chrome.runtime.sendMessage({
type: "set current style",
currentStyle: currentStyle
}, function(response) {
});
} }
}); });
} }
@ -77,7 +94,11 @@ function createIncludeStyle(data) {
includeStyleCheck.checked = data; includeStyleCheck.checked = data;
} }
getIncludeStyle(createIncludeStyle); chrome.runtime.sendMessage({
type: "get include style"
}, function(response) {
createIncludeStyle(response.includeStyle);
});
document.getElementById('includeStyleCheck').onclick = function () { document.getElementById('includeStyleCheck').onclick = function () {
var includeStyleCheck = document.getElementById('includeStyleCheck'); var includeStyleCheck = document.getElementById('includeStyleCheck');
@ -95,12 +116,6 @@ document.getElementById("editStyles").onclick = function() {
active: true active: true
}, function(tab) { }, function(tab) {
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.insertCSS(tab[0].id, {file: '/cssEditor.css'}); chrome.tabs.insertCSS(tab[0].id, {file: '/cssEditor.css'});
chrome.tabs.executeScript(tab[0].id, { chrome.tabs.executeScript(tab[0].id, {
@ -122,12 +137,6 @@ document.getElementById("editChapters").onclick = function() {
active: true active: true
}, function(tab) { }, function(tab) {
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.executeScript(tab[0].id, {file: '/jquery-sortable.js'}); chrome.tabs.executeScript(tab[0].id, {file: '/jquery-sortable.js'});
chrome.tabs.insertCSS(tab[0].id, {file: '/chapterEditor.css'}); chrome.tabs.insertCSS(tab[0].id, {file: '/chapterEditor.css'});
@ -139,116 +148,33 @@ document.getElementById("editChapters").onclick = function() {
}); });
}; };
function dispatch(action, justAddToBuffer) { function dispatch(commandType, justAddToBuffer) {
document.getElementById('busy').style.display = 'block'; document.getElementById('busy').style.display = 'block';
if (!justAddToBuffer) { if (!justAddToBuffer) {
removeEbook(); removeEbook();
} }
chrome.tabs.query({ chrome.runtime.sendMessage({
currentWindow: true, type: commandType
active: true
}, function(tab) {
chrome.tabs.sendMessage(tab[0].id, {
type: 'echo'
}, function(response) {
if (currentStyle && currentStyle.style) {
chrome.tabs.insertCSS(tab[0].id, {code: currentStyle.style});
appliedStyles.push(currentStyle);
}
if (!response) {
// 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: '/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);
});
}
});
// 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) {
sendMessage(tab[0].id, action, justAddToBuffer, appliedStyles);
}
});
});
}
function sendMessage(tabId, action, justAddToBuffer, appliedStyles) {
chrome.tabs.sendMessage(tabId, {
type: action,
appliedStyles: appliedStyles
}, function(response) { }, function(response) {
if (!response) { //FIXME - hidden before done
alert('Save as eBook does not work on this web site!'); document.getElementById('busy').style.display = 'none';
setIsBusy(false)
document.getElementById('busy').style.display = 'none';
return
}
if (response.length === 0) {
if (justAddToBuffer) {
alert('Cannot add an empty selection as chapter!');
} else {
alert('Cannot generate the eBook from an empty selection!');
}
window.close();
}
if (!justAddToBuffer) {
buildEbook([response], true);
} else {
getEbookPages(function (allPages) {
allPages.push(response);
saveEbookPages(allPages);
window.close();
});
}
setTimeout(function () {
document.getElementById('busy').style.display = 'none';
}, 500);
}); });
} }
document.getElementById('savePage').onclick = function() { document.getElementById('savePage').onclick = function() {
dispatch('extract-page', false); dispatch('save-page', false);
}; };
document.getElementById('saveSelection').onclick = function() { document.getElementById('saveSelection').onclick = function() {
dispatch('extract-selection', false); dispatch('save-selection', false);
}; };
document.getElementById('pageChapter').onclick = function() { document.getElementById('pageChapter').onclick = function() {
dispatch('extract-page', true); dispatch('add-page', true);
}; };
document.getElementById('selectionChapter').onclick = function() { document.getElementById('selectionChapter').onclick = function() {
dispatch('extract-selection', true); dispatch('add-selection', true);
}; };
// get all shortcuts and display them in the menuTitle // get all shortcuts and display them in the menuTitle

View file

@ -32,6 +32,7 @@ function buildEbookFromChapters() {
}) })
} }
// FIXME remove - keep one function
function buildEbook(allPages, fromMenu=false) { function buildEbook(allPages, fromMenu=false) {
_buildEbook(allPages, fromMenu); _buildEbook(allPages, fromMenu);
} }
@ -49,10 +50,10 @@ function _buildEbook(allPages, fromMenu=false) {
if (ebookTitle) { if (ebookTitle) {
// ~TODO a pre-processing function to apply escapeXMLChars to all page.titles // ~TODO a pre-processing function to apply escapeXMLChars to all page.titles
ebookName = escapeXMLChars(ebookTitle); ebookName = escapeXMLChars(ebookTitle);
ebookFileName = getEbookFileName(ebookTitle) + '.epub'; ebookFileName = getEbookFileName(removeSpecialChars(ebookTitle)) + '.epub';
} else { } else {
ebookName = escapeXMLChars(allPages[0].title); ebookName = escapeXMLChars(allPages[0].title);
ebookFileName = getEbookFileName(allPages[0].title) + '.epub'; ebookFileName = getEbookFileName(removeSpecialChars(allPages[0].title)) + '.epub';
} }
var zip = new JSZip(); var zip = new JSZip();
@ -180,21 +181,22 @@ function _buildEbook(allPages, fromMenu=false) {
var done = false; var done = false;
// FIXME // FIXME
var saveData = (function () { // var saveData = (function () {
var a = document.createElement("a"); // var a = document.createElement("a");
document.body.appendChild(a); // document.body.appendChild(a);
a.style = "display: none"; // a.style = "display: none";
return function (data, fileName) { // return function (data, fileName) {
var wURL = window.URL || window.mozURL; // var wURL = window.URL || window.mozURL;
var blob = new Blob([data], {type: "application/epub+zip"}), // var blob = new Blob([data], {type: "application/epub+zip"}),
url = wURL.createObjectURL(blob); // url = wURL.createObjectURL(blob);
//
chrome.downloads.download({ // chrome.downloads.download({
url: url, // url: url,
filename: fileName // filename: fileName,
}); // saveAs: true
}; // });
}()); // };
// }());
zip.generateAsync({ zip.generateAsync({
type: "blob" type: "blob"
@ -202,11 +204,7 @@ function _buildEbook(allPages, fromMenu=false) {
.then(function(content) { .then(function(content) {
done = true; done = true;
console.log("done !"); console.log("done !");
if (!fromMenu) { saveAs(content, ebookFileName);
saveAs(content, ebookFileName);
} else {
saveData(content, ebookFileName);
}
}); });
// FIXME - remove or fix? // FIXME - remove or fix?

View file

@ -318,6 +318,11 @@ function generateRandomTag(tagLen) {
return text; return text;
} }
function removeSpecialChars(text) {
// FIXME remove white spaces ?
return text.replace(/\//g, '-')
}
function escapeXMLChars(text) { function escapeXMLChars(text) {
return text.replace(/&/g, '&amp;') return text.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')