-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-large-images-pixiv.bookmarklet.js
147 lines (133 loc) · 3.79 KB
/
list-large-images-pixiv.bookmarklet.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
// javascript:new function(s,d){d=document;s=d.createElement('script');s.charset='utf-8';s.src='https://raw.github.com/yoko/bookmarklets/master/list-large-images-pixiv.bookmarklet.js';d.body.appendChild(s)}
// @title List Large Images (pixiv)
// @include http://www.pixiv.net/*
// @license MIT License
(function(w, d, $) { 'use strict';
var completed = false;
var container = $('\
<div class="introduction-modal-container" style="display:block;">\
<section class="introduction-modal">\
<div class="close ui-modal-close"></div>\
<ul></ul>\
</section>\
</div>\
').appendTo('body');
pixiv.ui.modal.push(container, {block: true});
container = $('ul', container);
var url = location.href,
page = $('.pager-container').length ? 1 : 0,
latest_id = w.prompt('latest id:');
(function loop() {
$.get(url, page ? {p: page} : null).then(function(html) {
var images = [];
$(html).find('img').each(function() {
// 1. http://i\d+\.pixiv\.net/img-inf/img/\d{4}/\d{2}/\d{2}/\d{2}/\d{2}/\d{2}/\d+_s\.(jpg|png|gif)
// 2. http://i\d+\.pixiv\.net/img\d+/img/[\w-]+/\d+_s\.(jpg|png|gif)
var src = this.dataset.src || this.src,
m = /(\d+)_s\.(?:jpg|png|gif)(?:\?\d+)?$/.exec(src);
if (!m) return;
if (m[1] === latest_id) {
completed = true;
return false;
}
images.push(this);
});
if (!images.length) return;
console.log('images', images);
findImage(images, page).then(function() {
if (!page || !latest_id || completed) {
console.log('completed');
$('<p>completed</p>').appendTo(container);
}
else {
console.log('next page');
++page;
loop();
}
});
});
})();
function findImage(images, page) {
return (function loop() {
var target = $(images.shift()),
url = target.closest('a').attr('href'),
src = target.dataset('src') || target.attr('src');
function next() {
if (images.length) {
return loop();
}
else {
console.log('completed page ' + page);
}
}
return wait(1000).then(function() {
console.log('findImage', url);
// mode=bigで判定すると漫画の際にmode=mediumと同じページが表示されるのでmode=mangaを見る
return $.get(url.replace('mode=medium', 'mode=manga')).then(
// エラーでも200が返るので常にdoneで受ける
function(html) {
if (html.indexOf('指定されたIDは漫画ではありません') === -1) {
console.log('maybe manga:', src);
add(findMangaURL(html));
return next();
}
else {
console.log('maybe illustration:', src);
if (oldImage(src)) {
add(src.replace(/_s(\.)/, '$1'));
return next();
}
else {
return wait(1000).then(function() {
return $.get(url).then(function(html) {
add(findURL(html));
return next();
});
});
}
}
},
function() {
console.log('error', src);
return next();
}
);
});
})();
}
function oldImage(src) {
return /\/img\d+\/img\/[\w-]+\/\d+_s\.(?:jpg|png|gif)/.test(src);
}
function findURL(text) {
var urls = text
.replace(/\s/g, '')
.match(/http:\/\/i\d+\.pixiv\.net\/img\d+\/img\/[\w-]+\/\d+_m?\.(?:jpg|png|gif)/g);
urls = urls.map(function(url) {
return url.replace(/_m(\.)/, '$1');
});
console.log('findURL', urls);
return urls;
}
function findMangaURL(text) {
var urls = text
.replace(/\s/g, '')
.match(/http:\/\/i\d+\.pixiv\.net\/img\d+\/img\/[\w-]+\/\d+_p\d+\.(?:jpg|png|gif)/g);
console.log('findMangaURL', urls);
return urls;
}
function add(urls) {
urls = $.makeArray(urls);
console.log(urls);
for (var i = 0, url; url = urls[i]; ++i) {
$('<li>' + url + '</li>').appendTo(container);
}
}
function wait(time) {
var d = $.Deferred();
setTimeout(function() {
d.resolve();
}, time);
console.log('waiting', time, new Date().getMilliseconds());
return d;
}
})(this, document, jQuery);