123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import { scriptInfo, triesInterval, triesLimit } from "../constants";
- import { getFeatures } from "../config";
- import { addGlobalStyle, autoPlural, error, getAssetUrl, getEvtData, insertAfter, log, openInNewTab, siteEvents } from "../utils";
- import type { FeatureConfig } from "../types";
- import { openMenu } from "./menu/menu_old";
- import "./layout.css";
- import { getGeniusUrl, getLyricsBtn, sanitizeArtists, sanitizeSong } from "./lyrics";
- let features: FeatureConfig;
- export async function preInitLayout() {
- features = await getFeatures();
- }
- //#MARKER watermark
- /** Adds a watermark beneath the logo */
- export function addWatermark() {
- const watermark = document.createElement("a");
- watermark.role = "button";
- watermark.id = "betterytm-watermark";
- watermark.className = "style-scope ytmusic-nav-bar";
- watermark.innerText = scriptInfo.name;
- watermark.title = "Open menu";
- watermark.tabIndex = 1000;
- watermark.addEventListener("click", () => openMenu());
- watermark.addEventListener("keydown", (e) => e.key === "Enter" && openMenu());
- const logoElem = document.querySelector("#left-content") as HTMLElement;
- insertAfter(logoElem, watermark);
- log("Added watermark element:", watermark);
- }
- //#MARKER remove upgrade tab
- let removeUpgradeTries = 0;
- /** Removes the "Upgrade" / YT Music Premium tab from the title / nav bar */
- export function removeUpgradeTab() {
- const tabElem = document.querySelector(".ytmusic-nav-bar ytmusic-pivot-bar-item-renderer[tab-id=\"SPunlimited\"]");
- if(tabElem) {
- tabElem.remove();
- log(`Removed upgrade tab after ${removeUpgradeTries} tries`);
- }
- else if(removeUpgradeTries < triesLimit) {
- setTimeout(removeUpgradeTab, triesInterval); // TODO: improve this
- removeUpgradeTries++;
- }
- else
- error(`Couldn't find upgrade tab to remove after ${removeUpgradeTries} tries`);
- }
- //#MARKER volume slider
- /** Sets the volume slider to a set size */
- export function setVolSliderSize() {
- const { volumeSliderSize: size } = features;
- if(typeof size !== "number" || isNaN(Number(size)))
- return;
- const style = `\
- .volume-slider.ytmusic-player-bar, .expand-volume-slider.ytmusic-player-bar {
- width: ${size}px !important;
- }`;
- addGlobalStyle(style, "vol-slider");
- }
- /** Sets the `step` attribute of the volume slider */
- export function setVolSliderStep() {
- const sliderElem = document.querySelector("tp-yt-paper-slider#volume-slider") as HTMLInputElement;
- sliderElem.setAttribute("step", String(features.volumeSliderStep));
- }
- //#MARKER queue buttons
- // TODO: account for the fact initially the elements might not exist, if the site was not opened directly with a video playing or via the /watch path
- export function initQueueButtons() {
- siteEvents.on("queueChanged", (evt) => {
- let amt = 0;
- for(const queueItm of getEvtData<HTMLElement>(evt).childNodes as NodeListOf<HTMLElement>) {
- if(!queueItm.classList.contains("bytm-has-queue-btns")) {
- addQueueButtons(queueItm);
- amt++;
- }
- }
- if(amt > 0)
- log(`Added buttons to ${amt} new queue ${autoPlural("item", amt)}`);
- });
- const queueItems = document.querySelectorAll("#contents.ytmusic-player-queue > ytmusic-player-queue-item");
- if(queueItems.length === 0)
- return;
- queueItems.forEach(itm => addQueueButtons(itm as HTMLElement));
- log(`Added buttons to ${queueItems.length} existing queue items`);
- }
- /**
- * Adds the buttons to each item in the current song queue.
- * Also observes for changes to add new buttons to new items in the queue.
- */
- async function addQueueButtons(queueItem: HTMLElement) {
- const queueBtnsCont = document.createElement("div");
- queueBtnsCont.className = "bytm-queue-btn-container";
- const songInfo = queueItem.querySelector(".song-info") as HTMLElement;
- if(!songInfo)
- return false;
- const [songEl, artistEl] = (songInfo.querySelectorAll("yt-formatted-string") as NodeListOf<HTMLElement>);
- const song = songEl.innerText;
- const artist = artistEl.innerText;
- if(!song || !artist)
- return false;
- // TODO: display "currently loading" icon
- const lyricsBtnElem = getLyricsBtn(undefined, false);
- lyricsBtnElem.title = "Open this song's lyrics in a new tab";
- lyricsBtnElem.style.cursor = "pointer";
- lyricsBtnElem.style.visibility = "initial";
- lyricsBtnElem.style.display = "inline-flex";
- lyricsBtnElem.style.pointerEvents = "initial";
- lyricsBtnElem.addEventListener("click", async () => {
- let lyricsUrl;
- if(songInfo.dataset.bytmLyrics && songInfo.dataset.bytmLyrics.length > 0)
- lyricsUrl = songInfo.dataset.bytmLyrics;
- else if(songInfo.dataset.bytmLoading !== "true") {
- songInfo.dataset.bytmLoading = "true";
- const imgEl = lyricsBtnElem.querySelector("img") as HTMLImageElement;
- imgEl.src = getAssetUrl("loading.gif");
- lyricsUrl = await getGeniusUrl(sanitizeArtists(artist), sanitizeSong(song));
- songInfo.dataset.bytmLoading = "false";
- imgEl.src = getAssetUrl("external/genius.png");
- if(!lyricsUrl) {
- if(confirm("Couldn't find a lyrics page for this song.\nDo you want to open genius.com to manually search for it?"))
- openInNewTab("https://genius.com/search");
- return;
- }
- songInfo.dataset.bytmLyrics = lyricsUrl;
- }
- lyricsUrl && openInNewTab(lyricsUrl);
- });
- queueBtnsCont.appendChild(lyricsBtnElem);
- songInfo.appendChild(queueBtnsCont);
- queueItem.classList.add("bytm-has-queue-btns");
- return true;
- }
- //#MARKER better clickable stuff
- // TODO: account for the fact initially the elements might not exist, if the site was opened directly with the /watch path
- export function addAnchorImprovements() {
- void 0;
- }
|