proxy.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. proxy.js
  3. This script help proxy text content of the requested page and render them locally
  4. */
  5. //Get the target webpage body
  6. requirelib("http");
  7. var websiteContent = http.get(url);
  8. var rootURL = url.split("/");
  9. rootURL.pop();
  10. rootURL = rootURL.join("/");
  11. //replace the relative path files with absolutes
  12. websiteContent = websiteContent.split('src="/').join('src="' + rootURL + '/');
  13. websiteContent = websiteContent.split('href="/').join('href="' + rootURL + '/');
  14. //Replace href with redirection code
  15. var htmlSegmentChunks = websiteContent.split(" ");
  16. var chunksToBeReplaced = [];
  17. for (var i = 0; i < htmlSegmentChunks.length; i++){
  18. var thisSegment = htmlSegmentChunks[i].trim();
  19. if (thisSegment.substring(0, 5) == "href="){
  20. //Process the segment and trim out only the href="xxx" part
  21. var cutPosition = thisSegment.lastIndexOf('"');
  22. thisSegment = thisSegment.substring(0, cutPosition + 1)
  23. if (thisSegment.trim().length > 6){
  24. chunksToBeReplaced.push(thisSegment);
  25. //console.log("SEGMENT:", thisSegment, thisSegment.trim().length);
  26. }
  27. }
  28. }
  29. for (var k= 0; k < chunksToBeReplaced.length; k++){
  30. var thisSegment = chunksToBeReplaced[k];
  31. thisSegment = thisSegment.replace('href="', "parent.loadWebsite(\"")
  32. thisSegment = thisSegment + ");"
  33. thisSegment = thisSegment.split("\"").join("'");
  34. thisSegment = "onclick=\"" + thisSegment + "\"";
  35. //Check if this is css / style files. If yes, bypass it
  36. var expectedFileExtension = thisSegment.trim().substring(thisSegment.lastIndexOf("."), thisSegment.length -4);
  37. if (expectedFileExtension == ".css" || expectedFileExtension == ".js" || thisSegment.indexOf(".css") >= 0){
  38. continue;
  39. }
  40. //console.log("REPLACING", chunksToBeReplaced[k], thisSegment);
  41. websiteContent = websiteContent.replace(chunksToBeReplaced[k], thisSegment);
  42. }
  43. //console.log(websiteContent);
  44. sendResp(websiteContent);