;
commands.addUserCommand(
['pixiv'],
'Save Image File from pixiv',
function(){
let contents=gBrowser.selectedBrowser.contentDocument;
if(contents.domain!="www.pixiv.net"){
liberator.echoerr('This page is not pixiv.');
return false;
}
if(contents.URL.search(/medium&illust_id=/i)==-1){
liberator.echoerr("This page is not pixiv's image page.");
return false;
}
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);
}else{
let st=contents.URL.search(/illust_id=/i)+'illust_id='.length;
let end=contents.URL.lastIndexOf('&');
id=contents.URL.substr(st,end-st);
}
let baseInfo;
let scroll;
let type=contents.getElementsByClassName('works_display').item(0)
.firstChild.getAttribute('href');
if(-1!=type.search(/big&illust_id=/i)){
baseInfo="http://www.pixiv.net/member_illust.php?mode=big&illust_id=";
scroll='';
}else if(-1!=type.search(/manga&illust_id=/i)){
baseInfo="http://www.pixiv.net/member_illust.php?mode=manga&illust_id=";
scroll='&type=scroll';
}else{
liberator.echoerr("This page is not image page and not manga page.");
return false;
}
let cookie=contents.cookie;
let directoryPicker=function() {
let path;
let fp=Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window,'Select Directory',Ci.nsIFilePicker.modeGetFolder);
let result=fp.show();
if(result==Ci.nsIFilePicker.returnOK){
return fp.file;
}
return null;
};
let saveDirectory=directoryPicker();
if(saveDirectory==null) return false;
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;
};
let getImageUrl=function(pageContents){
let url;
let htmldoc=getDOMHtmlDocument(pageContents);
if(htmldoc){
if(0