Allows to create, store and access video timestamps.
当前为
// ==UserScript==
// @name Youtube Timestamps
// @name:fr Horodatage youtube
// @namespace YTime
// @include *youtube.com*
// @version 1.0.1
// @author lapincroyable
// @description Allows to create, store and access video timestamps.
// @description:fr Permet de créer, stocker, et accéder aux horodatages vidéos.
// @run-at document-idle
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
// -----------------------
var YTimeList = [];
var YTimeValue = GM_getValue("YTIMELIST");
if (YTimeValue != undefined){ YTimeList = JSON.parse(YTimeValue); }
var IDVid ;
var Player ;
var btnStyle = {
borderRadius : "16px",
border : "2px solid black",
margin : "5px",
padding : "0px",
heigth : "20px"
};
var RemAllStyle = {
backgroundColor : "lightsalmon",
width : "20px"
};
var AddStyle = {
backgroundColor : "lightgreen",
width : "20px"
};
var RemStyle = {
backgroundColor : "black",
color : "white",
"margin-left" : "-26px",
width : "20px"
};
var TSStyle = {
"padding" : "0px 30px 0px 10px",
backgroundColor :"#ffffff80"
}
// -----------------------
function ConvSec(timestamp){
let split = timestamp.split(":");
let result = 0;
if(split.length > 2){
result = parseInt(split[0])*3600 + parseInt(split[1])*60 + parseInt(split[2]);
} else {
result = parseInt(split[0])*60 + parseInt(split[1]);
}
return result;
}
function LoadTimes(){
let indexvid = YTimeList.findIndex(i => i[0]===IDVid);
if(indexvid != -1){
YTimeList[indexvid].shift();
YTimeList[indexvid].sort();
for (let indexstamp in YTimeList[indexvid]){
AddTSButton(YTimeList[indexvid][indexstamp]);
}
YTimeList[indexvid].unshift(IDVid);
}
}
function AddTS(timestamp){
let indexvid = YTimeList.findIndex(i => i[0]===IDVid);
if(indexvid == -1){
YTimeList.push([IDVid,timestamp]);
GM_setValue("YTIMELIST",JSON.stringify(YTimeList))
AddTSButton(timestamp);
}else{
let indexstamp = YTimeList[indexvid].findIndex(i => i===timestamp);
if(indexstamp == -1){
YTimeList[indexvid].push(timestamp);
GM_setValue("YTIMELIST",JSON.stringify(YTimeList))
AddTSButton(timestamp);
}
}
}
function AddTSButton(timestamp){
let TSBar = document.getElementById("tsbar");
let Sec = ConvSec(timestamp);
let TSButton= document.createElement("button");
TSButton.innerHTML=timestamp;
Object.assign(TSButton.style,btnStyle,TSStyle);
TSButton.addEventListener("click",function(){
document.getElementsByClassName("html5-main-video")[0].currentTime = Sec;
document.getElementsByClassName("html5-main-video")[0].play();
});
let RemTSButton = document.createElement("button");
RemTSButton.innerHTML="x";
Object.assign(RemTSButton.style,btnStyle,RemStyle);
RemTSButton.addEventListener("click",function(){
RemTS(timestamp);
});
TSBar.appendChild(TSButton);
TSBar.appendChild(RemTSButton);
}
function RemTS(timestamp){
let indexvid = YTimeList.findIndex(i => i[0]===IDVid);
let indexstamp = YTimeList[indexvid].findIndex(i => i===timestamp);
YTimeList[indexvid].splice(indexstamp,1);
GM_setValue("YTIMELIST",JSON.stringify(YTimeList));
CleanBar();
LoadTimes();
}
function RemAllTS(){
let indexvid = YTimeList.findIndex(i => i[0]===IDVid);
if(indexvid != -1){
YTimeList.splice(indexvid,1);
GM_setValue("YTIMELIST",JSON.stringify(YTimeList));
CleanBar();
}
}
function CleanBar(){
let TSBar = document.getElementById("tsbar");
for (let i=TSBar.childElementCount;i>2;i--) {
TSBar.removeChild(TSBar.lastChild);
}
}
function check(changes, observer) {
if(Player.baseURI.match("watch")){
if(IDVid != (Player.baseURI.split("v=")[1].split("&")[0])){
IDVid = Player.baseURI.split("v=")[1].split("&")[0];
CleanBar();
LoadTimes();
}
}
}
function loadonwatch(changes, observer) {
if(document.baseURI.match("watch") && document.getElementById("info-contents")){
observer.disconnect();
Player = document.getElementsByClassName("html5-main-video")[0];
IDVid = Player.baseURI.split("v=")[1].split("&")[0];
let ZoneInfo = document.getElementById("info-contents");
let TSBar = document.createElement("div");
TSBar.setAttribute("id","tsbar");
let RemAllButton = document.createElement("button");
RemAllButton.innerHTML ="-";
Object.assign(RemAllButton.style,btnStyle,RemAllStyle);
RemAllButton.addEventListener("click",function(){
RemAllTS();
});
let AddButton = document.createElement("button");
AddButton.innerHTML ="+";
Object.assign(AddButton.style,btnStyle,AddStyle);
AddButton.addEventListener("click",function(){
let Current = document.getElementsByClassName("ytp-time-current")[0].innerHTML;
AddTS(Current);
});
ZoneInfo.parentNode.insertBefore(TSBar,ZoneInfo);
TSBar.appendChild(RemAllButton);
TSBar.appendChild(AddButton);
LoadTimes();
(new MutationObserver(check)).observe(Player, {attributes: true, subtree: true});
}
}
(new MutationObserver(loadonwatch)).observe(document, {attributes: true, subtree: true});