summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-05-07 07:05:01 +0300
committerAndrew Dolgov <[email protected]>2021-05-07 07:05:01 +0300
commit417bfe1db1ec316e6644e35aa39e0ac8077059be (patch)
tree625d84cfc8d0653f81c01426e9095bdc0b54803d
parent0b7db7d55ae747c2f3a5f7e11c9d231d93a3e929 (diff)
deal with iframe elements in article content
-rw-r--r--init.php92
1 files changed, 85 insertions, 7 deletions
diff --git a/init.php b/init.php
index c16b903..c1eaee1 100644
--- a/init.php
+++ b/init.php
@@ -12,22 +12,40 @@ class Af_Youtube_Thumb extends Plugin {
$this->host = $host;
$host->add_hook($host::HOOK_RENDER_ENCLOSURE, $this);
+ $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
+ $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
}
function get_css() {
return file_get_contents(__DIR__ . "/init.css");
}
+ function url_to_youtube_vid($url) {
+ $url = str_replace("youtube.com", "youtube-nocookie.com", $url);
+
+ $regexps = [
+ "/\/\/www\.youtube-nocookie\.com\/v\/([\w-]+)/",
+ "/\/\/www\.youtube-nocookie\.com\/embed\/([\w-]+)/",
+ "/\/\/www\.youtube-nocookie\.com\/watch?v=([\w-]+)/",
+ "/\/\/youtu.be\/([\w-]+)/",
+ ];
+
+ foreach ($regexps as $re) {
+ $matches = [];
+
+ if (preg_match($re, $url, $matches)) {
+ return $matches[1];
+ }
+ }
+
+ return false;
+ }
+
function hook_render_enclosure($entry, $hide_images) {
- $matches = array();
$url = $entry["content_url"];
- if (preg_match("/\/\/www\.youtube\.com\/v\/([\w-]+)/", $url, $matches) ||
- preg_match("/\/\/www\.youtube\.com\/watch?v=([\w-]+)/", $url, $matches) ||
- preg_match("/\/\/youtu.be\/([\w-]+)/", $url, $matches)) {
-
- $vid_id = $matches[1];
+ if ($vid_id = $this->url_to_youtube_vid($url)) {
$thumb_url = htmlspecialchars("https://img.youtube.com/vi/$vid_id/hqdefault.jpg");
$url = htmlspecialchars($url);
@@ -35,12 +53,72 @@ class Af_Youtube_Thumb extends Plugin {
return "<a target='_blank' rel='noopener noreferrer' href=\"$url\" title=\"".$this->__("Click to open video")."\">
<div class='youtube-thumb'>
<img class='thumbnail' src=\"$thumb_url\" referrerpolicy='no-referrer'>
- <div class='watermark' src=\"plugins.local/af_youtube_thumb/img/youtube.svg\">
+ <div class='watermark'></div>
</div>
</a>";
}
}
+ function hook_render_article($article) {
+ return $this->hook_render_article_cdm($article);
+ }
+
+ /* function hook_render_article_api($row) {
+ $article = isset($row['headline']) ? $row['headline'] : $row['article'];
+
+ return $this->hook_render_article_cdm($article, true);
+ } */
+
+ function hook_render_article_cdm($article, $api_mode = false) {
+ $doc = new DOMDocument();
+ $need_saving = false;
+
+ if (!empty($article["content"]) && @$doc->loadHTML($article["content"])) {
+ $xpath = new DOMXPath($doc);
+ $iframes = $xpath->query("//iframe[@src]");
+
+ foreach ($iframes as $iframe) {
+ $url = $iframe->getAttribute("src");
+
+ if ($vid_id = $this->url_to_youtube_vid($url)) {
+ $thumb_url = htmlspecialchars("https://img.youtube.com/vi/$vid_id/hqdefault.jpg");
+
+ $img = $doc->createElement("img");
+ $img->setAttribute("src", $thumb_url);
+
+ $watermark = $doc->createElement("div");
+ $watermark->setAttribute("class", "watermark");
+
+ $div = $doc->createElement("div");
+ $div->setAttribute("class", "youtube-thumb");
+
+ $div->appendChild($img);
+ $div->appendChild($watermark);
+
+ $a = $doc->createElement("a");
+ $a->setAttribute("target", "_blank");
+ $a->setAttribute("href", $url);
+
+ $a->appendChild($div);
+
+ $parent = $iframe->parentNode;
+
+ if ($parent->getAttribute("class") == "embed-responsive")
+ $parent->setAttribute("class", "");
+
+ $parent->replaceChild($a, $iframe);
+
+ $need_saving = true;
+ }
+ }
+ }
+
+ if ($need_saving) $article["content"] = $doc->saveXML();
+
+ return $article;
+ }
+
+
function api_version() {
return 2;
}