From 8ba44cf5c67b7b99107b66a92429a0a1907feb2c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 18 Dec 2023 17:47:19 +0000 Subject: [PATCH] Invidious: The addon is now user definable --- invidious/invidious.php | 78 +++++++++++++++++++++++++------- invidious/lang/C/messages.po | 32 ++++++++++--- invidious/templates/settings.tpl | 2 + 3 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 invidious/templates/settings.tpl diff --git a/invidious/invidious.php b/invidious/invidious.php index 7692229d..d6152ce4 100644 --- a/invidious/invidious.php +++ b/invidious/invidious.php @@ -2,26 +2,30 @@ /* * Name: invidious * Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node. - * Version: 0.2 + * Version: 0.3 * Author: Matthias Ebers + * Author: Michael Vogel * */ -use Friendica\App; use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\DI; +CONST INVIDIOUS_DEFAULT = 'https://invidio.us'; + function invidious_install() { - Hook::register('prepare_body_final', 'addon/invidious/invidious.php', 'invidious_render'); + Hook::register('prepare_body_final', __FILE__, 'invidious_render'); + Hook::register('addon_settings', __FILE__, 'invidious_settings'); + Hook::register('addon_settings_post', __FILE__, 'invidious_settings_post'); } /* Handle the send data from the admin settings */ function invidious_addon_admin_post() { - DI::config()->set('invidious', 'server', rtrim(trim($_POST['invidiousserver']), '/')); + DI::config()->set('invidious', 'server', trim($_POST['invidiousserver'], " \n\r\t\v\x00/")); } /* Hook into the admin settings to let the admin choose an @@ -29,29 +33,71 @@ function invidious_addon_admin_post() */ function invidious_addon_admin(string &$o) { - $invidiousserver = DI::config()->get('invidious', 'server'); + $invidiousserver = DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT); $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/'); $o = Renderer::replaceMacros($t, [ '$settingdescription' => DI::l10n()->t('Which Invidious server shall be used for the replacements in the post bodies? Use the URL with servername and protocol. See %s for a list of available public Invidious servers.', 'https://redirect.invidious.io'), - '$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, 'https://example.com'], - '$submit' => DI::l10n()->t('Save Settings'), + '$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, DI::l10n()->t('See %s for a list of available Invidious servers.', 'https://api.invidious.io/')], + '$submit' => DI::l10n()->t('Save Settings'), ]); } +function invidious_settings(array &$data) +{ + if (!DI::userSession()->getLocalUserId()) { + return; + } + + $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled'); + $server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT)); + + $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/invidious/'); + $html = Renderer::replaceMacros($t, [ + '$enabled' => ['enabled', DI::l10n()->t('Replace Youtube links with links to an Invidious server'), $enabled, DI::l10n()->t('If enabled, Youtube links are replaced with the links to the specified Invidious server.')], + '$server' => ['server', DI::l10n()->t('Invidious server'), $server, DI::l10n()->t('See %s for a list of available Invidious servers.', 'https://api.invidious.io/')], + ]); + + $data = [ + 'addon' => 'invidious', + 'title' => DI::l10n()->t('Invidious Settings'), + 'html' => $html, + ]; +} + +function invidious_settings_post(array &$b) +{ + if (!DI::userSession()->getLocalUserId() || empty($_POST['invidious-submit'])) { + return; + } + + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'enabled', (bool)$_POST['enabled']); + + $server = trim($_POST['server'], " \n\r\t\v\x00/"); + if ($server != DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT) && !empty($server)) { + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'server', $server); + } else { + DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'invidious', 'server'); + } +} + /* * replace "youtube.com" with the chosen Invidious instance */ function invidious_render(array &$b) { - // this needs to be a system setting - $replaced = false; - $invidious = DI::config()->get('invidious', 'server', 'https://invidio.us'); - if (strpos($b['html'], 'https://www.youtube.com/') !== false || strpos($b['html'], 'https://youtube.com/') !== false || strpos($b['html'], 'https://youtu.be/') !== false) { - $b['html'] = str_replace('https://youtu.be/', $invidious . '/watch?v=', $b['html']); - $b['html'] = str_replace(['https://www.youtube.com/', 'https://youtube.com/'], $invidious . '/', $b['html']); - $replaced = true; + if (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled')) { + return; } - if ($replaced) { - $b['html'] .= '

' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $invidious) . '

'; + + $original = $b['html']; + $server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT)); + + $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/watch\?v\=(.*?)/ism", $server . '/watch?v=$1', $b['html']); + $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/embed\/(.*?)/ism", $server . '/embed/$1', $b['html']); + $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/shorts\/(.*?)/ism", $server . '/shorts/$1', $b['html']); + $b['html'] = preg_replace("/https?:\/\/youtu.be\/(.*?)/ism", $server . '/watch?v=$1', $b['html']); + + if ($original != $b['html']) { + $b['html'] .= '

' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $server) . '

'; } } diff --git a/invidious/lang/C/messages.po b/invidious/lang/C/messages.po index f93a87d6..b9de496c 100644 --- a/invidious/lang/C/messages.po +++ b/invidious/lang/C/messages.po @@ -1,4 +1,4 @@ - # ADDON invidious +# ADDON invidious # Copyright (C) # This file is distributed under the same license as the Friendica invidious addon package. # @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-12-03 20:21+0100\n" +"POT-Creation-Date: 2023-12-18 17:23+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,7 +17,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: invidious.php:35 +#: invidious.php:39 #, php-format msgid "" "Which Invidious server shall be used for the replacements in the post " @@ -25,16 +25,34 @@ msgid "" "available public Invidious servers." msgstr "" -#: invidious.php:36 +#: invidious.php:40 invidious.php:57 msgid "Invidious server" msgstr "" -#: invidious.php:37 +#: invidious.php:40 invidious.php:57 +#, php-format +msgid "See %s for a list of available Invidious servers." +msgstr "" + +#: invidious.php:41 msgid "Save Settings" msgstr "" -#: invidious.php:64 +#: invidious.php:56 +msgid "Replace Youtube links with links to an Invidious server" +msgstr "" + +#: invidious.php:56 +msgid "" +"If enabled, Youtube links are replaced with the links to the specified " +"Invidious server." +msgstr "" + +#: invidious.php:62 +msgid "Invidious Settings" +msgstr "" + +#: invidious.php:101 #, php-format msgid "(Invidious addon enabled: YouTube links via %s)" msgstr "" - diff --git a/invidious/templates/settings.tpl b/invidious/templates/settings.tpl new file mode 100644 index 00000000..387286ef --- /dev/null +++ b/invidious/templates/settings.tpl @@ -0,0 +1,2 @@ +{{include file="field_checkbox.tpl" field=$enabled}} +{{include file="field_input.tpl" field=$server}}