|
| 1 | +var id = uniqueId(); |
| 2 | + |
| 3 | +var title = document.getElementsByClassName("watch-title")[0].getAttribute("title"); |
| 4 | + |
| 5 | +var uploader = document.getElementsByClassName("yt-user-info")[0].innerText; |
| 6 | + |
| 7 | +var video = document.getElementsByTagName("video")[0]; |
| 8 | + |
| 9 | +var timeout = 0; |
| 10 | + |
| 11 | +var repeat = setInterval(function() { |
| 12 | + sendData(title, uploader, video.currentTime, video.duration, video.paused, false); |
| 13 | +}, 500); |
| 14 | + |
| 15 | +window.onbeforeunload = function () { |
| 16 | + clearInterval(repeat); |
| 17 | + // send termination flag when tab or browser is closing. |
| 18 | + sendData(title, uploader, video.currentTime, video.duration, video.paused, true); |
| 19 | +}; |
| 20 | + |
| 21 | +function sendData(title, uploader, currentTime, duration, paused, terminate){ |
| 22 | + var xhttp = new XMLHttpRequest(); |
| 23 | + xhttp.open("POST", "http://127.0.0.1:60024/", true); |
| 24 | + xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| 25 | + var data = { |
| 26 | + "id" : id, |
| 27 | + "title" : title, |
| 28 | + "uploader": uploader, |
| 29 | + "currentTime": currentTime, |
| 30 | + "duration": duration, |
| 31 | + "paused" : paused, |
| 32 | + "terminate": terminate |
| 33 | + }; |
| 34 | + xhttp.send(JSON.stringify(data)); |
| 35 | + //return xhttp.status; |
| 36 | +} |
| 37 | + |
| 38 | +function uniqueId () { |
| 39 | + // desired length of Id |
| 40 | + var idStrLen = 32; |
| 41 | + // always start with a letter -- base 36 makes for a nice shortcut |
| 42 | + var idStr = (Math.floor((Math.random() * 25)) + 10).toString(36) + ""; |
| 43 | + // add a timestamp in milliseconds (base 36 again) as the base |
| 44 | + idStr += (new Date()).getTime().toString(36) + ""; |
| 45 | + // similar to above, complete the Id using random, alphanumeric characters |
| 46 | + do { |
| 47 | + idStr += (Math.floor((Math.random() * 35))).toString(36); |
| 48 | + } while (idStr.length < idStrLen); |
| 49 | + |
| 50 | + return (idStr); |
| 51 | +} |
0 commit comments