Show
Ignore:
Timestamp:
12/23/07 22:28:17 (13 months ago)
Author:
cho45
Message:

lang/javascript/userchrome/editonfavorite.uc.js:

IO つかうように

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userchrome/editonfavorite.uc.js

    r3483 r3491  
    11(function (Global) { 
    2  
    3 function File (path) { 
    4         if (this instanceof arguments.callee) { 
    5                 if (typeof path == "string") { 
    6                         this.file = Components.classes["@mozilla.org/file/local;1"] 
    7                                               .createInstance(Components.interfaces.nsILocalFile); 
    8                         this.file.initWithPath(path); 
    9                 } else { 
    10                         this.file = path; 
    11                 } 
    12                 this.charset = "UTF-8"; 
    13         } else { 
    14                 return new File(path); 
    15         } 
    16 } 
    17 File.prototype = { 
    18         get path () { 
    19                 return this.file.path; 
    20         }, 
    21  
    22         get lastModifiedTime () { 
    23                 return this.file.lastModifiedTime; 
    24         }, 
    25  
    26         read : function (cb) { 
    27                 if (!cb) cb = function (i) { 
    28                         var ret = []; 
    29                         var str = {}; 
    30                         while (i.readString(4096, str) != 0) { 
    31                                 ret.push(str.value); 
    32                         } 
    33                         return ret.join(""); 
    34                 }; 
    35                 var fistream = Components.classes["@mozilla.org/network/file-input-stream;1"] 
    36                                          .createInstance(Components.interfaces.nsIFileInputStream); 
    37                 var cistream = Components.classes["@mozilla.org/intl/converter-input-stream;1"] 
    38                                          .createInstance(Components.interfaces.nsIConverterInputStream); 
    39                 fistream.init(this.file, 0x01, 0444, 0); 
    40                 cistream.init(fistream, this.charset, 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); 
    41                 var ret = cb.call(this, cistream, fistream); 
    42                 cistream.close(); 
    43                 fistream.close(); 
    44                 return ret; 
    45         }, 
    46         write : function (cb) { 
    47                 if (typeof cb == "string") { 
    48                         var _str = cb; 
    49                         cb = function (i) { 
    50                                 i.writeString(_str); 
    51                                 return this; 
    52                         }; 
    53                 } 
    54                 var fostream = Components.classes["@mozilla.org/network/file-output-stream;1"] 
    55                                          .createInstance(Components.interfaces.nsIFileOutputStream); 
    56                 var costream = Components.classes["@mozilla.org/intl/converter-output-stream;1"] 
    57                                          .createInstance(Components.interfaces.nsIConverterOutputStream); 
    58                 fostream.init(this.file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate 
    59                 costream.init(fostream, this.charset, 4096, 0x0000); 
    60                 var ret = cb.call(this, costream, fostream); 
    61                 costream.close(); 
    62                 fostream.close(); 
    63                 return ret; 
    64         } 
    65 }; 
    66 File.TempFile = function (prefix, suffix) { 
    67         var name = [prefix, Math.floor(Math.random() * 0xffffff).toString(16), suffix].join("."); 
    68         var file = Components.classes["@mozilla.org/file/directory_service;1"] 
    69                              .getService(Components.interfaces.nsIProperties) 
    70                              .get("TmpD", Components.interfaces.nsIFile); 
    71         file.append(name); 
    72         file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0664); 
    73  
    74         Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"] 
    75                   .getService(Components.interfaces.nsPIExternalAppLauncher) 
    76                   .deleteTemporaryFileOnExit(file); 
    77  
    78         return File(file); 
    79 } 
    802 
    813const NAME = "EditOnFavorite"; 
     
    10527                                return self.onDOMContentLoad(e); 
    10628                        }, true); 
    107                         appcontent.addEventListener("unload", function (e) { 
     29                        // リンククリックしたときに、なぜか unload イベントが発生しない。意味不明 
     30                        window.addEventListener("unload", function (e) { 
    10831                                return self.onunload(e); 
    10932                        }, true); 
     
    12750        onkeypress : function (e) { 
    12851                var self = this; 
    129                 log([e.ctrlKey, String.fromCharCode(e.charCode)]); 
     52                // log([e.ctrlKey, String.fromCharCode(e.charCode)]); 
    13053                if (e.ctrlKey && String.fromCharCode(e.charCode) == 'e') { 
    13154                        var value = e.target.value; 
    132                         var tf = File.TempFile("editonfavorite", "txt"); 
    133                         tf.write(value); 
     55                        var file  = IO.getFile("Temp", ["editonfavorite", Math.floor(Math.random() * 0xffffff).toString(16), "txt"].join(".")); 
     56                        var strm  = IO.newOutputStream(file, "text"); 
     57                        strm.writeString(value); 
     58                        strm.close(); 
    13459 
    135                         var args = [tf.path]; 
     60                        var args = [file.path]; 
    13661                        var ps = Components.classes["@mozilla.org/process/util;1"] 
    13762                                           .createInstance(Components.interfaces.nsIProcess); 
    138                         ps.init(File("/usr/bin/open").file); 
     63                        ps.init(IO.getFileWithPath("/usr/bin/open")); 
    13964                        args.unshift("-a", "Vim"); 
    14065                        ps.run(false, args, args.length); 
    141                         self.watching[tf.path] = { 
    142                                 file: tf, 
     66                        self.watching[file.path] = { 
     67                                file: file, 
    14368                                time: (new Date()).getTime(), 
    14469                                doc: e.target.ownerDocument, 
     
    15479 
    15580        onunload : function (e) { 
     81                log("unload"+e.originalTarget); 
    15682                var self = this; 
    15783                for (var k in self.watching) { 
    15884                        if (!self.watching.hasOwnProperty(k)) continue; 
    15985                        var w = self.watching[k]; 
     86                        log(w.doc == e.originalTarget); 
    16087                        if (w.doc == e.originalTarget) { 
    16188                                log("unwatch: "+w.file.path); 
     
    17299                        if (w.time < w.file.lastModifiedTime) { 
    173100                                log("changed:"+w.file.path); 
    174                                 w.target.value = w.file.read(); 
     101                                var str, strm = IO.newInputStream(w.file, "text"); 
     102                                w.target.value = ""; 
     103                                while (str = strm.readString(4096)) { 
     104                                        w.target.value += str; 
     105                                } 
     106                                strm.close(); 
    175107                                w.time = w.file.lastModifiedTime; 
    176108                        }