loadpage.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var fs = require('fs');
  2. var page = require('webpage').create();
  3. var initial_done = false;
  4. var initial_bytes = '';
  5. var output = fs.open("OUS_out", {mode: 'wb'});
  6. page.onResourceRequested = function(request, network) {
  7. console.log('Request ' + JSON.stringify(request, undefined, 4));
  8. if(!initial_done){
  9. network.setHeader('X-Slitheen', initial_bytes);
  10. initial_done = true;
  11. } else if(fs.isFile("OUS_in")){
  12. var bytes = fs.read("OUS_in");
  13. if(bytes != ''){
  14. fs.remove("OUS_in");
  15. bytes.replace(/\r?\n|\r/g, "");
  16. console.log('Read in '+bytes.length+ ' bytes:' + bytes);
  17. network.setHeader('X-Slitheen', bytes);
  18. }
  19. }
  20. };
  21. //TODO: on partial resource data coming in
  22. page.onResourceReceived = function(response) {
  23. console.log('Receive ' + JSON.stringify(response, undefined, 4));
  24. if(response.contentType == "slitheen"){
  25. console.log("WOOOOOOO\n");
  26. fs.write("slitheen.out", response.body, 'a');
  27. }
  28. };
  29. for(;;){
  30. if(fs.isFile("OUS_in")){
  31. var initial_bytes = fs.read("OUS_in");
  32. if(initial_bytes != ''){
  33. fs.remove("OUS_in");
  34. initial_bytes.replace(/\r?\n|\r/g, "");
  35. console.log('Read in '+initial_bytes.length+ ' bytes:' + initial_bytes);
  36. page.open('https://cs.uwaterloo.ca', function(status) {
  37. console.log("Status: " + status);
  38. if(status === "success") {
  39. page.render('example.png');
  40. }
  41. phantom.exit();
  42. });
  43. break;
  44. }
  45. }
  46. }