123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /*
- proxy.js
- This script help proxy text content of the requested page and render them locally
- */
- //Get the target webpage body
- requirelib("http");
- var websiteContent = http.get(url);
- var rootURL = url.split("/");
- rootURL.pop();
- rootURL = rootURL.join("/");
- //replace the relative path files with absolutes
- websiteContent = websiteContent.split('src="/').join('src="' + rootURL + '/');
- websiteContent = websiteContent.split('href="/').join('href="' + rootURL + '/');
- //Replace href with redirection code
- var htmlSegmentChunks = websiteContent.split(" ");
- var chunksToBeReplaced = [];
- for (var i = 0; i < htmlSegmentChunks.length; i++){
- var thisSegment = htmlSegmentChunks[i].trim();
- if (thisSegment.substring(0, 5) == "href="){
- //Process the segment and trim out only the href="xxx" part
- var cutPosition = thisSegment.lastIndexOf('"');
- thisSegment = thisSegment.substring(0, cutPosition + 1)
- if (thisSegment.trim().length > 6){
- chunksToBeReplaced.push(thisSegment);
- //console.log("SEGMENT:", thisSegment, thisSegment.trim().length);
- }
- }
- }
- for (var k= 0; k < chunksToBeReplaced.length; k++){
- var thisSegment = chunksToBeReplaced[k];
- thisSegment = thisSegment.replace('href="', "parent.loadWebsite(\"")
- thisSegment = thisSegment + ");"
- thisSegment = thisSegment.split("\"").join("'");
- thisSegment = "onclick=\"" + thisSegment + "\"";
-
- //Check if this is css / style files. If yes, bypass it
- var expectedFileExtension = thisSegment.trim().substring(thisSegment.lastIndexOf("."), thisSegment.length -4);
- if (expectedFileExtension == ".css" || expectedFileExtension == ".js" || thisSegment.indexOf(".css") >= 0){
- continue;
- }
- //console.log("REPLACING", chunksToBeReplaced[k], thisSegment);
- websiteContent = websiteContent.replace(chunksToBeReplaced[k], thisSegment);
- }
- //console.log(websiteContent);
- sendResp(websiteContent);
|