From 6fb9ee7c3872ced68ca7db0a0a314b5e2ef6d459 Mon Sep 17 00:00:00 2001 From: phga Date: Fri, 19 Feb 2021 17:40:24 +0100 Subject: [PATCH] feat: embed yt-videos as iframes --- init.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 init.php diff --git a/init.php b/init.php new file mode 100644 index 0000000..200d0ac --- /dev/null +++ b/init.php @@ -0,0 +1,46 @@ +host = $host; + + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); + $host->add_hook($host::HOOK_IFRAME_WHITELISTED, $this); + } + + function hook_iframe_whitelisted($src) { + return in_array($src, ["www.youtube.com", "youtube.com", "youtu.be"]); + } + + function hook_article_filter($article) { + + // Match allowed links and extract "video ID" + if (preg_match_all("/.*(youtube|youtu)\.(com|be)\/watch\?v=([\w\d_-]+).*/i", + $article["link"], $matches, PREG_PATTERN_ORDER)) { + + if ($matches[3][0]) { + $link = "https://www.youtube.com/embed/" . $matches[3][0]; + $attributes = "width='819' height='460' class='youtube-player' allowfullscreen"; + $yframe = ""; + + $article["content"] = $yframe; + } + } + + $article["content"] .= "
" . $link; + return $article; + } + + function api_version() { + return 2; + } + +}