0x0049

html results

May 12th, 2015
320
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // generates an temporary html page and opens it for viewing
  2. // similar to how BOSS does its reports
  3. spacing();
  4. divider();
  5. console.log("This program and the file must be in the same directory.");
  6.  
  7. ask("Enter the filename (type quit to quit)", /.+/, function(filename) {
  8.   // quit if they typed quit
  9.   if (filename === "quit") {
  10.     console.log("Goodbye.");
  11.     quit();
  12.   }
  13.  
  14.   // otherwise try opening the file
  15.   else {
  16.         fs = require("fs");
  17.  
  18.         // check if the  file exists
  19.         fs.exists(filename, function(exists) {
  20.  
  21.             // if the file exists
  22.           if (exists) {
  23.             readFile(filename);
  24.           }
  25.  
  26.             // if the file does not exist
  27.           else {
  28.             console.log("That file does not exist.");
  29.             quit();
  30.           }
  31.  
  32.         });
  33.   }
  34.  
  35. }); // end ask
  36.  
  37.  
  38. // reads the file
  39. function readFile(filename) {
  40.     fs.stat(filename, function(error, stats) {
  41.         fs.open(filename, "r", function(error, fd) {
  42.             var buffer = new Buffer(stats.size);
  43.  
  44.             fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
  45.                 // grab the data
  46.                 var data = buffer.toString("utf8", 0, buffer.length);
  47.  
  48.                 // close fs
  49.                 fs.close(fd);
  50.  
  51.                 // check if our appdata folder exists
  52.                 // todo: don't hardcode the appdata folder
  53.                 var appdataFolder = "C:\\Users\\Lord Kai\\AppData\\Roaming\\.ourappdatafolder\\";
  54.                 fs.stat(appdataFolder, function(error, stats) {
  55.  
  56.                 // create the folder then move on
  57.                     if(error) {
  58.                         fs.mkdir(appdataFolder,function(error) {
  59.                             if(!error || (error && error.code === 'EEXIST')){
  60.                                 createHTML(data, appdataFolder);
  61.                             }
  62.  
  63.                             else {
  64.                                 console.log(error);
  65.                                 quit();
  66.                             }
  67.                         });
  68.                     }
  69.  
  70.                     // don't do anything, just move on
  71.                     else {
  72.                         createHTML(data, appdataFolder);
  73.                     }
  74.                 }); // end appdata folder check
  75.             }); // end read
  76.         }); // end open
  77.     }); // end stat
  78. }
  79.  
  80.  
  81. // creates HTML from our file and outputs it into a temporary HTML file
  82. function createHTML(data, appdataFolder) {
  83.     // find out what we should do with something (just an example)
  84.     ask("Enter something we can use", /.+/, function(something) {
  85.         var html = "<html><head><title>Title</title></head><body><h1>Click the link!</h1>";
  86.  
  87.         // more html goes here
  88.         // parse the data
  89.  
  90.         html += "</body></html>";
  91.  
  92.         // write the file
  93.       var tempFile = "temp.html";
  94.         fs.writeFile(appdataFolder + tempFile, html, function(error) {
  95.         if(error) {
  96.             console.log(error);
  97.             quit();
  98.         }
  99.  
  100.         // wrote successfully
  101.         else {
  102.           launchInChrome(appdataFolder + tempFile);
  103.             }
  104.         });
  105.     });
  106. }
  107.  
  108.  
  109. // launch the HTML file we created in Chrome
  110. function launchInChrome(tempLocation) {
  111.     var chromeLocation = "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"";
  112.  
  113.     var exec = require('child_process').exec;
  114.     var child = exec(chromeLocation + " \"" + tempLocation + "\"", function(error, stdout, stderr) {
  115.         if (error != null) {
  116.             console.log(stderr);
  117.             quit();
  118.         }
  119.  
  120.         // at this point we're done
  121.         quit();
  122.  
  123.     }); // end child
  124. }
  125.  
  126. // outputs some space and exits
  127. function quit() {
  128.       divider();
  129.       spacing();
  130.       process.exit();
  131. }
  132.  
  133. function divider() {
  134.     console.log("------------------------------------");
  135. }
  136.  
  137. function spacing() {
  138.     console.log("\n\n");
  139. }
  140.  
  141. // gets input from a user
  142. function ask(question, format, callback) {
  143.  var stdin = process.stdin, stdout = process.stdout;
  144.  
  145.  stdin.resume();
  146.  stdout.write(question + ": ");
  147.  
  148.  stdin.once('data', function(data) {
  149.    data = data.toString().trim();
  150.  
  151.    if (format.test(data)) {
  152.      callback(data);
  153.    } else {
  154.      stdout.write("It should match: "+ format +"\n");
  155.      ask(question, format, callback);
  156.    }
  157.  });
  158. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment