-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
179 lines (139 loc) · 4.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var book;
(function ($) {
'use strict';
var $droptarget = $('body');
var $progressbar = $('.bar');
function extractImages(file, opts) {
var images = [];
var fr = new FileReader();
// var file = files[0];
var re_file_ext = new RegExp(/\.(\w+)$/);
var archive_class = ({ cbz: 'Unzipper', cbr: 'Unrarrer' })[file.name.toLowerCase().match(re_file_ext)[1]];
var options = $.extend({
start: function () {},
extract: function (page_url) {},
progress: function (percent_complete) {},
finish: function (images) {},
}, opts);
if (!archive_class) {
alert('invalid file type, only cbz and cbr are supported.');
return false;
}
options.start(file);
fr.onload = function () {
var done = false;
var ua = new bitjs.archive[archive_class](this.result, 'lib/bitjs/');
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT, function (e) {
var mimetype, blob, url;
var file_extension = e.unarchivedFile.filename.toLowerCase().match(re_file_ext)[1];
switch (file_extension) {
case 'jpg':
case 'jpeg':
mimetype = 'image/jpeg';
break;
case 'png':
mimetype = 'image/png';
break;
case 'gif':
mimetype = 'image/gif';
break;
default:
return false;
}
blob = new Blob([e.unarchivedFile.fileData], { type: mimetype });
url = window.URL.createObjectURL(blob);
images.push(url);
options.extract(url, blob);
});
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS, function (e) {
options.progress(Math.floor(e.currentBytesUnarchived / e.totalUncompressedBytesInArchive * 100));
});
ua.addEventListener(bitjs.archive.UnarchiveEvent.Type.FINISH, function (e) {
options.finish(images);
});
ua.start();
};
fr.readAsArrayBuffer(file);
}
function openComicArchive(file) {
extractImages(file, {
start: function (file) {
this.file = file;
$('#open').hide();
$('#filename').text(file.name);
$('#progressbar').show();
},
extract: function (url, blob) {
// $('body').append($('<img>').attr('src', url).css('width', '10px'));
// console.log(url, Math.floor(blob.size / 1024));
},
progress: function (percent_complete) {
$progressbar.css('width', percent_complete + '%');
},
finish: function (pages) {
var name = this.file.name.replace(/\.[a-z]+$/, '');
var id = encodeURIComponent(name.toLowerCase());
var app_window = chrome.app.window.current();
var initial_bounds = app_window.getBounds();
book = new ComicBook('comic', pages, {
// start: localStorage.getItem(id + '_last_page')
libPath: 'lib/comicbook/js/'
});
$(book).on('navigate', function (e) {
// console.log(e);
// localStorage.setItem(id + '_last_page', page_number);
});
$('#progressbar').hide();
$('.bar').css('width', 0);
$('#filepicker').hide();
$('#comic-wrapper').show();
book.draw();
// TODO: fit to page width instead of maximising
app_window.maximize();
$('.icon-remove-sign').on('click', function () {
book.destroy();
book = null;
$(window).off('resize');
$('#open').show();
$('#comic-wrapper').hide();
$('#filepicker').show();
// hackfix for page freezing bug, would prefer to use app_window.restore()
// https://code.google.com/p/chromium/issues/detail?id=236105
setTimeout(function () {
window.resizeTo(initial_bounds.width, initial_bounds.height);
app_window.moveTo(initial_bounds.left, initial_bounds.top);
}, 200);
return false;
});
$(window).on('resize', function () {
book.draw();
});
}
});
}
document.body.addEventListener('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
this.classList.add('dragover');
}, false);
document.body.addEventListener('dragleave', function (e) {
e.stopPropagation();
e.preventDefault();
this.classList.remove('dragover');
}, false);
document.body.addEventListener('drop', function (e) {
e.stopPropagation();
e.preventDefault();
this.classList.remove('dragover');
openComicArchive(e.dataTransfer.files[0]);
}, false);
document.querySelector('#open button').addEventListener('click', function (e) {
chrome.fileSystem.chooseEntry({ type: 'openFile' }, function (entry) {
if (entry) {
entry.file(function (file) {
openComicArchive(file);
});
}
});
}, false);
})(jQuery);