Merge pull request 'This addon will replace "youtube.com" with the chosen Invidious instance' (#2) from loma-one-patch-2 into develop

Reviewed-on: loma-one/friendica-addons#2
This commit is contained in:
loma-one 2023-12-03 16:43:08 +01:00
commit f52c69ebaa
4 changed files with 74 additions and 0 deletions

4
invidious/README.md Normal file
View File

@ -0,0 +1,4 @@
invidious Addon for Friendica
==========================
This addon will replace "youtube.com" with the chosen Invidious instance

64
invidious/invidious.php Normal file
View File

@ -0,0 +1,64 @@
<?php
/*
* Name: invidious
* Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node.
* Version: 0.1
* Author: Matthias Ebers <@feb@loma.ml>
*
*/
use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\Renderer;
use Friendica\DI;
function invidious_install()
{
Hook::register('prepare_body_final', 'addon/invidious/invidious.php', 'invidious_render');
}
/* Handle the send data from the admin settings
*/
function invidious_addon_admin_post()
{
DI::config()->set('invidious', 'server', rtrim(trim($_POST['invidiousserver']), '/'));
}
/* Hook into the admin settings to let the admin choose an
* invidious server to use for the replacement.
*/
function invidious_addon_admin(string &$o)
{
$invidiousserver = DI::config()->get('invidious', 'server');
$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'),
]);
}
/*
* 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 (strstr($b['html'], 'https://www.youtube.com')) {
$b['html'] = str_replace('https://www.youtube.com', $invidious, $b['html']);
$replaced = true;
}
if (strstr($b['html'], 'https://youtube.com')) {
$b['html'] = str_replace('https://youtube.com', $invidious, $b['html']);
$replaced = true;
}
if (strstr($b['html'], 'https://youtu.be')) {
$b['html'] = str_replace('https://youtu.be', $invidious, $b['html']);
$replaced = true;
}
if ($replaced) {
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $invidious) . '</small></p>';
}
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,5 @@
<p>{{$settingdescription}}</p>
{{include file="field_input.tpl" field=$invidiousserver}}
<div class="submit"><input type="submit" name="page_site" value="{{$submit}}" /></div>