This addon will replace "youtube.com" with the chosen Invidious instance #1441

Merged
MrPetovan merged 19 commits from :develop into 2023.09-rc 2023-12-08 20:50:23 +01:00
4 changed files with 74 additions and 0 deletions
Showing only changes of commit f52c69ebaa - Show all commits

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')) {

You need to also check for 'https://youtube.com' now that the replacement has been combined. Additionally strpos() has better performance for what you're trying to do here.

And what about https://youtu.be/[video id] links? They need to be replaced by [Invidious domain]/?watch=[video id].

Lastly, I added an ending slash to the needle strings to match video links. If someone just mentions youtube.com and links to it, it shouldn't be replaced with an Invidious link.

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=', $b['html']);
    $b['html'] = str_replace(['https://www.youtube.com/', 'https://youtube.com/'], $invidious . '/', $b['html']);
You need to also check for `'https://youtube.com'` now that the replacement has been combined. Additionally `strpos()` has better performance for what you're trying to do here. And what about `https://youtu.be/[video id]` links? They need to be replaced by `[Invidious domain]/?watch=[video id]`. Lastly, I added an ending slash to the needle strings to match video links. If someone just mentions youtube.com and links to it, it shouldn't be replaced with an Invidious link. ```php 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=', $b['html']); $b['html'] = str_replace(['https://www.youtube.com/', 'https://youtube.com/'], $invidious . '/', $b['html']); ```
$b['html'] = str_replace('https://www.youtube.com', $invidious, $b['html']);
$replaced = true;
}
if (strstr($b['html'], 'https://youtube.com')) {

Why is this block duplicated?

Why is this block duplicated?

To ensure that different spellings with www and without are intercepted and redirected.

To ensure that different spellings with **www** and without are intercepted and redirected.

Thanks for the reply, the indentation still needs to be corrected.

Thanks for the reply, the indentation still needs to be corrected.

you could combine the str_replace with an array as first parameter.

you could combine the `str_replace` with an array as first parameter.
$b['html'] = str_replace('https://youtube.com', $invidious, $b['html']);
$replaced = true;
}
if (strstr($b['html'], 'https://youtu.be')) {

You can simply do

            $b['html'] = str_replace(['https://www.youtube.com', 'https://youtube.com'], $invidious, $b['html']);
You can simply do ``` $b['html'] = str_replace(['https://www.youtube.com', 'https://youtube.com'], $invidious, $b['html']); ```
$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>