-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #64
- Loading branch information
Showing
5 changed files
with
126 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
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,80 @@ | ||
import { waitFor } from "../../maze-utils/src"; | ||
import { waitForElement } from "../../maze-utils/src/dom"; | ||
import { onMobile } from "../../maze-utils/src/pageInfo"; | ||
import { isOnInvidious } from "../../maze-utils/src/video"; | ||
import { getOriginalTitleElement } from "../titles/titleRenderer"; | ||
import { logError } from "../utils/logger"; | ||
import { BrandingLocation, replaceVideoCardBranding } from "./videoBranding"; | ||
|
||
let mutationObserver: MutationObserver | null = null; | ||
async function onNotificationMenuOpened() { | ||
const notificationMenu = await waitForElement("ytd-multi-page-menu-renderer")!; | ||
|
||
if (mutationObserver) { | ||
mutationObserver.disconnect(); | ||
} else { | ||
mutationObserver = new MutationObserver((mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.addedNodes.length > 0) { | ||
for (const node of mutation.addedNodes) { | ||
if (node instanceof HTMLElement && node.tagName.toLowerCase() === "ytd-notification-renderer") { | ||
replaceNotificationBranding(node); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
mutationObserver.observe(notificationMenu, { childList: true, subtree: true }); | ||
} | ||
|
||
function replaceNotificationBranding(notification: HTMLElement) { | ||
// Only if this notification format is supported | ||
const originalTitle = getOriginalTitleElement(notification as HTMLElement, BrandingLocation.Notification)?.textContent; | ||
if (originalTitle && notificationToTitle(originalTitle)) { | ||
replaceVideoCardBranding(notification as HTMLElement, BrandingLocation.Notification).catch(logError); | ||
} | ||
} | ||
|
||
export async function setupNotificationHandler() { | ||
if (!onMobile() && !isOnInvidious()) { | ||
const notificationButton = await waitFor(() => document.querySelector("ytd-notification-topbar-button-renderer"), 20000, 500); | ||
|
||
if (notificationButton) { | ||
notificationButton.addEventListener("click", () => void(onNotificationMenuOpened())); | ||
} | ||
} | ||
} | ||
|
||
const notificationFormats = [ | ||
"$CHANNEL$ uploaded: $TITLE$" | ||
]; | ||
const channelTemplate = "$CHANNEL$"; | ||
const titleTemplate = "$TITLE$"; | ||
function formatToRegex(format: string): RegExp { | ||
return new RegExp(format.replace(channelTemplate, "(.+)").replace(titleTemplate, "(.+)"), "i"); | ||
} | ||
export function notificationToTitle(title: string): string { | ||
for (const format of notificationFormats) { | ||
const titleMatch = title.match(formatToRegex(format))?.[2]; | ||
|
||
if (titleMatch) { | ||
return titleMatch; | ||
} | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
export function titleToNotificationFormat(newTitle: string, originalTitle: string): string { | ||
for (const format of notificationFormats) { | ||
const titleMatch = originalTitle.match(formatToRegex(format))?.[2]; | ||
|
||
if (titleMatch) { | ||
return originalTitle.replace(titleMatch, newTitle); | ||
} | ||
} | ||
|
||
return ""; | ||
} |
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