aboutsummaryrefslogtreecommitdiffstats
path: root/simg.js
blob: 8100db0c841d75e7fd7c804b261d2fa1d26f88e5 (plain)
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
// INFO //
var INFO =
<plugin name="simg.js" version="0.3"
        summary="Save image on contents area"
        href="http://github.com/vimpr/vimperator-plugins/blob/master/simg.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"/>
  <p>
    You can save image on the currnet context area by this plugin.
  </p>
  <item>
    <tags>'simg'</tags>
    <spec>:simg</spec>
    <description>
      <p>You can save image on the currnet context area by this plugin.</p>
    </description>
  </item>
</plugin>;

commands.addUserCommand(
  ['simg'],
  'Save Image File current page',
  function(){
    let contents=gBrowser.selectedBrowser.contentDocument;
    let Cc=Components.classes;
    let Ci=Components.interfaces;
    let cookie=contents.cookie;
    let xhrImg;

    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();
      switch(result){
        case Ci.nsIFilePicker.returnOK:
          path=fp.file.path;
          break;
        default:
        case Ci.nsIFilePicker.returnCancel:
          return '';
      }
      return path;
    };

    let saveDirectory=directoryPicker();
    if(saveDirectory.length<1) return;
    let imgURL=contents.URL;
    let savePath;

    let trueCurrntImg=function(){
      let fileName=imgURL.substr(imgURL.lastIndexOf('/'));
      if (-1!=fileName.indexOf('?')){
        fileName=fileName.substr(0,fileName.indexOf('?'));
      }
      savePath=saveDirectory+fileName;
      let instream=xhrImg.responseText;
      let aFile=Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
      aFile.initWithPath(savePath);
      if(true===aFile.exists()){
        let value=window.prompt('すでに同じ名前のファイルがあります。デフォルトファイル名を変更してください。',fileName.substr(1));
        if(null===value){
          return false;
        }
        fileName='/'+value;
        savePath=saveDirectory+fileName;
        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();
      }
    };
    let falseCurrntImg=function(){
      liberator.echo("Image file accept error.");
      return false;
    };

    xhrImg=Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
      .createInstance();
    xhrImg.QueryInterface(Ci.nsIDOMEventTarget);
    xhrImg.addEventListener("load",trueCurrntImg,false);
    xhrImg.addEventListener("error",falseCurrntImg,false);
    xhrImg.QueryInterface(Ci.nsIXMLHttpRequest);
    xhrImg.open("GET",imgURL,true);
    xhrImg.overrideMimeType('text/plain;charset=x-user-defined');
    xhrImg.setRequestHeader('Referer',contents.URL);
    xhrImg.setRequestHeader('Cookie',cookie);
    xhrImg.send(null);
  }
);