]> git.mxchange.org Git - friendica-addons.git/blob - invidious/invidious.php
This addon will replace "youtube.com" with the chosen Invidious instance
[friendica-addons.git] / invidious / invidious.php
1 <?php
2 /*
3  * Name: invidious
4  * Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node.
5  * Version: 0.2
6  * Author: Matthias Ebers <https://loma.ml/profile/feb>
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 function invidious_install()
16 {
17     Hook::register('prepare_body_final', 'addon/invidious/invidious.php', 'invidious_render');
18 }
19
20 /* Handle the send data from the admin settings
21  */
22 function invidious_addon_admin_post()
23 {
24     DI::config()->set('invidious', 'server', rtrim(trim($_POST['invidiousserver']), '/'));
25 }
26
27 /* Hook into the admin settings to let the admin choose an
28  * invidious server to use for the replacement.
29  */
30 function invidious_addon_admin(string &$o)
31 {
32     $invidiousserver = DI::config()->get('invidious', 'server');
33     $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/');
34     $o = Renderer::replaceMacros($t, [
35         '$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'),
36         '$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, 'https://example.com'],
37         '$submit' => DI::l10n()->t('Save Settings'),
38     ]);
39 }
40
41 /*
42  *  replace "youtube.com" with the chosen Invidious instance
43  */
44 function invidious_render(array &$b)
45 {
46     // this needs to be a system setting
47     $replaced = false;
48     $invidious = DI::config()->get('invidious', 'server', 'https://invidio.us');
49     if (strstr($b['html'], 'https://www.youtube.com')) {
50         $b['html'] = str_replace(['https://www.youtube.com', 'https://youtube.com'], $invidious, $b['html']);
51         $replaced = true;
52     }
53     if ($replaced) {
54         $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $invidious) . '</small></p>';
55     }
56 }