swdyh (owner)

Revisions

gist: 93524 Download_button fork
public
Public Clone URL: git://gist.github.com/93524.git
Embed All Files: show embed
pixnet.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
var base_dir = '/Users/youhei/Desktop/'
main()
 
function main() {
    var dir = base_dir + document.getElementById('crumbAlb').textContent
    var list = Array.filter(document.links, function(i) {
        return /photo\/\d+#pictop$/.test(i.href)
    })
 
    if (list.length > 1) {
        createDir(dir)
        list.forEach(function(i) {
            var u = i.firstChild.src.replace('thumb_', '')
            var l = dir + '/' + u.split('/').slice(-1)[0]
            download(u, l)
            // log([u, l])
        })
    }
    else {
        log('no item.')
    }
}
 
function log(arg) {
    Firebug.Console.log(Array.prototype.slice.call(arguments))
}
 
function download(url, path) {
    const PrefBranch = Components.Constructor('@mozilla.org/preferences;1', 'nsIPrefBranch');
    const IOService = Components.Constructor('@mozilla.org/network/io-service;1', 'nsIIOService');
    const Transfer = Components.Constructor('@mozilla.org/transfer;1', 'nsITransfer');
    const WebBrowserPersist = Components.Constructor('@mozilla.org/embedding/browser/nsWebBrowserPersist;1', 'nsIWebBrowserPersist');
    const LocalFile = Components.Constructor('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath');
 
    var ios = IOService();
    var targetURI = ios.newFileURI((path instanceof Components.interfaces.nsIFile) ? path : new LocalFile(path))
    var sourceURI = ios.newURI(url, null, null);
    var persist = WebBrowserPersist();
    with(persist){
        var transfer = Transfer();
        transfer.init(sourceURI, targetURI, '', null, null, null, persist);
 
        progressListener = transfer;
        persistFlags =
            PERSIST_FLAGS_REPLACE_EXISTING_FILES |
            PERSIST_FLAGS_FROM_CACHE;
        saveURI(sourceURI, null, null, null, null, targetURI);
    }
}
 
function createDir(dir) {
    const LocalFile = Components.Constructor('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath');
    var dir = (dir instanceof Components.interfaces.nsIFile) ? dir : new LocalFile(dir);
    if (dir.exists()) {
        if (dir.isDirectory()) {
            dir.permissions = 0774;
        }
    } else {
        dir.create(dir.DIRECTORY_TYPE, 0774);
    }
    return dir;
}