Auto Shortening URLs

This commit is contained in:
Karthik Kasturi 2025-08-09 00:01:25 +01:00
parent 08c44cfb05
commit 059e64adb1
7 changed files with 43 additions and 2 deletions

View file

@ -84,6 +84,11 @@ languageselection = false
; the documents encryption key.
; urlshortener = "https://shortener.example.com/api?link="
; (optional) Whether to shorten the URL by default when a new document is created.
; If set to true, the "Shorten URL" functionality will be automatically called.
; This only works if the "urlshortener" option is set.
; shortenbydefault = false
; (optional) Let users create a QR code for sharing the document URL with one click.
; It works both when a new document is created and when you view a document.
; qrcode = true

View file

@ -210,6 +210,7 @@
"Encrypted note on %s": "Encrypted note on %s",
"Visit this link to see the note. Giving the URL to anyone allows them to access the note, too.": "Visit this link to see the note. Giving the URL to anyone allows them to access the note, too.",
"URL shortener may expose your decrypt key in URL.": "URL shortener may expose your decrypt key in URL.",
"URL shortener is enabled by default.": "URL shortener is enabled by default.",
"Save document": "Save document",
"Your IP is not authorized to create documents.": "Your IP is not authorized to create documents.",
"Trying to shorten a URL that isn't pointing at our instance.": "Trying to shorten a URL that isn't pointing at our instance.",

View file

@ -2120,6 +2120,21 @@ jQuery.PrivateBin = (function($) {
Helper.selectText($pasteUrl[0]);
};
/**
* Checks if auto-shortening is enabled and sends the link to the shortener if it is.
*
* @name PasteStatus.checkAutoShorten
* @function
*/
me.checkAutoShorten = function() {
// check if auto-shortening is enabled
if ($shortenButton.data('autoshorten') === true) {
// if so, we send the link to the shortener
// we do not remove the button, in case shortener fails
sendToShortener();
}
}
/**
* extracts URLs from given string
*
@ -5030,6 +5045,8 @@ jQuery.PrivateBin = (function($) {
TopNav.hideDownloadButton();
Editor.hide();
PasteStatus.checkAutoShorten();
// parse and show text
// (preparation already done in me.sendPaste())
PasteViewer.run();

View file

@ -61,6 +61,7 @@ class Configuration
'languageselection' => false,
'languagedefault' => '',
'urlshortener' => '',
'shortenbydefault' => false,
'qrcode' => true,
'email' => true,
'icon' => 'jdenticon',

View file

@ -477,6 +477,7 @@ class Controller
$page->assign('EXPIRE', $expire);
$page->assign('EXPIREDEFAULT', $this->_conf->getKey('default', 'expire'));
$page->assign('URLSHORTENER', $this->_conf->getKey('urlshortener'));
$page->assign('SHORTENBYDEFAULT', $this->_conf->getKey('shortenbydefault'));
$page->assign('QRCODE', $this->_conf->getKey('qrcode'));
$page->assign('EMAIL', $this->_conf->getKey('email'));
$page->assign('HTTPWARNING', $this->_conf->getKey('httpwarning'));

View file

@ -565,12 +565,20 @@ endif;
if (!empty($URLSHORTENER)) :
?>
<p>
<button id="shortenbutton" data-shortener="<?php echo I18n::encode($URLSHORTENER); ?>" type="button" class="btn btn-<?php echo $isDark ? 'warning' : 'primary'; ?> btn-block">
<button id="shortenbutton" data-shortener="<?php echo I18n::encode($URLSHORTENER); ?>"
<?php if ($SHORTENBYDEFAULT) : ?>
data-autoshorten="true"
<?php endif; ?>
type="button" class="btn btn-<?php echo $isDark ? 'warning' : 'primary'; ?> btn-block"
>
<span class="glyphicon glyphicon-send" aria-hidden="true"></span> <?php echo I18n::_('Shorten URL'), PHP_EOL; ?>
</button>
</p>
<div role="alert" class="alert alert-danger">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<?php if ($SHORTENBYDEFAULT) : ?>
<?php echo I18n::_('URL shortener is enabled by default.'), PHP_EOL; ?>
<?php endif; ?>
<?php echo I18n::_('URL shortener may expose your decrypt key in URL.'), PHP_EOL; ?>
</div>
<?php

View file

@ -432,12 +432,20 @@ endif;
if (!empty($URLSHORTENER)) :
?>
<p>
<button id="shortenbutton" data-shortener="<?php echo I18n::encode($URLSHORTENER); ?>" type="button" class="btn btn-primary btn-block d-flex justify-content-center align-items-center gap-1">
<button id="shortenbutton" data-shortener="<?php echo I18n::encode($URLSHORTENER); ?>"
<?php if ($SHORTENBYDEFAULT) : ?>
data-autoshorten="true"
<?php endif; ?>
type="button" class="btn btn-primary btn-block d-flex justify-content-center align-items-center gap-1"
>
<svg width="16" height="16" fill="currentColor" aria-hidden="true"><use href="img/bootstrap-icons.svg#send" /></svg> <?php echo I18n::_('Shorten URL'), PHP_EOL; ?>
</button>
</p>
<div role="alert" class="alert alert-danger">
<svg width="16" height="16" fill="currentColor" aria-hidden="true"><use href="img/bootstrap-icons.svg#exclamation-circle" /></svg>
<?php if ($SHORTENBYDEFAULT) : ?>
<?php echo I18n::_('URL shortener is enabled by default.'), PHP_EOL; ?>
<?php endif; ?>
<?php echo I18n::_('URL shortener may expose your decrypt key in URL.'), PHP_EOL; ?>
</div>
<?php