$(document).ready(function(){chrome.storage.local.get(["websiteTimes"],function(d){d=d.websiteTimes||[];d=calculateAverageAndKeepTime(d);rendertable(d)})});
function rendertable(d){d.sort((a,b)=>b.timestamp-a.timestamp);d=d.map(a=>{const b=formatTimestamp(a.timestamp);return{...a,formattedTimestamp:b}});var c=$("#example").DataTable();c.destroy();c=$("#example").DataTable({pageLength:25,data:d.map(a=>[a.website,a.time,a.averageTime,a.formattedTimestamp]),dom:"Bfrtip",buttons:[{extend:"copy",exportOptions:{columns:[1,2,3,0],format:{body:function(a,b,e,f){return 0===e?$(a).data("href")||a:a}}}},{extend:"excel",exportOptions:{columns:[1,2,3,0],format:{body:function(a,
b,e,f){return 0===e?$(a).data("href")||a:a}}}},{extend:"csv",exportOptions:{columns:[1,2,3,0],format:{body:function(a,b,e,f){return 0===e?$(a).data("href")||a:a}}}},"print"],createdRow:function(a,b,e){$(a).addClass("table-items")},columns:[{title:"URL",render:function(a,b,e,f){b=a;50<a.length&&(b=a.substring(0,50)+"...");return'<span title="Click to copy" class="url-text" data-href="'+a+'">'+b+"</span>"}},{title:"Load Time(s)"},{title:"Avg Load Time(s)"},{title:"Timestamp"},{title:"Action",render:function(a,
b,e,f){return""!=e[1]?'<button class="btn btn-outline-info click-to-copy action-css mr-2">Copy Url</button><a target="_blank" href="'+e[1]+'"><a href="'+(e[0]+'" target="_blank"><button class="btn btn-outline-success action-css">View</button><a><button class="btn btn-outline-danger click-to-del action-css ml-2">Delete</button>'):""}}],autoWidth:!1,columnDefs:[{width:"40%",targets:0},{width:"10%",targets:1,className:"dt-left"},{width:"10%",targets:2,className:"dt-left"},{width:"20%",targets:3},{width:"20%",
targets:4}],order:[[3,"desc"]]});$(document).on("click",".click-to-copy",function(){const a=$(this).closest("tr").find(".url-text").data("href"),b=$("<textarea>");$("body").append(b);b.val(a).select();document.execCommand("copy");b.remove();$(this).text("Copied");setTimeout(()=>{$(this).text("Copy Url")},2E3)});$("#example tbody").on("click",".click-to-del",function(){c.row($(this).closest("tr")).remove().draw()})}
function formatTimestamp(d){var c=new Date(1E3*d);d=String(c.getDate()).padStart(2,"0");const a=String(c.getMonth()+1).padStart(2,"0"),b=c.getFullYear(),e=String(c.getHours()).padStart(2,"0"),f=String(c.getMinutes()).padStart(2,"0");c=String(c.getSeconds()).padStart(2,"0");return`${d}-${a}-${b} ${e}:${f}:${c}`}
function calculateAverageAndKeepTime(d){var c={};$.each(d,function(a,b){a=b.website;b=parseFloat(b.time);c[a]?(c[a].totalTime+=b,c[a].count+=1):c[a]={totalTime:b,count:1}});$.each(d,function(a,b){a=b.website;a=(c[a].totalTime/c[a].count).toFixed(2);b.averageTime=a});return d};
