<commit_before>// ==UserScript==
// @name        Youtube Video Resumer
// @namespace   totalamd
// @match       *://*/*
// @version     1
// @downloadURL 
// @updateURL   
// @grant       GM_listValues
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// ==/UserScript==

// TODO:
// [ ] make autostop only on main site OR autoplay = 1

"use strict";

(function(){
	let l = function(){}, i = function(){};
	l = console.log.bind(console), i = console.info.bind(console);

	const embed_url_pattern = /^https?:\/\/(?:www.)?youtube\.com\/embed\/([\w-]+)/;               // embed iframe
	const main_url_pattern = /^https?:\/\/(?:www.)?youtube\.com\/watch\?v=([\w-]+)/;              // main YT site
	const video_id_pattern = /^https?:\/\/(?:www.)?youtube\.com\/(?:watch\?v=|embed\/)([\w-]+)/;  // overall regexp
	// const autoplay_pattern = / /;
	let video_id;
	let timing;
	let video = document.querySelector('video.html5-main-video');
	// console.log(`new run\n`, location.href, `\n`, video);

	
	function saveTiming () {
		if (!video.ended) {
			GM_setValue(video_id, video.currentTime);
			// console.info(document.title, video.currentTime);
		} else {
			GM_deleteValue(video_id);
			// console.log(document.title, '<< timeupdate ended detected');
		}
		if (!video) {console.warn("save: no video!"); return;}
		if (video.paused) console.info(document.title, video_id, '<< saved', video.currentTime);
	}

	function loadTiming () {
		if (!video) {console.warn("load: no video!"); video = document.querySelector('video.html5-main-video');}
		timing = GM_getValue(video_id);
		if (timing !== undefined) {
			console.info(document.title, video_id, '>> loading:', timing);
			video.currentTime = timing;
		} else {console.info(document.title, '>> new video');}
	}
	
	// TODO
	/*
	// if embeded
	if (location.hostname === "") { 
		// if autoplay=1
		if ()
	} else if (location.href.match(main_url_pattern) && (window.self === window.top)) {
		// AND NOT (main AND top)
		
	} else {
		// console.log('Not yotube video, exiting...');
		return;
	}
	*/
	
	if (location.hostname === "www.youtube.com") {
		video_id = location.href.match(video_id_pattern)[1];
		if (!video) {video = document.querySelector('video.html5-main-video');}
		loadTiming();
	
	// get all GET params
	/*let paramsArray = {};
	location.search.substr(1).split('&').forEach(function(e){
		paramsArray[e.split('=')[0]] = e.split('=')[1];
	});*/

		video.addEventListener('timeupdate', function(){
			// console.info('timeupdate!');
			saveTiming();
		});
		video.addEventListener('ended', function(){
			saveTiming();
		})
	} else {
		console.info(location.hostname, 'not youtube');
		return;
	}
})()<commit_msg>console.log -> l(), console.info -> i()<commit_after>// ==UserScript==
// @name        Youtube Video Resumer
// @namespace   totalamd
// @match       *://*/*
// @version     1
// @downloadURL 
// @updateURL   
// @grant       GM_listValues
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// ==/UserScript==

// TODO:
// [ ] make autostop only on main site OR autoplay = 1

"use strict";
const l = function(){}, i = function(){};

(function(){
	const l = console.log.bind(console, `${GM_info.script.name} debug:`), i = console.info.bind(console, `${GM_info.script.name} debug:`);

	const embed_url_pattern = /^https?:\/\/(?:www.)?youtube\.com\/embed\/([\w-]+)/;               // embed iframe
	const main_url_pattern = /^https?:\/\/(?:www.)?youtube\.com\/watch\?v=([\w-]+)/;              // main YT site
	const video_id_pattern = /^https?:\/\/(?:www.)?youtube\.com\/(?:watch\?v=|embed\/)([\w-]+)/;  // overall regexp
	// const autoplay_pattern = / /;
	let video_id;
	let timing;
	let video = document.querySelector('video.html5-main-video');
	// l(`new run\n`, location.href, `\n`, video);

	
	function saveTiming () {
		if (!video.ended) {
			GM_setValue(video_id, video.currentTime);
			// i(document.title, video.currentTime);
		} else {
			GM_deleteValue(video_id);
			// l(document.title, '<< timeupdate ended detected');
		}
		if (!video) {l("save: no video!"); return;}
		if (video.paused) i(document.title, video_id, '<< saved', video.currentTime);
	}

	function loadTiming () {
		if (!video) {l("load: no video!"); video = document.querySelector('video.html5-main-video');}
		timing = GM_getValue(video_id);
		if (timing !== undefined) {
			i(document.title, video_id, '>> loading:', timing);
			video.currentTime = timing;
		} else {i(document.title, '>> new video');}
	}
	
	// TODO
	/*
	// if embeded
	if (location.hostname === "") { 
		// if autoplay=1
		if ()
	} else if (location.href.match(main_url_pattern) && (window.self === window.top)) {
		// AND NOT (main AND top)
		
	} else {
		// l('Not yotube video, exiting...');
		return;
	}
	*/
	
	if (location.hostname === "www.youtube.com") {
		video_id = location.href.match(video_id_pattern)[1];
		if (!video) {video = document.querySelector('video.html5-main-video');}
		loadTiming();
	
	// get all GET params
	/*let paramsArray = {};
	location.search.substr(1).split('&').forEach(function(e){
		paramsArray[e.split('=')[0]] = e.split('=')[1];
	});*/

		video.addEventListener('timeupdate', function(){
			// i('timeupdate!');
			saveTiming();
		});
		video.addEventListener('ended', function(){
			saveTiming();
		})
	} else {
		i(location.hostname, 'not youtube');
		return;
	}
})()
{'fraction_non_alphanumeric': 0.2124976821806045, 'fraction_numerical': 0.0037085110328203227, 'mean_word_length': 3.1976653696498056, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911076', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1875, 'n_words': 366}