Skip to content

Commit

Permalink
Inject viewsetup automagically with cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McNamara committed Feb 18, 2015
1 parent b2682fe commit fedb4e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
43 changes: 20 additions & 23 deletions lib/bundle_views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ var express = require('express');
var app = express();
var configHelper = require('../config');
var filteredConfig = configHelper.getFilteredConfig();
var path = require('path');
var cheerio = require('cheerio');
var fs = require('fs');
var path = require('path');
var log = require('../logger')('nodecg/lib/bundle_views');
var Bundles = require('../bundles');

Expand All @@ -16,27 +17,12 @@ log.trace('Adding Express routes');
app.set('views', path.resolve(__dirname, '../..'));

app.get('/viewsetup.js', function(req, res) {
// Do some entirely silly string operations to extract the bundle name
// from the referer and pass it to the API setup script
// TODO: Must be a better way, surely
var referer = req.headers.referer;
var bundleName = null;

if (!referer) {
// Zombie is the headless browser that NodeCG uses for testing
// For some reason, it does not send a referer header when requesting viewsetup.js
// This is a workaround for that
if (req.headers['user-agent'].indexOf('Zombie.js') >= 0) {
bundleName = 'test-bundle';
} else {
log.error('viewsetup.js requested with no referer by', req.connection.remoteAddress);
res.status(400).send('Bad Request, no referer');
return;
}
} else {
var start = referer.indexOf('/view/') + 6;
var end = referer.indexOf('/', start + 1);
bundleName = referer.substring(start, end);
var bundleName = req.query.bundle || '';

// Don't send the script if the query string is missing
if (bundleName === '') {
res.send(404);
return;
}

var bundle = Bundles.find(bundleName);
Expand Down Expand Up @@ -80,7 +66,18 @@ app.get('/view/:bundleName*', function(req, res, next) {
return;
}

res.sendFile(fileLocation);
// If it's a HTML file, inject the viewsetup script and serve that
// otherwise, send the file unmodified
if (resName.endsWith('.html')) {
var file = fs.readFileSync(fileLocation);
var $ = cheerio.load(file);
var scriptNode = '<script src="/viewsetup.js?bundle='+ bundle.name +'"></script>';
$('body').append(scriptNode);

res.send($.html());
} else {
res.sendFile(fileLocation);
}
});

module.exports = app;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"body-parser": "^1.2.2",
"bower": "^1.3.10",
"cheerio": "^0.18.0",
"chokidar": "^1.0.0-rc3",
"connect-nedb-session": "0.0.3",
"cookie-parser": "^1.1.0",
Expand Down
5 changes: 1 addition & 4 deletions test/setup/test-bundle/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
window.viewApi = nodecg;
})
</script>

<!-- NodeCG API, must be the last script included -->
<script src="/viewsetup.js"></script>
</body>
</html>
</html>

0 comments on commit fedb4e1

Please sign in to comment.