Created
August 9, 2010 04:46
-
-
Save johnboxall/514948 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
var http = require('http'); | |
var script = '<script type="text/javascript" src="http://m.com/static/tests/test-wired.js"></script>'; | |
var host = "www.wired.com"; | |
var headTagRe = /<head.*?>/i; | |
var htmlContentTypeRe = /text\/html/; | |
http.createServer(function (req, res) { | |
var client = http.createClient(80, host); | |
// TODO: copy req.headers and change host. | |
//var proxyReq = c.request(req.method, req.url, req.headers); | |
var proxyReq = client.request('GET', req.url, {'host': host}); | |
proxyReq.addListener('response', function (proxyRes) { | |
res.writeHead(proxyRes.statusCode, proxyRes.headers); | |
var needScript = htmlContentTypeRe.test(proxyRes.headers["content-type"]); | |
proxyRes.addListener('data', function (chunk) { | |
// chunk is a buffer. http://nodejs.org/api.html#buffers-2 | |
if (needScript) { | |
needScript = false; | |
var chunkStr = chunk.toString('utf8'); | |
//var index = chunkStr.indexOf('<head>'); | |
var match = headTagRe.exec(chunkStr); | |
if (match && match.length) { | |
var insertAt = match.index + match[0].length; | |
chunkStr = chunkStr.slice(0, insertAt) + script + chunkStr.slice(insertAt); | |
chunk = new Buffer(chunkStr.length); | |
chunk.write(chunkStr); | |
} | |
} | |
res.write(chunk, 'binary'); | |
}); | |
proxyRes.addListener('end', function () { | |
res.end(); | |
}); | |
}); | |
req.addListener('end', function () { | |
proxyReq.end() | |
}); | |
}).listen(8124, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:8124/'); |
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
document.addEventListener("DOMContentLoaded", function() { | |
document.body.addEventListener("click", function(e) { | |
if (e.target.nodeName.toLowerCase() == "a" && /^http:\/\/www\.wired\.com/.test(e.target.href)) { | |
e.preventDefault(); | |
var url = "/" + e.target.href.split("/").slice(3).join("/"); | |
window.location = url; | |
} | |
}, false); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment