Merge pull request 'Initial commit of url_replace addon.' (#1453) from toddy/friendica-addons:develop into develop

Reviewed-on: https://git.friendi.ca/friendica/friendica-addons/pulls/1453
This commit is contained in:
Tobias Diekershoff 2024-01-05 09:56:46 +01:00
commit 82073db292
5 changed files with 220 additions and 0 deletions

21
url_replace/LICENSE.md Normal file
View file

@ -0,0 +1,21 @@
# MIT License
Copyright © 2024 Dr. Tobias Quathamer <t.quathamer@mailbox.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

17
url_replace/README.md Normal file
View file

@ -0,0 +1,17 @@
# URL replace
This addon will replace all occurrences of specified URLs with the address of
alternative servers in all displayed postings on a Friendica node.
You can use this to switch from Twitter (or X) to a nitter instance, from
YouTube to an invidious instance, or from some news sites to 12ft.io.
Note: If you are using the twitter connector on your server, the links to the
contacts profile pages will not be replaced by this addon. Only links in the
body of the postings are affected.
## Why
- Access a website without JavaScript enabled to prevent JavaScript analytics
and potential IP-based tracking
- Avoid seeing ads on YouTube videos

View file

@ -0,0 +1,50 @@
# ADDON url_replace
# Copyright (C)
# This file is distributed under the same license as the Friendica url_replace addon package.
#
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-01-05 00:06+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"
#: url_replace.php:54
msgid "Nitter server"
msgstr ""
#: url_replace.php:56
msgid "Specify the URL with protocol. The default is https://nitter.net."
msgstr ""
#: url_replace.php:62
msgid "Invidious server"
msgstr ""
#: url_replace.php:64
msgid "Specify the URL with protocol. The default is https://yewtu.be."
msgstr ""
#: url_replace.php:70
msgid "Sites which are accessed through 12ft.io"
msgstr ""
#: url_replace.php:72
msgid "Specify the URLs with protocol, one per line."
msgstr ""
#: url_replace.php:76
msgid "Save settings"
msgstr ""
#: url_replace.php:125
msgid "(URL replace addon enabled for X, YouTube and some news sites.)"
msgstr ""

View file

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

127
url_replace/url_replace.php Normal file
View file

@ -0,0 +1,127 @@
<?php
/**
* Name: URL Replace
* Description: Replaces occurrences of specified URLs with the address of alternative servers in all displays of postings on a node.
* Version: 1.0
* Author: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
* Maintainer: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
*/
use Friendica\Core\Hook;
use Friendica\Core\Renderer;
use Friendica\DI;
function url_replace_install()
{
Hook::register('prepare_body_final', 'addon/url_replace/url_replace.php', 'url_replace_render');
}
/**
* Handle sent data from admin settings
*/
function url_replace_addon_admin_post()
{
DI::config()->set('url_replace', 'nitter_server', rtrim(trim($_POST['nitter_server']), '/'));
DI::config()->set('url_replace', 'invidious_server', rtrim(trim($_POST['invidious_server']), '/'));
// Convert twelvefeet_sites into an array before setting the new value
$twelvefeet_sites = explode(PHP_EOL, $_POST['twelvefeet_sites']);
// Normalize URLs by using lower case, removing a trailing slash and whitespace
$twelvefeet_sites = array_map(fn ($value): string => rtrim(trim(strtolower($value)), '/'), $twelvefeet_sites);
// Do not store empty lines or duplicates
$twelvefeet_sites = array_filter($twelvefeet_sites, fn ($value): bool => !empty($value));
$twelvefeet_sites = array_unique($twelvefeet_sites);
// Ensure a protocol and default to HTTPS
$twelvefeet_sites = array_map(
fn ($value): string => substr($value, 0, 4) !== 'http' ? 'https://'.$value : $value,
$twelvefeet_sites
);
asort($twelvefeet_sites);
DI::config()->set('url_replace', 'twelvefeet_sites', $twelvefeet_sites);
}
/**
* Hook into admin settings to enable choosing a different server
* for twitter, youtube, and news sites.
*/
function url_replace_addon_admin(string &$o)
{
$nitter_server = DI::config()->get('url_replace', 'nitter_server');
$invidious_server = DI::config()->get('url_replace', 'invidious_server');
$twelvefeet_sites = implode(PHP_EOL, DI::config()->get('url_replace', 'twelvefeet_sites'));
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/url_replace/');
$o = Renderer::replaceMacros($t, [
'$nitter_server' => [
'nitter_server',
DI::l10n()->t('Nitter server'),
$nitter_server,
DI::l10n()->t('Specify the URL with protocol. The default is https://nitter.net.'),
null,
'placeholder="https://nitter.net"',
],
'$invidious_server' => [
'invidious_server',
DI::l10n()->t('Invidious server'),
$invidious_server,
DI::l10n()->t('Specify the URL with protocol. The default is https://yewtu.be.'),
null,
'placeholder="https://yewtu.be"',
],
'$twelvefeet_sites' => [
'twelvefeet_sites',
DI::l10n()->t('Sites which are accessed through 12ft.io'),
$twelvefeet_sites,
DI::l10n()->t('Specify the URLs with protocol, one per line.'),
null,
'rows=6'
],
'$submit' => DI::l10n()->t('Save settings'),
]);
}
/**
* Replace proprietary URLs with their specified counterpart
*/
function url_replace_render(array &$b)
{
$replaced = false;
$nitter_server = DI::config()->get('url_replace', 'nitter_server');
if (empty($nitter_server)) {
$nitter_server = 'https://nitter.net';
}
$invidious_server = DI::config()->get('url_replace', 'invidious_server');
if (empty($invidious_server)) {
$invidious_server = 'https://yewtu.be';
}
// Handle some of twitter and youtube
$replacements = [
'https://mobile.twitter.com' => $nitter_server,
'https://twitter.com' => $nitter_server,
'https://mobile.x.com' => $nitter_server,
'https://x.com' => $nitter_server,
'https://www.youtube.com' => $invidious_server,
'https://youtube.com' => $invidious_server,
'https://m.youtube.com' => $invidious_server,
'https://youtu.be' => $invidious_server,
];
foreach ($replacements as $server => $replacement) {
if (strpos($b['html'], $server) !== false) {
$b['html'] = str_replace($server, $replacement, $b['html']);
$replaced = true;
}
}
$twelvefeet_sites = DI::config()->get('url_replace', 'twelvefeet_sites');
foreach ($twelvefeet_sites as $twelvefeet_site) {
if (strpos($b['html'], $twelvefeet_site) !== false) {
$b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/'.$twelvefeet_site, $b['html']);
$replaced = true;
}
}
if ($replaced) {
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube and some news sites.)') . '</small></p>';
}
}