From a26e90b2024ca3dbdccfd44ae5a33e11aafa7ae2 Mon Sep 17 00:00:00 2001 From: "Dr. Tobias Quathamer" Date: Fri, 5 Jan 2024 00:12:20 +0100 Subject: [PATCH] Initial commit of url_replace addon. This closes https://github.com/friendica/friendica/issues/13220 --- url_replace/LICENSE.md | 21 ++++++ url_replace/README.md | 17 +++++ url_replace/lang/C/messages.po | 50 +++++++++++++ url_replace/templates/admin.tpl | 5 ++ url_replace/url_replace.php | 127 ++++++++++++++++++++++++++++++++ 5 files changed, 220 insertions(+) create mode 100644 url_replace/LICENSE.md create mode 100644 url_replace/README.md create mode 100644 url_replace/lang/C/messages.po create mode 100644 url_replace/templates/admin.tpl create mode 100644 url_replace/url_replace.php diff --git a/url_replace/LICENSE.md b/url_replace/LICENSE.md new file mode 100644 index 00000000..09dc8f64 --- /dev/null +++ b/url_replace/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright © 2024 Dr. Tobias Quathamer + +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. diff --git a/url_replace/README.md b/url_replace/README.md new file mode 100644 index 00000000..278de91b --- /dev/null +++ b/url_replace/README.md @@ -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 diff --git a/url_replace/lang/C/messages.po b/url_replace/lang/C/messages.po new file mode 100644 index 00000000..c09ad6d9 --- /dev/null +++ b/url_replace/lang/C/messages.po @@ -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 \n" +"Language-Team: LANGUAGE \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 "" diff --git a/url_replace/templates/admin.tpl b/url_replace/templates/admin.tpl new file mode 100644 index 00000000..b99e6355 --- /dev/null +++ b/url_replace/templates/admin.tpl @@ -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}} + +
diff --git a/url_replace/url_replace.php b/url_replace/url_replace.php new file mode 100644 index 00000000..51bc10fe --- /dev/null +++ b/url_replace/url_replace.php @@ -0,0 +1,127 @@ + + * Maintainer: Dr. Tobias Quathamer + */ +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'] .= '

' . DI::l10n()->t('(URL replace addon enabled for X, YouTube and some news sites.)') . '

'; + } +}