Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created April 11, 2009 10:39
Show Gist options
  • Select an option

  • Save swdyh/93524 to your computer and use it in GitHub Desktop.

Select an option

Save swdyh/93524 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment