|
@@ -6,14 +6,58 @@
|
|
|
//Get the target webpage body
|
|
|
requirelib("http");
|
|
|
var websiteContent = http.get(url);
|
|
|
-var rootURL = url.split("/");
|
|
|
-rootURL.pop();
|
|
|
-rootURL = rootURL.join("/");
|
|
|
+var protocol = url.substring(0, url.indexOf("/") + 2);
|
|
|
+var domain = url.substring(url.indexOf("/") + 2).split("/").shift();
|
|
|
+rootURL = protocol + domain;
|
|
|
|
|
|
|
|
|
-//replace the relative path files with absolutes
|
|
|
-websiteContent = websiteContent.split('src="/').join('src="' + rootURL + '/');
|
|
|
-websiteContent = websiteContent.split('href="/').join('href="' + rootURL + '/');
|
|
|
+var currentDirUrl = url;
|
|
|
+currentDirUrl = currentDirUrl.split("?")[0];
|
|
|
+var lastSegment = currentDirUrl.split("/").pop();
|
|
|
+if (lastSegment.indexOf(".") >= 0){
|
|
|
+ //Contain filename. Pop the filename as well
|
|
|
+ currentDirUrl = currentDirUrl.split("/");
|
|
|
+ currentDirUrl.pop();
|
|
|
+ currentDirUrl = currentDirUrl.join("/");
|
|
|
+}
|
|
|
+
|
|
|
+//Replace the src path with remote domain path
|
|
|
+var srcChunks = websiteContent.split('src="');
|
|
|
+var patchedSrcChunks = [srcChunks[0]];
|
|
|
+for (var i = 1; i < srcChunks.length; i++){
|
|
|
+ var thisChunk = srcChunks[i];
|
|
|
+ if (thisChunk.substr(0,1) == "/"){
|
|
|
+ //Inject the root URL in front
|
|
|
+ patchedSrcChunks.push(rootURL + "/" + thisChunk);
|
|
|
+ }else if (thisChunk.trim().substr(0,4) != "http" && thisChunk.trim().substr(0,5) != "data:"){
|
|
|
+ //Inject the current dir into it
|
|
|
+ patchedSrcChunks.push(currentDirUrl + "/" + thisChunk);
|
|
|
+ }else{
|
|
|
+ patchedSrcChunks.push(thisChunk);
|
|
|
+ }
|
|
|
+}
|
|
|
+websiteContent = patchedSrcChunks.join('src="');
|
|
|
+
|
|
|
+//Replace css url("xxx") if exists
|
|
|
+websiteContent = websiteContent.split('url("/').join("{url_root_dummy}");
|
|
|
+websiteContent = websiteContent.split('url("').join('url("' + currentDirUrl + "/");
|
|
|
+websiteContent = websiteContent.split('{url_root_dummy}').join('url("/' + rootURL + "/");
|
|
|
+
|
|
|
+var hrefChunks = websiteContent.split('href="');
|
|
|
+var patchedHrefChunks = [hrefChunks[0]];
|
|
|
+for (var j = 1; j < hrefChunks.length; j++){
|
|
|
+ var thisChunk = hrefChunks[j];
|
|
|
+ if (thisChunk.substr(0,1) == "/"){
|
|
|
+ //Inject the root URL in front
|
|
|
+ patchedHrefChunks.push(rootURL + "/" + thisChunk);
|
|
|
+ }else if (thisChunk.trim().substr(0,4) != "http"){
|
|
|
+ //Inject the current dir into it
|
|
|
+ patchedHrefChunks.push(currentDirUrl + "/" + thisChunk);
|
|
|
+ }else{
|
|
|
+ patchedHrefChunks.push(thisChunk);
|
|
|
+ }
|
|
|
+}
|
|
|
+websiteContent = patchedHrefChunks.join('href="');
|
|
|
|
|
|
//Replace href with redirection code
|
|
|
var htmlSegmentChunks = websiteContent.split(" ");
|