pub / yt2html

Fetches Youtube content via RSS and provides a chronological timeline
git clone https://src.jayvii.de/pub/yt2html.git
Home | Log | Files | Exports | Refs | README | RSS

yt.js (5979B)


      1 function yt2html_toggle_player(video) {
      2 
      3   console.log("video: " + video);
      4 
      5 	// get video section
      6 	var section = document.getElementById("video_" + video);
      7 
      8 	// Preview image
      9 	var preview = section.querySelector("#preview_" + video);
     10 
     11 	// if player-iframe exists, remove it and unhide preview image
     12 	var player = section.querySelectorAll("#player_" + video);
     13 	
     14 	if (player.length > 0) {
     15 
     16 		// unhide preview image
     17 		preview.classList.remove("hidden");
     18 
     19 		// remove player
     20 		player.forEach(function(x) { x.remove(); });
     21 
     22 		// Hide player container
     23 		section.querySelector(".player_container").classList.add("hidden");
     24 
     25 	} else {
     26 
     27 		// hide preview image
     28 		preview.classList.add("hidden");
     29 
     30 		// unhide player container
     31 		section.querySelector(".player_container").classList.remove("hidden");
     32 
     33 		// Construct player iframe
     34 	  var player = document.createElement("iframe");
     35   	player.id = "player_" + video;
     36   	player.classList.add("player");
     37 	  player.allowFullscreen = "true";
     38     player.sandbox = "allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox";
     39 	  player.width = "100%";
     40 	  player.height = "100%";
     41 	  player.frameborder = "0";
     42 	  player.scrolling = "no";
     43 	  player.src = "https://www.youtube-nocookie.com/embed/" + video +
     44 	    "?autoplay=1" + // no auto play
     45 	    "&cc_load_policy=0" + // no subtitles
     46 	    "&color=white" // white status bar
     47 
     48 		// inject player into video container
     49 		section.querySelector(".player_container").prepend(player);
     50 
     51 		// Focus player
     52 		document.getElementById("player_" + video).focus();
     53 
     54 		// Scroll to player
     55 		document.location = "#video_" + video;
     56 
     57 	}
     58 
     59 }
     60 
     61 function yt2html_update_channels_list() {
     62 
     63 	// Initilise channels array
     64 	var channels = [];
     65 
     66 	// Fetch channel input fields
     67 	var channels_obj = document.querySelectorAll(".channels_input");
     68 	channels_obj.forEach(function(x) {
     69 		if (x.value != "") {
     70 			channels.push(x.value);
     71 		}
     72 	});
     73 
     74 	// If last input field has a value, add a new (empty) one to the list, so the
     75 	// user can continue with input
     76 	if (channels_obj[channels_obj.length - 1].value != "") {
     77 		yt2html_add_channels_input(channels_obj.length);
     78 	}
     79 
     80 	// join channels array
     81 	channels = channels.join(",");
     82 
     83 	// insert array into hidden "channels" field
     84 	document.querySelector("input[name=channels]").value = channels;
     85 }
     86 
     87 function yt2html_add_channels_input(index) {
     88 
     89 	// Create label object
     90 	var label = document.createElement("label");
     91 	label.for = "channel_" + index;
     92 	label.innerHTML = "<mark>Add a new channel here</mark>";
     93 
     94 	// Create input object
     95 	var input = document.createElement("input");
     96 	input.name = "channel_" + index;
     97 	input.classList.add("channels_input");
     98 	input.type = "text";
     99 	input.addEventListener("input", yt2html_update_channels_list);
    100 
    101 	// Append label and input objects
    102 	document.getElementById("channels_list").append(label);
    103 	document.getElementById("channels_list").append(input);
    104 }
    105 
    106 function create_video_box(data) {
    107 
    108 	// Create section object from invisible template
    109 	var vid = document.querySelector("#video_template").cloneNode(true);
    110 	vid.id = "video_" + data["vid"];
    111 	vid.setAttribute("data", data["time"]);
    112 
    113 	// Update Headline Text and Link
    114 	vid.querySelector(".title").innerText = data["title"];
    115 	vid.querySelector(".title").href = "https://www.youtube.com/watch?v=" +
    116 		data["vid"];
    117 
    118 	// Update Channel Information
    119 	vid.querySelector(".channel").innerText = data["author"];
    120 	vid.querySelector(".channel").href = "/?channels=" + data["aid"];
    121 
    122 	// Update Timestamp Info
    123 	var ts = new Date(data["time"] * 1000);
    124 	var yyyy = ts.getFullYear();
    125 	var mm = ("0" + (ts.getMonth() + 1)).slice(-2);
    126 	var dd = ("0" + ts.getDate()).slice(-2);
    127 	var hh = ("0" + ts.getHours()).slice(-2);
    128 	var m = ("0" + ts.getMinutes()).slice(-2);
    129 	var ft = yyyy + "-" + mm + "-" + dd + " " + hh + ":" + m;
    130 	vid.querySelector(".timestamp").innerText = ft;
    131 
    132 	// Update Views Count Info
    133 	vid.querySelector(".views").innerText = "(" + data["views"] + " Views)";
    134 
    135 	// Update Arguments in button toggle
    136 	vid.querySelector(".toggle_video").setAttribute(
    137 		"onclick",
    138 		"yt2html_toggle_player('" + data["vid"] + "');"
    139 	);
    140 	vid.querySelector(".play_button").setAttribute(
    141 		"onclick",
    142 		"yt2html_toggle_player('" + data["vid"] + "');"
    143 	);
    144 
    145 	// Update Preview Image
    146 	vid.querySelector(".preview").id = "preview_" + data["vid"];
    147 	vid.querySelector(".preview_image").src = "https://i4.ytimg.com/vi/" +
    148 		data["vid"] + "/hqdefault.jpg";
    149 
    150 	// Update Description Box
    151 	vid.querySelector(".description").innerText = data["desc"];
    152 
    153 	// Return object
    154 	return vid;
    155 
    156 }
    157 
    158 async function fetch_and_insert_yt(channel) {
    159 	// gather feed information from API endpoint
    160 	const videos_raw = await fetch('/lib/fetch_feed.php?channel=' + channel);
    161 
    162 	// parse gathered information
    163 	var videos = await videos_raw.json();
    164 
    165 	// Update Channel List Name
    166 	if (videos.length > 0) {
    167 		document.querySelector("#channellist_" + channel).innerText = videos[0]["author"];
    168 	} else {
    169 		document.querySelector("#channellist_" + channel).innerHTML = "<mark>ERROR</mark>";
    170 		document.querySelector("#channellist_" + channel).classList.add("error")
    171 	}
    172 
    173 	// create and insert each video into the feed
    174 	for (let i=0; i < videos.length; i++) {
    175 
    176 		// Create video object
    177 		var vid = create_video_box(videos[i]);
    178 
    179 		// Check which videos are already present
    180 		var vids = document.querySelectorAll(".video");
    181 
    182 		// Run through all present videos from the top to bottom
    183 		for (let j = 0; j < vids.length; j++) {
    184 
    185 			// If a video has a smaller (earlier) timestamp, put the video BEFORE it
    186 			if (vids[j].getAttribute("data") < vid.getAttribute("data")) {
    187 				vids[j].before(vid);
    188 				break;
    189 			}
    190 		}
    191 	}
    192 
    193 	// Update loaded channels global
    194 	loaded_channels.push(channel);
    195 	loading_indicator();
    196 }
    197 
    198 function loading_indicator() {
    199 		var await_channels = document.querySelector("#loading").getAttribute("data").split(",");
    200 		if (await_channels.length == loaded_channels.length) {
    201 			document.querySelector("#loading").classList.add("finished")
    202 		}
    203 }