4 * Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node.
6 * Author: Matthias Ebers <https://loma.ml/profile/feb>
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
15 CONST INVIDIOUS_DEFAULT = 'https://invidio.us';
17 function invidious_install()
19 Hook::register('prepare_body_final', __FILE__, 'invidious_render');
20 Hook::register('addon_settings', __FILE__, 'invidious_settings');
21 Hook::register('addon_settings_post', __FILE__, 'invidious_settings_post');
24 /* Handle the send data from the admin settings
26 function invidious_addon_admin_post()
28 DI::config()->set('invidious', 'server', trim($_POST['invidiousserver'], " \n\r\t\v\x00/"));
31 /* Hook into the admin settings to let the admin choose an
32 * invidious server to use for the replacement.
34 function invidious_addon_admin(string &$o)
36 $invidiousserver = DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT);
37 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/');
38 $o = Renderer::replaceMacros($t, [
39 '$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'),
40 '$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
41 '$submit' => DI::l10n()->t('Save Settings'),
45 function invidious_settings(array &$data)
47 if (!DI::userSession()->getLocalUserId()) {
51 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled');
52 $server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT));
54 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/invidious/');
55 $html = Renderer::replaceMacros($t, [
56 '$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.')],
57 '$server' => ['server', DI::l10n()->t('Invidious server'), $server, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
61 'addon' => 'invidious',
62 'title' => DI::l10n()->t('Invidious Settings'),
67 function invidious_settings_post(array &$b)
69 if (!DI::userSession()->getLocalUserId() || empty($_POST['invidious-submit'])) {
73 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'enabled', (bool)$_POST['enabled']);
75 $server = trim($_POST['server'], " \n\r\t\v\x00/");
76 if ($server != DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT) && !empty($server)) {
77 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'server', $server);
79 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'invidious', 'server');
84 * replace "youtube.com" with the chosen Invidious instance
86 function invidious_render(array &$b)
88 if (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled')) {
92 $original = $b['html'];
93 $server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT));
95 $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/watch\?v\=(.*?)/ism", $server . '/watch?v=$1', $b['html']);
96 $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/embed\/(.*?)/ism", $server . '/embed/$1', $b['html']);
97 $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/shorts\/(.*?)/ism", $server . '/shorts/$1', $b['html']);
98 $b['html'] = preg_replace("/https?:\/\/youtu.be\/(.*?)/ism", $server . '/watch?v=$1', $b['html']);
100 if ($original != $b['html']) {
101 $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $server) . '</small></p>';