Special thanks to master Michael Zoumperakis!!!
Doesn't work, let's do it again
- Open Terminal execute the commands
- cd /Users/ari/Tools/javascript/randomPage/160525
- nodemon index.js
- Open http://localhost:3612/ to take the list
- Save the content to the
list.json
file inside~/Sites/mousikobostani.github.io
M-x mousiko-bostani
HTML Code
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("asdgfasdg"); $.get("list.json", function(data, status){ alert("Data: " + data + "\nStatus: " + status); <!--window.open(data[Math.floor(Math.random() * data.length)]);--> },"json"); alert("TTg") }); }); </script> </head> <body> <button>Send an HTTP GET request to a </button> </body> </html>
Install Node.js and play with Javascript!
Terminal commands:
~/Tools/javascript/michael/
nodemon randomPageCollector.js
Here is the nodemon randomPageCollector.js
file
/* Create random page collector @ Berlin with Michael Zoumperakis Aris Bezas 150312 */ 'use strict'; var _ = require('lodash'); var express = require('express'); var fs = require('fs'); var http = require('http'); var app = express(); var path = require('path'); var open = require('open'); var pageCollector = (function(){ var dir2read = '/Users/ari/Sites/mousikobostani.github.io'; var files_ = []; function getFilesMB (dir, files_){ files_ = files_ || []; var files = fs.readdirSync(dir); for (var i in files){ var name = dir + '/' + files[i]; if (fs.statSync(name).isDirectory()){ if(!name.match(/\.git\//) & !name.match('tags') & !name.match('makam!')) { getFilesMB(name, files_); } } else { if(!name.match('index.html') & !name.match('index.xml') & !name.match('.xml') & !name.match('.js')) { files_.push(name); } } } //return files_; return files_[Math.floor(Math.random() * files_.length)]; } return { getFiles: function(res){ return getFilesMB(dir2read); } }; })(); http.createServer(app).listen(process.env.PORT || 3612); app.use(express.bodyParser()); app.get('/', function(req, res){ //res.json(pageCollector.getFiles()); open(pageCollector.getFiles(), function (err) { console.log('The user closed the browser'); }); res.json(console.log('The user')); });
Links
- https://nodejs.org/api/fs.html
- node fs how to get complet directory struction
- http://howtonode.org/do-it-fast
- Export directly to
json
file