Created
August 1, 2008 19:15
-
-
Save swdyh/3668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jsactions script. | |
// download http://www.humberthumbert.net/code/codelist.html | |
var base_dir = '/Users/youhei/Desktop/' | |
main() | |
function main() { | |
var dir = base_dir + 'hhcode/' | |
createDir(dir) | |
var code_dir = dir + 'code/' | |
createDir(code_dir) | |
var img_dir = dir + 'image/' | |
createDir(img_dir) | |
document.body.removeAttribute('onload') | |
var link = document.getElementsByTagName('link')[0] | |
var css = link.href.split('/').slice(-1)[0] | |
download(link.href, dir + css) | |
link.setAttribute('href', css) | |
var as = document.getElementsByTagName('a') | |
Array.forEach(as, function(i) { | |
var gif = i.href.replace('code', 'code/img').replace('html', 'gif') | |
var div = document.createElement('div') | |
div.setAttribute('style', 'margin:5em;') | |
var a = document.createElement('a') | |
var id = i.getAttribute('href').replace('.html', '') | |
a.setAttribute('id', id) | |
i.setAttribute('href', '#' + id) | |
i.removeAttribute('onclick') | |
i.removeAttribute('target') | |
var p = document.createElement('p') | |
var to = document.createElement('a') | |
to.setAttribute('href', 'javascript:scrollTo(0, 0)') | |
to.appendChild(document.createTextNode('to top')) | |
var img = document.createElement('img') | |
img.setAttribute('src', gif) | |
div.appendChild(a) | |
a.appendChild(img) | |
p.appendChild(to) | |
div.appendChild(p) | |
document.body.appendChild(div) | |
}) | |
Array.forEach(document.images, function(i) { | |
var n = i.src.split('/').slice(-1)[0] | |
download(i.src, img_dir + n) | |
i.setAttribute('src', 'image/' + n) | |
}) | |
var s = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' + | |
'<html>' + | |
document.body.parentNode.innerHTML + | |
'</html>' | |
write(dir + 'index.html', s, ({charset: document.characterSet})) | |
} | |
function write(path, data, opt) { | |
const LocalFile = Components.Constructor('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath'); | |
var writePath = new LocalFile(path); | |
var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream); | |
outStream.init(writePath, 0x04 | 0x08 | 0x20, 0664, 0); // write, create, truncate | |
// outStream.write(data, data.length); | |
// outStream.close | |
var opt = opt || {} | |
var charset = opt.charset || "UTF-8"; | |
var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"] | |
.createInstance(Components.interfaces.nsIConverterOutputStream); | |
os.init(outStream, charset, 0, 0x0000); | |
os.writeString(data) | |
os.close(); | |
} | |
function log(arg) { | |
Firebug.Console.log(arg) | |
} | |
function getElementsByXPath(xpath, node) { | |
var node = node || document | |
var doc = node.ownerDocument ? node.ownerDocument : node | |
var nodesSnapshot = doc.evaluate(xpath, node, null, | |
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) | |
var data = [] | |
for (var i = 0; i < nodesSnapshot.snapshotLength; i++) { | |
data.push(nodesSnapshot.snapshotItem(i)) | |
} | |
return data | |
} | |
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