Skip to content

Instantly share code, notes, and snippets.

@blasten
Last active August 22, 2016 22:44
Show Gist options
  • Save blasten/e8cb42cd2f6cd98e013874e0e3b90cba to your computer and use it in GitHub Desktop.
Save blasten/e8cb42cd2f6cd98e013874e0e3b90cba to your computer and use it in GitHub Desktop.
Proxy
var express = require('express');
var request = require('request');
var dom5 = require('dom5');
var argv = require('minimist')(process.argv.slice(2));
var app = express();
const PORT = process.env.PORT || argv.p || argv.port;
function removeNode(node) {
if (node) {
dom5.remove(node);
}
}
app.use('/static', express.static('public'));
app.get('/', (req, res) => {
res.set({
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
});
request(req.query.url,
(error, response, body) => {
if (!error && response.statusCode == 200) {
var document = dom5.parse(body);
removeNode(dom5.query(document, dom5.predicates.hasAttrValue('class', 'navFrame')));
removeNode(dom5.query(document, dom5.predicates.hasAttrValue('class', 'masthead')));
removeNode(dom5.query(document, dom5.predicates.hasAttrValue('class', 'navButton')));
removeNode(dom5.query(document, dom5.predicates.hasAttrValue('class', 'navClose')));
removeNode(dom5.query(document, dom5.predicates.hasAttrValue('id', 'navscrim')));
removeNode(dom5.query(document,
dom5.predicates.AND(
dom5.predicates.hasTagName('script'),
dom5.predicates.hasAttrValue('custom-element', 'amp-install-serviceworker')
)
));
removeNode(dom5.query(document, dom5.predicates.hasTagName('amp-install-serviceworker')));
res.send(dom5.serialize(document));
} else {
res.send('error');
}
});
});
if (PORT) {
app.listen(PORT);
} else {
app.listen();
}
{
"name": "amp-proxy",
"description": "AMP proxy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"dom5": "^1.3.1",
"express": "^4.14.0",
"minimist": "^1.2.0",
"request": "^2.72.0"
},
"engines": {
"node" : "~5.6.0"
},
"devDependencies": {},
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "Google Inc."
}
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<script>
(function() {
var v = location.search.substr(1);
if (!(/^\d+(-canary)?$/.test(v))) return;
var u = 'https://3p.ampproject.net/'+encodeURIComponent(v)+'/f.js';
document.write('<script'+' src="'+encodeURI(u)+'"><'+'/script>');
})();
</script>
</head>
<body style="margin:0">
<div id="c" style="position:absolute;top:0;left:0;bottom:0;right:0;">
<script>
draw3p(undefined,
// List of expected amp-ad types.
['allowed-ad-type1', 'allowed-ad-type2'],
// List of hostnames that are allowed to embed this change.
// Please also use ALLOW-FROM X-Frame-Options to get security in
// browsers that do not support location.ancestorOrigins.
[
'localhost',
'polymer-pica.appspot.com',
'polymer-pica-amp.appspot.com',
'polymer-ampproxy.appspot.com',
'polymer-news.appspot.com',
'washingtonpost.com'
]);
</script>
</div>
<script>if (window.docEndCallback) window.docEndCallback()</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment