Skip to content

Commit

Permalink
Tried to fix #18 (can`t zip / generate blob object when out of memory…
Browse files Browse the repository at this point in the history
… on Firefox) by detecting if browser throw an error; wrapped and ellipsised file name on progress table

检测浏览器是否抛出错误以尝试修复 #18(在 Firefox 下当内存不足时无法生成 zip 文件);在进度栏中隐藏超出长度的文件名
  • Loading branch information
ccloli committed Mar 14, 2016
1 parent 37e5712 commit 5c84981
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion e-hentai-downloader.meta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name E-Hentai Downloader
// @version 1.19.1
// @version 1.19.2
// @description Download E-Hentai archive as zip file
// @author 864907600cc
// @icon https://secure.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
Expand Down
51 changes: 27 additions & 24 deletions e-hentai-downloader.user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name E-Hentai Downloader
// @version 1.19.1
// @version 1.19.2
// @description Download E-Hentai archive as zip file
// @author 864907600cc
// @icon https://secure.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
Expand Down Expand Up @@ -9702,8 +9702,8 @@ var ehDownloadStyle = '\
.ehD-setting { position: fixed; left: 0; right: 0; top: 0; bottom: 0; padding: 5px; border: 1px solid #000000; background: #34353b; color: #dddddd; width: 550px; height: 550px; max-width: 100%; max-height: 100%; overflow: auto; box-sizing: border-box; margin: auto; z-index: 999; text-align: left; font-size: 12px; }\
.ehD-setting .g2 { padding-bottom: 10px; }\
.ehD-setting input, .ehD-box input { vertical-align: middle; }\
.ehD-pt {}\
.ehD-pt-name { word-break: break-all; }\
.ehD-pt { table-layout: fixed; width: 100%; }\
.ehD-pt-name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }\
.ehD-pt-progress-outer { width: 160px; position: relative; }\
.ehD-pt-progress { width: 150px; }\
.ehD-pt-progress-text { position: absolute; width: 100%; text-align: center; color: #34353b; left: 0; right: 0; }\
Expand Down Expand Up @@ -9852,6 +9852,20 @@ String.prototype.replaceOrigin = function() {
return isREH ? this.replace('g.e-hentai.org', 'r.e-hentai.org').toString() : this.toString();
};

function createBlob(abdata, config) {
try { // to detect if blob generates successfully
return new Blob(abdata, config);
}
catch (error) {
pushDialog('An error occurred when generating Blob object.');
console.error('[EHD] An error occurred when generating Blob object. Error Name >', error.name, '| Error Message >', error.message);
if (confirm('An error occurred when generating Blob object.\n\nError Name: ' + error.name + '\nError message: ' + error.message + '\n\n Try again?')) return createBlob(abdata, config);

abdata = undefined;
throw new Error('[EHD] An error occurred when generating Blob object, and user refused to retry.');
}
}

// show info in dialog box
function pushDialog(str) {
if (typeof str === 'string') ehDownloadDialog.innerHTML += str.replace(/\n/gi, '<br>');
Expand Down Expand Up @@ -9956,7 +9970,7 @@ function generateZip(isFromFS, fs, isRetry){
imageList.forEach(function(elem, index){
infoStr += '\n\nPage ' + elem['realIndex'] + ': ' + elem['pageURL'] + '\nImage ' + elem['realIndex'] + ': ' + elem['imageName'] /*+ '\nImage URL: ' + elem['imageURL']*/; // Image URL may useless, see https://github.com/ccloli/E-Hentai-Downloader/issues/6
});
pushDialog('\n\nFinish downloading at ' + new Date());
pushDialog('\nFinish downloading at ' + new Date() + '\n');
infoStr += '\n\nFinish downloading at ' + new Date() + '\n\nGenerated by E-Hentai Downloader. https://github.com/ccloli/E-Hentai-Downloader';
zip.folder(dirName).file('info.txt', infoStr.replace(/\n/gi, '\r\n'));
}
Expand Down Expand Up @@ -9987,7 +10001,7 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile('raw/' + files[fileIndex]['name'], {create: true}, function(fileEntry){
fileEntry.createWriter(function(fileWriter){
console.log('[EHD] FileIndex >', fileIndex, '| FilesLength >', filesLength);
var blob = new Blob([files[fileIndex].asArrayBuffer()], {type: 'application/octet-stream'});
var blob = createBlob([files[fileIndex].asArrayBuffer()], {type: 'application/octet-stream'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand All @@ -9997,7 +10011,7 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile('config.txt', {create: true}, function(fileEntry){
fileEntry.createWriter(function(fileWriter){
var t = JSON.stringify({fileName: fileName, dirName: dirName});
var blob = new Blob([t], {type: 'text/plain'});
var blob = createBlob([t], {type: 'text/plain'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand Down Expand Up @@ -10036,7 +10050,7 @@ function generateZip(isFromFS, fs, isRetry){
// I tried setting it as 100MB but some parts were still gone, so I have to make it smaller.
console.log('[EHD] DataIndex >', dataIndex, '| DataLastIndex >', dataLastIndex, '| FileWriterLength >', fileWriter.length, '| DataLength >', dataLength);
pushDialog('\n' + dataIndex + '-' + dataLastIndex + '/' + dataLength);
var blob = new Blob([data.slice(dataIndex, dataLastIndex)], {type: 'application/zip'});
var blob = createBlob([data.slice(dataIndex, dataLastIndex)], {type: 'application/zip'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand All @@ -10046,26 +10060,14 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile(unsafeWindow.gid + '.zip', {create: true}, loopWrite, ehDownloadFS.errorHandler);
}
else { // or just using blob
try {
blobObj = new Blob([abData], {type: 'application/zip'});
}
catch (error) {
abData = undefined;

pushDialog('An error occurred when generating Zip file as Blob.');
console.error('[EHD] An error occurred when generating Zip file as Blob.');
console.error(error);
if (confirm('An error occurred when generating Zip file as Blob. Try again?')) return generateZip(isFromFS, fs, 1);

return;
}

blobObj = createBlob([abData], {type: 'application/zip'});
saveAs(blobObj, fileName + '.zip');

var redownloadBtn = document.createElement('button');
redownloadBtn.textContent = 'Not download? Click here to download';
redownloadBtn.addEventListener('click', function(){
// rebuild blob object if "File is not exist" occured
blobObj = new Blob([abData], {type: 'application/zip'});
blobObj = createBlob([abData], {type: 'application/zip'});
saveAs(blobObj, fileName + '.zip');
});
ehDownloadDialog.appendChild(redownloadBtn);
Expand Down Expand Up @@ -10678,7 +10680,7 @@ function initEHDownload() {

function initProgressTable(){
progressTable = document.createElement('table');
progressTable.style.width = '100%';
progressTable.className = 'ehD-pt';
ehDownloadDialog.style.display = 'block';
ehDownloadDialog.appendChild(progressTable);
}
Expand Down Expand Up @@ -10713,6 +10715,7 @@ function getPageData(index) {
</td>\
<td class="ehD-pt-status">Fetching URL...</td>';
progressTable.appendChild(node);
ehDownloadDialog.scrollTop = ehDownloadDialog.scrollHeight;

var nodeList = {
current: node,
Expand Down Expand Up @@ -10941,7 +10944,7 @@ document.body.appendChild(ehDownloadDialog);

window.onbeforeunload = function(){
ehDownloadFS.removeFile(unsafeWindow.gid + '.zip');
if (isDownloading) return 'E-Hentai Downloader is still running, please don\'t close this tab before it finish downloading.';
if (isDownloading) return 'E-Hentai Downloader is still running, please don\'t close this tab before it finished downloading.';
};

// Forced request File System to check if have temp archive
Expand Down
49 changes: 26 additions & 23 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ var ehDownloadStyle = '\
.ehD-setting { position: fixed; left: 0; right: 0; top: 0; bottom: 0; padding: 5px; border: 1px solid #000000; background: #34353b; color: #dddddd; width: 550px; height: 550px; max-width: 100%; max-height: 100%; overflow: auto; box-sizing: border-box; margin: auto; z-index: 999; text-align: left; font-size: 12px; }\
.ehD-setting .g2 { padding-bottom: 10px; }\
.ehD-setting input, .ehD-box input { vertical-align: middle; }\
.ehD-pt {}\
.ehD-pt-name { word-break: break-all; }\
.ehD-pt { table-layout: fixed; width: 100%; }\
.ehD-pt-name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }\
.ehD-pt-progress-outer { width: 160px; position: relative; }\
.ehD-pt-progress { width: 150px; }\
.ehD-pt-progress-text { position: absolute; width: 100%; text-align: center; color: #34353b; left: 0; right: 0; }\
Expand Down Expand Up @@ -343,6 +343,20 @@ String.prototype.replaceOrigin = function() {
return isREH ? this.replace('g.e-hentai.org', 'r.e-hentai.org').toString() : this.toString();
};

function createBlob(abdata, config) {
try { // to detect if blob generates successfully
return new Blob(abdata, config);
}
catch (error) {
pushDialog('An error occurred when generating Blob object.');
console.error('[EHD] An error occurred when generating Blob object. Error Name >', error.name, '| Error Message >', error.message);
if (confirm('An error occurred when generating Blob object.\n\nError Name: ' + error.name + '\nError message: ' + error.message + '\n\n Try again?')) return createBlob(abdata, config);

abdata = undefined;
throw new Error('[EHD] An error occurred when generating Blob object, and user refused to retry.');
}
}

// show info in dialog box
function pushDialog(str) {
if (typeof str === 'string') ehDownloadDialog.innerHTML += str.replace(/\n/gi, '<br>');
Expand Down Expand Up @@ -447,7 +461,7 @@ function generateZip(isFromFS, fs, isRetry){
imageList.forEach(function(elem, index){
infoStr += '\n\nPage ' + elem['realIndex'] + ': ' + elem['pageURL'] + '\nImage ' + elem['realIndex'] + ': ' + elem['imageName'] /*+ '\nImage URL: ' + elem['imageURL']*/; // Image URL may useless, see https://github.com/ccloli/E-Hentai-Downloader/issues/6
});
pushDialog('\n\nFinish downloading at ' + new Date());
pushDialog('\nFinish downloading at ' + new Date() + '\n');
infoStr += '\n\nFinish downloading at ' + new Date() + '\n\nGenerated by E-Hentai Downloader. https://github.com/ccloli/E-Hentai-Downloader';
zip.folder(dirName).file('info.txt', infoStr.replace(/\n/gi, '\r\n'));
}
Expand Down Expand Up @@ -478,7 +492,7 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile('raw/' + files[fileIndex]['name'], {create: true}, function(fileEntry){
fileEntry.createWriter(function(fileWriter){
console.log('[EHD] FileIndex >', fileIndex, '| FilesLength >', filesLength);
var blob = new Blob([files[fileIndex].asArrayBuffer()], {type: 'application/octet-stream'});
var blob = createBlob([files[fileIndex].asArrayBuffer()], {type: 'application/octet-stream'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand All @@ -488,7 +502,7 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile('config.txt', {create: true}, function(fileEntry){
fileEntry.createWriter(function(fileWriter){
var t = JSON.stringify({fileName: fileName, dirName: dirName});
var blob = new Blob([t], {type: 'text/plain'});
var blob = createBlob([t], {type: 'text/plain'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand Down Expand Up @@ -527,7 +541,7 @@ function generateZip(isFromFS, fs, isRetry){
// I tried setting it as 100MB but some parts were still gone, so I have to make it smaller.
console.log('[EHD] DataIndex >', dataIndex, '| DataLastIndex >', dataLastIndex, '| FileWriterLength >', fileWriter.length, '| DataLength >', dataLength);
pushDialog('\n' + dataIndex + '-' + dataLastIndex + '/' + dataLength);
var blob = new Blob([data.slice(dataIndex, dataLastIndex)], {type: 'application/zip'});
var blob = createBlob([data.slice(dataIndex, dataLastIndex)], {type: 'application/zip'});
fileWriter.write(blob);
if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser now
blob = null;
Expand All @@ -537,26 +551,14 @@ function generateZip(isFromFS, fs, isRetry){
fs.root.getFile(unsafeWindow.gid + '.zip', {create: true}, loopWrite, ehDownloadFS.errorHandler);
}
else { // or just using blob
try {
blobObj = new Blob([abData], {type: 'application/zip'});
}
catch (error) {
abData = undefined;

pushDialog('An error occurred when generating Zip file as Blob.');
console.error('[EHD] An error occurred when generating Zip file as Blob.');
console.error(error);
if (confirm('An error occurred when generating Zip file as Blob. Try again?')) return generateZip(isFromFS, fs, 1);

return;
}

blobObj = createBlob([abData], {type: 'application/zip'});
saveAs(blobObj, fileName + '.zip');

var redownloadBtn = document.createElement('button');
redownloadBtn.textContent = 'Not download? Click here to download';
redownloadBtn.addEventListener('click', function(){
// rebuild blob object if "File is not exist" occured
blobObj = new Blob([abData], {type: 'application/zip'});
blobObj = createBlob([abData], {type: 'application/zip'});
saveAs(blobObj, fileName + '.zip');
});
ehDownloadDialog.appendChild(redownloadBtn);
Expand Down Expand Up @@ -1169,7 +1171,7 @@ function initEHDownload() {

function initProgressTable(){
progressTable = document.createElement('table');
progressTable.style.width = '100%';
progressTable.className = 'ehD-pt';
ehDownloadDialog.style.display = 'block';
ehDownloadDialog.appendChild(progressTable);
}
Expand Down Expand Up @@ -1204,6 +1206,7 @@ function getPageData(index) {
</td>\
<td class="ehD-pt-status">Fetching URL...</td>';
progressTable.appendChild(node);
ehDownloadDialog.scrollTop = ehDownloadDialog.scrollHeight;

var nodeList = {
current: node,
Expand Down Expand Up @@ -1432,7 +1435,7 @@ document.body.appendChild(ehDownloadDialog);

window.onbeforeunload = function(){
ehDownloadFS.removeFile(unsafeWindow.gid + '.zip');
if (isDownloading) return 'E-Hentai Downloader is still running, please don\'t close this tab before it finish downloading.';
if (isDownloading) return 'E-Hentai Downloader is still running, please don\'t close this tab before it finished downloading.';
};

// Forced request File System to check if have temp archive
Expand Down

0 comments on commit 5c84981

Please sign in to comment.