loadpage.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var fs = require('fs');
  2. var page = require('webpage').create();
  3. var initial_done = false;
  4. var initial_bytes = '';
  5. var upstream_data = {};
  6. var output = fs.open("OUS_out", {mode: 'wb'});
  7. page.captureContent = ['.*'];
  8. page.onResourceRequested = function(request, network) {
  9. console.log('Request ' + JSON.stringify(request, undefined, 4));
  10. /// if(!initial_done){
  11. /// network.setHeader('X-Slitheen', initial_bytes);
  12. /// upstream_data[request.id] = initial_bytes;
  13. /// initial_done = true;
  14. if(fs.isFile("OUS_in")){
  15. var bytes = fs.read("OUS_in");
  16. if(bytes != ''){
  17. fs.remove("OUS_in");
  18. bytes.replace(/\r?\n|\r/g, "");
  19. console.log('Read in '+bytes.length+ ' bytes:' + bytes);
  20. network.setHeader('X-Slitheen', bytes);
  21. upstream_data[request.id] = bytes;
  22. }
  23. }
  24. };
  25. //TODO: on partial resource data coming in
  26. page.onResourceReceived = function(response) {
  27. console.log('Receive ' + JSON.stringify(response, undefined, 4));
  28. var id = response.id;
  29. //check to see if request successfully carried data
  30. if(upstream_data.hasOwnProperty(id)){
  31. if(response.status != 0){
  32. console.log('Successfully transmitted data (id '+id);
  33. delete upstream_data[id];
  34. if(upstream_data.hasOwnProperty(id)){
  35. console.log('deletion failed');
  36. }
  37. } else {
  38. console.log("Couldn't transmit data: "+upstream_data[id]);
  39. }
  40. } else {
  41. console.log('dictionary does not have key ' + id);
  42. }
  43. if(response.contentType == "slitheen"){
  44. console.log("WOOOOOOO\n");
  45. fs.write("slitheen.out", response.body, 'a');
  46. fs.write("slitheen.out", '\n', 'a');
  47. output.write(response.body);
  48. output.flush();
  49. }
  50. };
  51. ///for(;;){
  52. /// if(fs.isFile("OUS_in")){
  53. //page_loaded = false;
  54. ///var initial_bytes = fs.read("OUS_in");
  55. ///if(initial_bytes != ''){
  56. /// fs.remove("OUS_in");
  57. /// initial_bytes.replace(/\r?\n|\r/g, "");
  58. /// console.log('Read in '+initial_bytes.length+ ' bytes:' + initial_bytes);
  59. page.open('https://cs.uwaterloo.ca', function(status) {
  60. console.log("Status: " + status);
  61. if(status === "success") {
  62. page.render('example.png');
  63. }
  64. for( var id in upstream_data){
  65. //write it back to OUS_in
  66. fs.write("OUS_in", upstream_data[id], 'a');
  67. console.log("key: "+id+" value: "+upstream_data[id]);
  68. }
  69. phantom.exit();
  70. });
  71. /// break;
  72. /// }
  73. /// }
  74. ///}