-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathpixiv.js
356 lines (337 loc) · 11.3 KB
/
pixiv.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// {{{ INFO
var INFO =xml`
<plugin name="pixiv.js" version="0.7.3"
summary="Download image from pixiv"
href="http://github.com/vimpr/vimperator-plugins/blob/master/pixiv.js"
xmlns="http://vimperator.org/namespaces/liberator">
<author email="[email protected]">Mitsugu Oyama</author>
<license href="http://opensource.org/licenses/mit-license.php">MIT</license>
<project name="Vimperator" minVersion="3.2"/>
<p>
You can save image from pixiv by this plugin.
</p>
<item>
<tags>'pixiv'</tags>
<spec>:pixiv</spec>
<description>
<p>You can save image from <link topic="http://www.pixiv.net/">pixiv</link> by this plugin.</p>
<p>You need libDLImage.js and libDLMangaSingleContent.js under of plugin/modules.</p>
<p>You must login pixiv.</p>
<p>This plugin is being ported to the WebExtensions add-on.Please wait for a while until completion.</p>
</description>
</item>
</plugin>`;
// }}}
commands.addUserCommand(
['pixiv'],
'Save Image File from pixiv',
function(){
// {{{ environment
let contents=gBrowser.selectedBrowser.contentDocument;
if(contents.domain!="www.pixiv.net"){
liberator.echoerr('This page is not pixiv.');
return false;
}
if((contents.URL.search(/illust_id=/i)==-1)||
(contents.URL.search(/mode=medium/i)==-1)){
liberator.echoerr("This page is not pixiv's image page.");
return false;
}
const Cc=Components.classes;
const Ci=Components.interfaces;
let cookie=contents.cookie;
// }}}
let fp;
let url;
let type;
let saveDirectory;
let xhrImgInfo;
// {{{ convert to DOM Document from text
let getDOMHtmlDocument=function(str){
let doc;
let range;
try{
if(document.implementation.createHTMLDocument){
doc=document.implementation.createHTMLDocument('');
range=doc.createRange();
range.selectNodeContents(doc.documentElement);
range.deleteContents();
doc.documentElement.appendChild(range.createContextualFragment(str));
}else{
let doctype=document.implementation.createDocumentType(
'html',
'-//W3C//DTD HTML 4.01 Transitional//EN',
'http://www.w3.org/TR/html4/loose.dtd'
);
doc=document.implementation.createDocument(null,'html',doctype);
range=doc.createRange();
range.selectNodeContents(doc.documentElement);
let content=doc.adoptNode(range.createContextualFragment(str));
doc.documentElement.appendChild(content);
}
}catch(e){
doc=null;
}
return doc;
};
// }}}
// {{{ get image id
let id;
let idTmp=contents.URL.match(/illust_id=(\d+)/i);
if(idTmp===null){
liberator.echoerr("This page is not image page and not manga page.");
return false;
}else{
id=idTmp[1];
}
// }}}
// {{{ make ChromeWorker
let createWorker=function(fileName){
let ret;
const resourceName="vimp-plugin";
const ioService=Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
const resProt=ioService.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
let pluginDirs=io.getRuntimeDirectories("plugin");
if (pluginDirs.length === 0){
return null;
}
resProt.setSubstitution(resourceName,ioService.newFileURI(pluginDirs[0]));
try {
worker=new ChromeWorker("resource://"+resourceName+"/modules/"+fileName);
}catch(e){
return null;
}
return worker;
};
// }}}
// {{{ save image ChromeWorker process
let workerImage=createWorker('libDLImage.js');
if(workerImage==null){
liberator.echoerr('plugin directory is not found');
return false;
}
workerImage.addEventListener('message',function(event){
if(event.data.status=='error'){
liberator.echoerr(event.data.message);
return false;
};
let instream=event.data.message;
let savePath=event.data.savePath;
let aFile=Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
aFile.initWithPath(savePath);
let outstream=Cc["@mozilla.org/network/safe-file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
outstream.init(aFile,0x02|0x08|0x20,0664,0);
outstream.write(instream,instream.length);
if (outstream instanceof Ci.nsISafeOutputStream) {
outstream.finish();
}else{
outstream.close();
}
},false);
workerImage.addEventListener('error',function(event){
liberator.echoerr(event.data.status);
},false);
// }}}
// {{{ Recieve Manga Contents ChromeWorker
let workerManga=createWorker('libDLMangaSingleContent.js');
workerManga.addEventListener('message',function(event){
if(event.data.status=='error'){
liberator.echoerr(event.data.message);
return false;
};
let domContent=getDOMHtmlDocument(event.data.message);
if(domContent){
// DOM で画像 URL を取得して画像ファイルの取得リクエストを発行
let imgUrl=domContent.getElementsByTagName('img')
.item(0).getAttribute('src');
let destPath=getDestPath(imgUrl);
if(destPath==null){
return false;
};
saveImage(
imgUrl,
destPath.path,
event.data.refererUrl,
cookie
);
}
},false);
workerManga.addEventListener('error',function(event){
liberator.echoerr(event.data.status);
},false);
// }}}
// {{{ send request save image
let saveImage=function(imgUrl,savePath,referer,cookie){
let objMessage={
imageUrl :'',
savePath :'',
refererUrl:'',
cookie :''
};
objMessage.imageUrl=imgUrl;
objMessage.savePath=savePath;
objMessage.refererUrl=referer;
objMessage.cookie=cookie;
let JSONmessage=JSON.stringify(objMessage);
workerImage.postMessage(JSONmessage);
};
// }}}
// {{{ get destnation fullpath name
let getDestPath=function(url){
let fname=url.substr(url.lastIndexOf('/')+1);
if(fname.lastIndexOf('?')!=-1){
fname=fname.substr(0,fname.lastIndexOf('?'));
}
let path=saveDirectory.clone();
path.append(fname);
let aFile=Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
let newPath=path.clone();
aFile.initWithFile(path);
if(true===aFile.exists()){
let value=window.prompt('すでに同じ名前のファイルがあります。デフォルトファイル名を変更してください。',fname);
if(null===value){
return null;
};
if(fname!=value){
newPath=saveDirectory.clone();
newPath.append(value);
}
}
return newPath;
};
// }}}
// {{{ save single image file
let getImageUrl=function(pContents){
let url = pContents.getElementsByClassName("original-image")[0].getAttribute( "data-src" );
return url;
};
let saveImageFile=function(imgUrl){
if(0<imgUrl.length){
let destPath=getDestPath(imgUrl);
if(destPath==null){
return false;
};
saveImage(
imgUrl,
destPath.path,
contents.URL,
cookie
);
}else{
liberator.echoerr("You should login pixiv :<");
};
};
// }}}
// {{{ save manga image file
let requestMangaSingleContent=function(url,ref){
let objMessage={
pageUrl :'',
refererUrl:'',
cookie :''
};
objMessage.pageUrl=url;
objMessage.refererUrl=ref;
objMessage.cookie=cookie;
let JSONmessage=JSON.stringify(objMessage);
workerManga.postMessage(JSONmessage);
};
let saveMangaFiles=function(){
let htmldoc=getDOMHtmlDocument(xhrImgInfo.responseText);
if(htmldoc){
let max=htmldoc.getElementsByClassName('image ui-scroll-view').length;
for(var i=0;i<max;i++){
requestMangaSingleContent(
url.replace('manga','manga_big')
.replace('type=scroll','page=')
.replace('&uarea=follower_new_illust','')+i,
url.replace('&type=scroll','')
);
}
}
};
// }}}
// {{{ trueNestImgInfo
let trueNestImgInfo=function(){
let htmldoc=getDOMHtmlDocument(xhrImgInfo.responseText);
if ( htmldoc ) {
let imgUrl = htmldoc.getElementsByTagName( 'img' )[0].getAttribute( 'src' );
saveImageFile(imgUrl);
}
};
// }}}
// {{{ trueImgInfo
let trueImgInfo=function(){
if(-1!=type.search(/manga&illust_id=/i)){
saveMangaFiles();
}else{
liberator.echoerr("This page is not image page and not manga page.");
return false;
}
};
// }}}
// {{{ falseImgInfo
let falseImgInfo=function(){
liberator.echo("Image Infomation page accept error.");
return false;
};
// }}}
// {{{ fpCallback
let fpCallback = {
done : function (aResult) {
if ( aResult == fp.returnOK ) {
saveDirectory = fp.file;
if ( url == null ) {
if ( type == null ) {
let imgUrl=getImageUrl(contents);
saveImageFile(imgUrl);
} else {
let secondUrl = "http://www.pixiv.net/"+type;
xhrImgInfo = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance();
xhrImgInfo.QueryInterface( Ci.nsIDOMEventTarget );
xhrImgInfo.addEventListener( "load", trueNestImgInfo, false );
xhrImgInfo.addEventListener( "error", falseImgInfo, false );
xhrImgInfo.QueryInterface( Ci.nsIXMLHttpRequest );
xhrImgInfo.open( "GET", secondUrl, true);
xhrImgInfo.setRequestHeader( 'Referer', contents.URL );
xhrImgInfo.setRequestHeader( 'Cookie', cookie );
xhrImgInfo.send( null );
}
} else {
xhrImgInfo = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance();
xhrImgInfo.QueryInterface( Ci.nsIDOMEventTarget );
xhrImgInfo.addEventListener( "load", trueImgInfo, false );
xhrImgInfo.addEventListener( "error", falseImgInfo, false );
xhrImgInfo.QueryInterface( Ci.nsIXMLHttpRequest );
xhrImgInfo.open( "GET", url, true);
xhrImgInfo.setRequestHeader( 'Referer', contents.URL );
xhrImgInfo.setRequestHeader( 'Cookie', cookie );
xhrImgInfo.send( null );
}
}
}
};
// }}}
type = contents.getElementsByClassName('works_display')
.item(0).firstChild.getAttribute('href');
if( type == null ){ // single iamge
url=null;
}else if(-1!=type.search(/member_illust\.php\?mode=big\&illust\_id=/i)) {
url=null;
}else if(-1!=type.search(/manga&illust_id=/i)){ // plural image
url=contents.documentURI.replace('medium','manga')+'&type=scroll';
}else{
liberator.echoerr("This page is not image page and not manga page.");
return false;
}
fp = Cc["@mozilla.org/filepicker;1"].createInstance( Ci.nsIFilePicker );
fp.init( window, 'Select Directory', fp.modeGetFolder );
let result=fp.open( fpCallback );
},
{},
true
);