youtube nocookie

This commit is contained in:
Anton Tranelis 2025-06-15 00:02:17 +02:00
parent 5ed20dbc05
commit 895c31a54a
5 changed files with 25 additions and 29 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
lib/node_modules/
lib/dist/
frontend/node_modules/
frontend/dist/

View File

@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { log } from 'node:console'
import { Color } from '@tiptap/extension-color'
import { Image } from '@tiptap/extension-image'
import { Link } from '@tiptap/extension-link'
@ -209,12 +207,14 @@ export function getStyledMarkdown(editor: Editor): string {
/(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/,
)
const videoId = match?.[1]
const nocookieUrl = `https://www.youtube-nocookie.com/embed/${videoId}`
if (videoId) {
const nocookieUrl = `https://www.youtube-nocookie.com/embed/${videoId}`
let tag = '<div class="tw:w-full tw:aspect-video tw:overflow-hidden">'
tag += `<iframe src="${nocookieUrl}" allowfullscreen class="tw-w-full tw-h-full" loading="lazy"></iframe>`
tag += '</div>'
state.write(tag)
let tag = '<div class="tw:w-full tw:aspect-video tw:overflow-hidden">'
tag += `<iframe src="${nocookieUrl}" allowfullscreen class="tw-w-full tw-h-full" loading="lazy"></iframe>`
tag += '</div>'
state.write(tag)
}
}
const customSerializer = new MarkdownSerializer(
@ -258,16 +258,12 @@ const CustomYoutube = Youtube.extend({
tag: 'iframe[src*="/embed/"]',
priority: 1000,
getAttrs: (dom) => {
const src = (dom as HTMLIFrameElement).getAttribute('src') || ''
console.log(src)
const src = (dom as HTMLIFrameElement).getAttribute('src') ?? ''
const match = src.match(/\/embed\/([A-Za-z0-9_-]{11})/)
console.log(match)
if (!match) {
return false
}
const videoId = match[1]
console.log(videoId)
// immer auf nocookie normieren
return {
src: `https://www.youtube-nocookie.com/embed/${videoId}`,
}

View File

@ -20,6 +20,7 @@ import { fixUrls, mailRegex } from '#utils/ReplaceURLs'
import type { Item } from '#types/Item'
import type { Tag } from '#types/Tag'
import type { Root, Element } from 'hast'
/**
* @category Map
@ -246,20 +247,25 @@ export const sanitizeSchema = {
}
export function rehypeFilterYouTubeIframes() {
return (tree: any) => {
return (tree: Root) => {
visit(tree, 'element', (node) => {
if (node.tagName === 'iframe') {
const src = String(node.properties?.src || '')
// node ist hier typischerweise ein HAST-Element
const el = node
if (el.tagName === 'iframe') {
const src = String(el.properties?.src ?? '')
// Nur echte YouTube-Embed-URLs zulassen
if (
// eslint-disable-next-line security/detect-unsafe-regex
!/^https:\/\/(?:www\.)?(?:youtube\.com|youtube-nocookie\.com)\/embed\/[A-Za-z0-9_-]+(?:\?.*)?$/.test(
src,
)
) {
// ersetze es durch einen leeren div
node.tagName = 'div'
node.properties = {}
node.children = []
el.tagName = 'div'
el.properties = {}
el.children = []
}
}
})

View File

@ -1,8 +0,0 @@
{
"hash": "af787104",
"configHash": "61d63c0d",
"lockfileHash": "fc7082fc",
"browserHash": "ef8d4f33",
"optimized": {},
"chunks": {}
}

View File

@ -1,3 +0,0 @@
{
"type": "module"
}