diff options
Diffstat (limited to 'pixiv.js')
-rw-r--r-- | pixiv.js | 170 |
1 files changed, 111 insertions, 59 deletions
@@ -1,12 +1,12 @@ // INFO // var INFO = -<plugin name="pixiv.js" version="0.5" +<plugin name="pixiv.js" version="0.7.2" summary="Download image from pixiv" href="http://github.com/vimpr/vimperator-plugins/blob/master/pixiv.js" xmlns="http://vimperator.org/namespaces/liberator"> <author email="mitsugu.oyama@gmail.com">Mitsugu Oyama</author> <license href="http://opensource.org/licenses/mit-license.php">MIT</license> - <project name="Vimperator" minVersion="2.3"/> + <project name="Vimperator" minVersion="3.2"/> <p> You can save image from pixiv by this plugin. </p> @@ -15,6 +15,7 @@ var INFO = <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 under of plugin/modules.</p> <p>You must login pixiv.</p> </description> </item> @@ -29,21 +30,69 @@ commands.addUserCommand( liberator.echoerr('This page is not pixiv.'); return false; } - if(contents.URL.search(/medium&illust_id=/i)==-1){ + 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; } - let Cc=Components.classes; - let Ci=Components.interfaces; + const Cc=Components.classes; + const Ci=Components.interfaces; + + 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; + }; + let worker=createWorker('libDLImage.js'); + if(worker==null){ + liberator.echoerr('plugin directory is not found'); + return false; + } + worker.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); + worker.addEventListener('error',function(event){ + liberator.echoerr(event.data.status); + },false); let id; - if(-1==contents.URL.search(/\&from_sid=/i)){ - id=contents.URL.substr(contents.URL.lastIndexOf('=')+1); + 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{ - let st=contents.URL.search(/illust_id=/i)+'illust_id='.length; - let end=contents.URL.lastIndexOf('&'); - id=contents.URL.substr(st,end-st); + id=idTmp[1]; } let baseInfo; @@ -67,18 +116,13 @@ commands.addUserCommand( let fp=Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); fp.init(window,'Select Directory',Ci.nsIFilePicker.modeGetFolder); let result=fp.show(); - switch(result){ - case Ci.nsIFilePicker.returnOK: - path=fp.file.path; - break; - default: - case Ci.nsIFilePicker.returnCancel: - return ''; + if(result==Ci.nsIFilePicker.returnOK){ + return fp.file; } - return path; + return null; }; - let savePath=directoryPicker(); - if(savePath.length<1) return; + let saveDirectory=directoryPicker(); + if(saveDirectory==null) return false; let getDOMHtmlDocument=function(str){ let doc; @@ -125,54 +169,57 @@ commands.addUserCommand( }; let imgUrl; + let destPath; - let truePixivImg=function(){ - let fileName=imgUrl.substr(imgUrl.lastIndexOf('/')); - if (-1!=fileName.indexOf('?')){ - fileName=fileName.substr(0,fileName.indexOf('?')); - } - let tmpPath=savePath+fileName; - let instream=xhrImg.responseText; - let aFile=Cc["@mozilla.org/file/local;1"] - .createInstance(Ci.nsILocalFile); - aFile.initWithPath(tmpPath); - 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(); - } - }; - - let falsePixivImg=function(){ - liberator.echoerr("Image file accept error."); - return false; + let saveImage=function(){ + let objMessage={ + imageUrl :'', + savePath :'', + refererUrl:'', + cookie :'' + }; + objMessage.imageUrl=imgUrl; + objMessage.savePath=destPath.path; + objMessage.refererUrl=contents.URL; + objMessage.cookie=cookie; + let JSONmessage=JSON.stringify(objMessage); + worker.postMessage(JSONmessage); }; - let saveImage=function(){ - xhrImg=Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] - .createInstance(); - xhrImg.QueryInterface(Ci.nsIDOMEventTarget); - xhrImg.addEventListener("load",truePixivImg,false); - xhrImg.addEventListener("error",falsePixivImg,false); - xhrImg.QueryInterface(Ci.nsIXMLHttpRequest); - xhrImg.open("GET",imgUrl,false); - xhrImg.overrideMimeType('text/plain;charset=x-user-defined'); - xhrImg.setRequestHeader('Referer',contents.URL); - xhrImg.setRequestHeader('Cookie',cookie); - xhrImg.send(null); + 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; }; let saveImageFile=function(){ imgUrl=getImageUrl(xhrImgInfo.responseText); if(0<imgUrl.length){ + destPath=getDestPath(imgUrl); + if(destPath==null){ + return false; + }; saveImage(); }else{ liberator.echoerr("You should login pixiv :<"); - } + }; }; let getImageUrls=function(pageContents){ @@ -219,6 +266,10 @@ commands.addUserCommand( if(-1!=pnt){ imgUrl=imgUrl.substr(0,pnt); } + destPath=getDestPath(imgUrl); + if(destPath==null){ + continue; + } saveImage(); } }else{ @@ -252,6 +303,7 @@ commands.addUserCommand( xhrImgInfo.setRequestHeader('Referer',contents.URL); xhrImgInfo.setRequestHeader('Cookie',cookie); xhrImgInfo.send(null); - - } + }, + {}, + true ); |