Skip to content

Commit

Permalink
Merge pull request #35 from Voiture-0/main
Browse files Browse the repository at this point in the history
Copy With Time button formats "t" url param as "1h2m3s" instead of "3723s"🙂
  • Loading branch information
vyneer authored Aug 11, 2023
2 parents 350a721 + 65cfb1a commit 1f007b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 8 additions & 8 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(player.getCurrentTime())}s`);
params.set("t", convertSecondsToTime(player.getCurrentTime()));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});

Expand All @@ -778,7 +778,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(player.getCurrentTime())}`);
params.set("t", convertSecondsToTime(player.getCurrentTime()));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});

Expand Down Expand Up @@ -842,7 +842,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(replacedVideo.currentTime)}`);
params.set("t", convertSecondsToTime(replacedVideo.currentTime));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});
break;
Expand Down Expand Up @@ -872,7 +872,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(replacedVideo.currentTime)}`);
params.set("t", convertSecondsToTime(replacedVideo.currentTime));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});
break;
Expand Down Expand Up @@ -917,7 +917,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(replacedVideo.currentTime)}`);
params.set("t", convertSecondsToTime(replacedVideo.currentTime));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});
});
Expand All @@ -926,7 +926,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
const playerObservser = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.attributeName === 'src') {
if (time) mutation.target.currentTime = Number(time.split("s")[0]);
if (time) mutation.target.currentTime = time;
playerObservser.disconnect();
}
})
Expand All @@ -945,7 +945,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(api.getCurrentTime())}s`);
params.set("t", convertSecondsToTime(api.getCurrentTime()));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});
}});
Expand Down Expand Up @@ -993,7 +993,7 @@ var loadPlayer = function(id, time, type, cdn, start, end, provider, map) {
$("#copy-button").show();
$("#copy-button").click(function () {
let params = new URLSearchParams(window.location.href);
params.set("t", `${Math.round(replacedVideo.currentTime)}`);
params.set("t", convertSecondsToTime(replacedVideo.currentTime));
navigator.clipboard.writeText(`${decodeURIComponent(params.toString())}`);
});
})
Expand Down
26 changes: 22 additions & 4 deletions assets/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var memeMessages = ["<div class='emote YEE' title=YEE></div> neva lie, <div clas
"YOU'RE A DUMBFUCK! <div class='emote REE' title=REE></div> A DUMBFUCK <div class='emote REE' title=REE></div>",
"Gen <div class='emote YEE' title=YEE></div>", "<div class='emote PepoTurkey' title=PepoTurkey></div> goblgoblgobl"];

const SECONDS_IN_HOUR = 60*60;
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_DAY = 24*60*60;
const MINUTES_IN_HOUR = 60;
var convertTimeToSeconds = function(time) {
if (time == null) return null;
// Just digits
Expand All @@ -89,11 +93,25 @@ var convertTimeToSeconds = function(time) {
var matches = time.match(/^(?=.)(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/i);
if (matches == null) return null;
var seconds = 0;
seconds += Number(matches[1] ?? 0) * 60 * 60;// Hours
seconds += Number(matches[2] ?? 0) * 60; // Minutes
seconds += Number(matches[3] ?? 0); // Seconds
seconds += Number(matches[1] ?? 0) * SECONDS_IN_HOUR; // Hours
seconds += Number(matches[2] ?? 0) * SECONDS_IN_MINUTE; // Minutes
seconds += Number(matches[3] ?? 0); // Seconds
return seconds;
}
var convertSecondsToTime = function(seconds) {
if (seconds == null) return null;
// If not a number or negative or more than 24 hours
if (isNaN(seconds) || 0 > seconds || seconds > SECONDS_IN_DAY) return seconds;
seconds = Math.round(Number(seconds)); // cast as a number and round to be safe
var hrs = Math.floor(seconds / SECONDS_IN_HOUR);
var min = Math.floor(seconds / SECONDS_IN_MINUTE) % MINUTES_IN_HOUR;
var sec = seconds % SECONDS_IN_MINUTE;
var time = "";
if (seconds >= SECONDS_IN_HOUR) time += hrs + "h";
if (seconds >= SECONDS_IN_MINUTE) time += min + "m";
time += sec + "s";
return time;
}

var formatLength = function(seconds) {
var time = Math.floor(Number(seconds));
Expand Down Expand Up @@ -523,4 +541,4 @@ function updateASCIILimit() {

function updateIgnoredPhrases() {
localStorage.setItem('ignoredPhrases', document.getElementById("ignoredPhrases").value);
}
}

0 comments on commit 1f007b3

Please sign in to comment.