generated from JohnBra/vite-web-extension
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from VampireChicken12/dev
Dev
- Loading branch information
Showing
11 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add i18n support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { isShortsPage, isWatchPage, waitForSpecificMessage } from "@/src/utils/utilities"; | ||
import { YouTubePlayerDiv } from "@/src/types"; | ||
import { calculateRemainingTime } from "./utils"; | ||
import eventManager from "@/src/utils/EventManager"; | ||
async function playerTimeUpdateListener() { | ||
// Get the player element | ||
const playerContainer = isWatchPage() | ||
? (document.querySelector("div#movie_player") as YouTubePlayerDiv | null) | ||
: isShortsPage() | ||
? (document.querySelector("div#shorts-player") as YouTubePlayerDiv | null) | ||
: null; | ||
|
||
// If player element is not available, return | ||
if (!playerContainer) return; | ||
|
||
// Get the video element | ||
const videoElement = playerContainer.childNodes[0]?.childNodes[0] as HTMLVideoElement | null; | ||
|
||
// If video element is not available, return | ||
if (!videoElement) return; | ||
// Get the remaining time element | ||
const remainingTimeElement = document.querySelector("span#ytp-time-remaining"); | ||
if (!remainingTimeElement) return; | ||
const remainingTime = await calculateRemainingTime({ videoElement, playerContainer }); | ||
remainingTimeElement.textContent = remainingTime; | ||
} | ||
export async function setupRemainingTime() { | ||
// Wait for the "options" message from the content script | ||
const optionsData = await waitForSpecificMessage("options", "request_data", "content"); | ||
if (!optionsData) return; | ||
const { | ||
data: { options } | ||
} = optionsData; | ||
// Extract the necessary properties from the options object | ||
const { enable_remaining_time } = options; | ||
// If remaining time option is disabled, return | ||
if (!enable_remaining_time) return; | ||
const timeDisplay = document.querySelector(".ytp-time-display > span:nth-of-type(2)"); | ||
if (!timeDisplay) return; | ||
// Get the player element | ||
const playerContainer = isWatchPage() | ||
? (document.querySelector("div#movie_player") as YouTubePlayerDiv | null) | ||
: isShortsPage() | ||
? (document.querySelector("div#shorts-player") as YouTubePlayerDiv | null) | ||
: null; | ||
// If player element is not available, return | ||
if (!playerContainer) return; | ||
// Get the video element | ||
const videoElement = playerContainer.childNodes[0]?.childNodes[0] as HTMLVideoElement | null; | ||
// If video element is not available, return | ||
if (!videoElement) return; | ||
const remainingTime = await calculateRemainingTime({ videoElement, playerContainer }); | ||
const remainingTimeElementExists = document.querySelector("span#ytp-time-remaining") !== null; | ||
const remainingTimeElement = document.querySelector("span#ytp-time-remaining") ?? document.createElement("span"); | ||
if (!remainingTimeElementExists) { | ||
remainingTimeElement.id = "ytp-time-remaining"; | ||
remainingTimeElement.textContent = remainingTime; | ||
timeDisplay.insertAdjacentElement("beforeend", remainingTimeElement); | ||
} | ||
eventManager.addEventListener(videoElement, "timeupdate", playerTimeUpdateListener, "remainingTime"); | ||
} | ||
export function removeRemainingTimeDisplay() { | ||
const remainingTimeElement = document.querySelector("span#ytp-time-remaining"); | ||
if (!remainingTimeElement) return; | ||
remainingTimeElement.remove(); | ||
eventManager.removeEventListeners("remainingTime"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { YouTubePlayerDiv } from "@/src/types"; | ||
function formatTime(timeInSeconds: number) { | ||
timeInSeconds = Math.round(timeInSeconds); | ||
const units: number[] = [ | ||
Math.floor(timeInSeconds / (3600 * 24)), | ||
Math.floor((timeInSeconds % (3600 * 24)) / 3600), | ||
Math.floor((timeInSeconds % 3600) / 60), | ||
Math.floor(timeInSeconds % 60) | ||
]; | ||
|
||
const formattedUnits: string[] = units.reduce((acc: string[], unit) => { | ||
if (acc.length > 0) { | ||
acc.push(unit.toString().padStart(2, "0")); | ||
} else { | ||
if (unit > 0) { | ||
acc.push(unit.toString()); | ||
} | ||
} | ||
|
||
return acc; | ||
}, []); | ||
return ` (-${formattedUnits.length > 0 ? formattedUnits.join(":") : "0"})`; | ||
} | ||
export async function calculateRemainingTime({ | ||
videoElement, | ||
playerContainer | ||
}: { | ||
videoElement: HTMLVideoElement; | ||
playerContainer: YouTubePlayerDiv; | ||
}) { | ||
// Get the player speed (playback rate) | ||
const { playbackRate } = videoElement; | ||
|
||
// Get the current time and duration of the video | ||
const currentTime = await playerContainer.getCurrentTime(); | ||
const duration = await playerContainer.getDuration(); | ||
|
||
// Calculate the remaining time in seconds | ||
const remainingTimeInSeconds = (duration - currentTime) / playbackRate; | ||
|
||
// Format the remaining time | ||
return formatTime(remainingTimeInSeconds); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters