diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-01-05 23:43:39 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-05 23:43:39 +0100 |
commit | c57e57f5ba31db9b8d57f58ce98aab6eac9e48d7 (patch) | |
tree | d1d33a051d063d61b071861d6075237d779c4762 /modules | |
parent | 468e0f73f38ae64f912de3b9ac249b3b1198e76a (diff) |
changes: Diffing!
Diffstat (limited to 'modules')
-rw-r--r-- | modules/changes/changes.html | 59 | ||||
-rw-r--r-- | modules/changes/changes.js | 72 |
2 files changed, 128 insertions, 3 deletions
diff --git a/modules/changes/changes.html b/modules/changes/changes.html index 739cb28..a7cd439 100644 --- a/modules/changes/changes.html +++ b/modules/changes/changes.html @@ -44,6 +44,7 @@ function focusData(data){ if(!header.firstChild)header.appendChild(document.createTextNode(data.url)); else header.firstChild.nodeValue=data.url; header.setAttribute("href",data.url); + document.getElementById("diffbox").innerHTML=""; var l=tbody.children,i; for(i=l.length-1;i>=0;i--)tbody.removeChild(l[i]); @@ -67,6 +68,22 @@ function focusData(data){ tr.appendChild(td); td=document.createElement("td"); + e=document.createElement("input"); + e.setAttribute("type","radio"); + e.setAttribute("name","diffold"); + e.setAttribute("value",data.timeline[i][0]); + td.appendChild(e); + tr.appendChild(td); + + td=document.createElement("td"); + e=document.createElement("input"); + e.setAttribute("type","radio"); + e.setAttribute("name","diffnew"); + e.setAttribute("value",data.timeline[i][0]); + td.appendChild(e); + tr.appendChild(td); + + td=document.createElement("td"); e=document.createElement("a"); e.href="javascript:void(0)"; e.appendChild(document.createTextNode("Delete till here")); @@ -182,6 +199,30 @@ function addURLbutton(){ }); } +function doShowDiff(){ + var oldradio=document.querySelector("input[name=diffold]:checked"); + var newradio=document.querySelector("input[name=diffnew]:checked"); + if(!oldradio||!newradio){ + alert("Select two different records to perform a diff"); + return; + } + var olddate=oldradio.value; + var newdate=newradio.value; + var url=document.getElementById("timelineheader").firstChild.nodeValue; + fetch("POST","/changes/diff", + JSON.stringify({url:url,olddate:olddate,newdate:newdate}), + function(status,body){ + var diffbox; + if(status==200){ + diffbox=document.getElementById("diffbox"); + if(diffbox.firstChild)diffbox.removeChild(diffbox.firstChild); + diffbox.appendChild(document.createTextNode(body)); + } else { + alert("Error: ("+status+") "+body); + } + }); +} + function addURLkeypress(ev){ if(ev.keyCode==10||ev.keyCode==13)addURLbutton(); } @@ -251,8 +292,13 @@ h1{ #timelinetbl td, #timelinetbl th{ border:1px #888 solid; } -#timelinetbl td:nth-child(3){ +#timelinetbl td:nth-child(3), #timelinetbl td:nth-child(4){ + text-align:center; +} +#timelinetbl td:nth-child(5){ font-size:10px; + padding-left:40px; + padding-right:10px; } tr.repeated{ @@ -285,9 +331,16 @@ tr.repeated{ <div id="timelinecontainer"> <h2><a id="timelineheader"></a></h2> <table id="timelinetbl"> - <thead><tr><th>Date</th><th>Hash</th></tr></thead> + <thead><tr> + <th>Date</th> + <th>Hash</th> + <th>Old</th> + <th>New</th> + </tr></thead> <tbody id="timelinetbody"></tbody> - </table> + </table> <br> + <input type="button" value="Show diff" onclick="doShowDiff();"> <br> + <pre id="diffbox"></pre> </div> </body> </html> diff --git a/modules/changes/changes.js b/modules/changes/changes.js index 7cbe7ee..8a0b3d9 100644 --- a/modules/changes/changes.js +++ b/modules/changes/changes.js @@ -5,6 +5,7 @@ var cmn=require("../$common.js"), crypto=require("crypto"), http=require("http"), https=require("https"), + fs=require("fs"), URL=require("url"), child_process=require("child_process"); @@ -376,4 +377,75 @@ module.exports=function(app,io,_moddir){ refreshURLs(); res.send(); }); + app.post("/changes/diff",function(req,res){ + var param; + try {param=JSON.parse(req.body);} + catch(e){ + res.status(400); + res.send("Invalid JSON sent"); + return; + } + var url=param.url; + var newdate=new Date(param.newdate); + var olddate=new Date(param.olddate); + if(!urls[url]){ + res.status(404); + res.send("URL not found in watch list"); + return; + } + if(newdate.getTime()==NaN||olddate.getTime()==NaN){ + res.status(400); + res.send("Invalid time(s) sent"); + return; + } + if(newdate.getTime()==olddate.getTime()){ + res.status(400); + res.send("Please select different records to diff"); + return; + } + var tl=urls[url].timeline; + var newi=-1,oldi=-1,i; + for(i=0;i<tl.length;i++){ + if(tl[i][0].getTime()==newdate.getTime()){ + newi=i; + } + if(tl[i][0].getTime()==olddate.getTime()){ + oldi=i; + } + } + if(newi==-1||oldi==-1){ + res.status(404); + res.send("Times not found in timeline for URL"); + return; + } + //res.send(tl[newi][1]+"\n"+tl[oldi][1]+"\n"); + fs.mkdtemp("/tmp/changes-diff-tmp-",function(err,folder){ + if(err){ + console.log(err); + res.status(500); + res.send(); + return; + } + var counter=0; + var fscb=function(err){ + if(err){ + console.log(err); + res.status(500); + res.send(); + return; + } + counter++; + if(counter<2)return; + var proc=child_process.spawn( + "diff", + [folder+"/old.html",folder+"/new.html"], + {stdio:[process.stdin,fs.openSync(folder+"/diff.txt","w"),process.stderr]}); + proc.on("exit",function(code,signal){ + fs.createReadStream(folder+"/diff.txt").pipe(res); + }); + }; + fs.writeFile(folder+"/old.html",tl[oldi][2],fscb); + fs.writeFile(folder+"/new.html",tl[newi][2],fscb); + }); + }); }; |