Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created October 31, 2008 18:58
Show Gist options
  • Save swdyh/21393 to your computer and use it in GitHub Desktop.
Save swdyh/21393 to your computer and use it in GitHub Desktop.
// http://photo.ameba.jp/user/nakamura-miu/0791ba741a0105htt5o811/
var base_dir = '/Users/youhei/Desktop/'
main()
function main() {
var tmp = document.location.href.split('/').slice(-3)
var n = [tmp[0], tmp[1]].join('_')
var dir = base_dir + n
var list = Array.filter(document.images, function(i) {
return (i.width == 100 || i.height == 100)
})
// list = [list[0], list[1]]
if (list.length > 1) {
createDir(dir)
list.forEach(function(i) {
var s = get(i.parentNode.href)
var m = s.match(/url=(.+?)"/)
var u = m[1]
var l = dir + '/' + u.split('/').slice(-1)[0]
download(u, l)
})
}
else {
log('no item.')
}
}
function log(arg) {
Firebug.Console.log(arg)
}
function get(url) {
var req = new XMLHttpRequest();
req.open('GET', url, false);
req.send(null);
if (req.status == 200) {
return req.responseText
}
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment