proxy.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 protocol = url.substring(0, url.indexOf("/") + 2);
  9. var domain = url.substring(url.indexOf("/") + 2).split("/").shift();
  10. rootURL = protocol + domain;
  11. var currentDirUrl = url;
  12. currentDirUrl = currentDirUrl.split("?")[0];
  13. var lastSegment = currentDirUrl.split("/").pop();
  14. if (lastSegment.indexOf(".") >= 0){
  15. //Contain filename. Pop the filename as well
  16. currentDirUrl = currentDirUrl.split("/");
  17. currentDirUrl.pop();
  18. currentDirUrl = currentDirUrl.join("/");
  19. }
  20. //Replace the src path with remote domain path
  21. var srcChunks = websiteContent.split('src="');
  22. var patchedSrcChunks = [srcChunks[0]];
  23. for (var i = 1; i < srcChunks.length; i++){
  24. var thisChunk = srcChunks[i];
  25. if (thisChunk.substr(0,1) == "/"){
  26. //Inject the root URL in front
  27. patchedSrcChunks.push(rootURL + thisChunk);
  28. }else if (thisChunk.trim().substr(0,4) != "http" && thisChunk.trim().substr(0,5) != "data:"){
  29. //Inject the current dir into it
  30. patchedSrcChunks.push(currentDirUrl + "/" + thisChunk);
  31. }else{
  32. patchedSrcChunks.push(thisChunk);
  33. }
  34. }
  35. websiteContent = patchedSrcChunks.join('src="');
  36. //Replace css url("xxx") if exists
  37. websiteContent = websiteContent.split('url("/').join("{url_root_dummy}");
  38. websiteContent = websiteContent.split('url("').join('url("' + currentDirUrl + "/");
  39. websiteContent = websiteContent.split('{url_root_dummy}').join('url("/' + rootURL + "/");
  40. var hrefChunks = websiteContent.split('href="');
  41. var patchedHrefChunks = [hrefChunks[0]];
  42. for (var j = 1; j < hrefChunks.length; j++){
  43. var thisChunk = hrefChunks[j];
  44. if (thisChunk.substr(0,1) == "/"){
  45. //Inject the root URL in front
  46. patchedHrefChunks.push(rootURL + thisChunk);
  47. }else if (thisChunk.trim().substr(0,4) != "http"){
  48. //Inject the current dir into it
  49. patchedHrefChunks.push(currentDirUrl + "/" + thisChunk);
  50. }else{
  51. patchedHrefChunks.push(thisChunk);
  52. }
  53. }
  54. websiteContent = patchedHrefChunks.join('href="');
  55. //Replace href with redirection code
  56. var htmlSegmentChunks = websiteContent.split(" ");
  57. var chunksToBeReplaced = [];
  58. for (var i = 0; i < htmlSegmentChunks.length; i++){
  59. var thisSegment = htmlSegmentChunks[i].trim();
  60. if (thisSegment.substring(0, 5) == "href="){
  61. //Process the segment and trim out only the href="xxx" part
  62. var cutPosition = thisSegment.lastIndexOf('"');
  63. thisSegment = thisSegment.substring(0, cutPosition + 1)
  64. if (thisSegment.trim().length > 6){
  65. chunksToBeReplaced.push(thisSegment);
  66. //console.log("SEGMENT:", thisSegment, thisSegment.trim().length);
  67. }
  68. }
  69. }
  70. for (var k= 0; k < chunksToBeReplaced.length; k++){
  71. var thisSegment = chunksToBeReplaced[k];
  72. thisSegment = thisSegment.replace('href="', "parent.loadWebsite(\"")
  73. thisSegment = thisSegment + ");"
  74. thisSegment = thisSegment.split("\"").join("'");
  75. thisSegment = "onclick=\"" + thisSegment + "\"";
  76. //Check if this is css / style files. If yes, bypass it
  77. var expectedFileExtension = thisSegment.trim().substring(thisSegment.lastIndexOf("."), thisSegment.length -4);
  78. if (expectedFileExtension == ".css" || expectedFileExtension == ".js" || thisSegment.indexOf(".css") >= 0){
  79. continue;
  80. }
  81. //console.log("REPLACING", chunksToBeReplaced[k], thisSegment);
  82. websiteContent = websiteContent.replace(chunksToBeReplaced[k], thisSegment);
  83. }
  84. //console.log(websiteContent);
  85. sendResp(websiteContent);