This addon will replace "youtube.com" with the chosen Invidious instance #1441
5 changed files with 111 additions and 0 deletions
4
invidious/README.md
Normal file
4
invidious/README.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
invidious Addon for Friendica
|
||||||
|
==========================
|
||||||
|
|
||||||
|
This addon will replace "youtube.com" with the chosen Invidious instance
|
57
invidious/invidious.php
Normal file
57
invidious/invidious.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Name: invidious
|
||||||
|
* Description: Replaces links to youtube.com to an invidious instance in all displays of postings on a node.
|
||||||
|
* Version: 0.2
|
||||||
|
* Author: Matthias Ebers <https://loma.ml/profile/feb>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 (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;
|
||||||
|
}
|
||||||
heluecht
commented
Why is this block duplicated? Why is this block duplicated?
loma-one
commented
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.
MrPetovan
commented
Thanks for the reply, the indentation still needs to be corrected. Thanks for the reply, the indentation still needs to be corrected.
heluecht
commented
you could combine the you could combine the `str_replace` with an array as first parameter.
|
|||||||
|
if ($replaced) {
|
||||||
|
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $invidious) . '</small></p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
heluecht
commented
You can simply do
You can simply do
```
$b['html'] = str_replace(['https://www.youtube.com', 'https://youtube.com'], $invidious, $b['html']);
```
|
40
invidious/lang/C/messages.po
Normal file
40
invidious/lang/C/messages.po
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# ADDON invidious
|
||||||
|
# Copyright (C)
|
||||||
|
# This file is distributed under the same license as the Friendica invidious addon package.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#, fuzzy
|
||||||
|
msgid ""
|
||||||
|
msgstr ""
|
||||||
|
"Project-Id-Version: \n"
|
||||||
|
"Report-Msgid-Bugs-To: \n"
|
||||||
|
"POT-Creation-Date: 2023-12-03 20:21+0100\n"
|
||||||
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||||
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||||
|
"Language: \n"
|
||||||
|
"MIME-Version: 1.0\n"
|
||||||
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
|
"Content-Transfer-Encoding: 8bit\n"
|
||||||
|
|
||||||
|
#: invidious.php:35
|
||||||
|
#, php-format
|
||||||
|
msgid ""
|
||||||
|
"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."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: invidious.php:36
|
||||||
|
msgid "Invidious server"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: invidious.php:37
|
||||||
|
msgid "Save Settings"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: invidious.php:64
|
||||||
|
#, php-format
|
||||||
|
msgid "(Invidious addon enabled: YouTube links via %s)"
|
||||||
|
msgstr ""
|
||||||
|
|
5
invidious/templates/admin.tpl
Normal file
5
invidious/templates/admin.tpl
Normal 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>
|
|
@ -20,4 +20,9 @@
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin-top: 25px;
|
margin-top: 25px;
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
|
/* The pageheader box */
|
||||||
|
padding: 20px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
border-radius: 2px;
|
||||||
|
margin-bottom: 15px;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue
You need to also check for
'https://youtube.com'
now that the replacement has been combined. Additionallystrpos()
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.