From cd5ede5670985e6985d01d6cbce87b85f4c130da Mon Sep 17 00:00:00 2001 From: Haocen Xu Date: Tue, 13 Aug 2019 21:44:53 -0400 Subject: [PATCH 1/4] Enhance URL shortener integration, clear address bar when create new paste from existing paste --- js/privatebin.js | 57 ++++++++++++++++++++++++++++++++++++++++++++--- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index 1669d7b0..b8962e48 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -1711,8 +1711,58 @@ jQuery.PrivateBin = (function($, RawDeflate) { */ function sendToShortener() { - window.location.href = $shortenButton.data('shortener') + - encodeURIComponent($pasteUrl.attr('href')); + if ($shortenButton.hasClass('buttondisabled')) { + return; + } + $.ajax({ + type: 'GET', + url: `${$shortenButton.data('shortener')}${encodeURIComponent($pasteUrl.attr('href'))}`, + headers: {'Accept': 'text/html, application/xhtml+xml, application/xml, application/json'}, + processData: false, + timeout: 10000, + xhrFields: { + withCredentials: false + }, + success: function(response) { + let responseString = response; + if (typeof responseString === 'object') { + responseString = JSON.stringify(responseString); + } + if (typeof responseString === 'string' && responseString.length > 0) { + const shortUrlMatcher = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; + const shortUrl = responseString.match(shortUrlMatcher).sort(function(a, b) { + return a.length - b.length; + })[0]; + if (typeof shortUrl === 'string' && shortUrl.length > 0) { + $('#pastelink').html( + I18n._( + 'Your paste is %s (Hit [Ctrl]+[c] to copy)', + shortUrl, shortUrl + ) + ); + // we disable the button to avoid calling shortener again + $shortenButton.addClass('buttondisabled'); + // save newly created element + $pasteUrl = $('#pasteurl'); + // we pre-select the link so that the user only has to [Ctrl]+[c] the link + Helper.selectText($pasteUrl[0]); + return; + } + } + Alert.showError( + I18n._('Cannot parse response from URL shortener.') + ); + } + }) + .fail(function(data, textStatus, errorThrown) { + console.error(textStatus, errorThrown); + // we don't know why it failed, could be CORS of the external server not setup properly, in which case we follow old behavior to open it in new tab + window.open( + `${$shortenButton.data('shortener')}${encodeURIComponent($pasteUrl.attr('href'))}`, + '_blank', + 'noopener, noreferrer' + ) + }); } /** @@ -1754,7 +1804,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { // and add click event $pasteUrl.click(pasteLinkClick); - // shorten button + // delete link $('#deletelink').html('' + I18n._('Delete data') + ''); // show result @@ -4688,6 +4738,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { TopNav.showCreateButtons(); Alert.hideLoading(); + history.pushState({type: 'create'}, document.title, Helper.baseUri()); }; /** diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 924b5a35..76fe9f4b 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -71,7 +71,7 @@ if ($MARKDOWN): endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index c330720b..8113d49c 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -49,7 +49,7 @@ if ($MARKDOWN): endif; ?> - + From a5cd8696e6f7be59aa56816220bb96816e04830f Mon Sep 17 00:00:00 2001 From: Haocen Xu Date: Tue, 13 Aug 2019 21:55:35 -0400 Subject: [PATCH 2/4] Ensure shortener button re-enable after creating new paste --- js/privatebin.js | 3 +++ tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index b8962e48..d9207059 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -4108,6 +4108,9 @@ jQuery.PrivateBin = (function($, RawDeflate) { deleteUrl = baseUri + 'pasteid=' + data.id + '&deletetoken=' + data.deletetoken; PasteStatus.createPasteNotification(url, deleteUrl); + // enable shortener button + $shortenButton.removeClass('buttondisabled'); + // show new URL in browser bar history.pushState({type: 'newpaste'}, document.title, url); diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 76fe9f4b..f22a87f7 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -71,7 +71,7 @@ if ($MARKDOWN): endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index 8113d49c..f048899f 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -49,7 +49,7 @@ if ($MARKDOWN): endif; ?> - + From 1af7b536a58e5588ae770427e82358633f62ac03 Mon Sep 17 00:00:00 2001 From: Haocen Xu Date: Wed, 14 Aug 2019 00:02:25 -0400 Subject: [PATCH 3/4] Fix an edge case where we cannot find any shortUrl --- js/privatebin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/privatebin.js b/js/privatebin.js index d9207059..b8e63ddc 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -1730,7 +1730,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { } if (typeof responseString === 'string' && responseString.length > 0) { const shortUrlMatcher = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; - const shortUrl = responseString.match(shortUrlMatcher).sort(function(a, b) { + const shortUrl = (responseString.match(shortUrlMatcher) || []).sort(function(a, b) { return a.length - b.length; })[0]; if (typeof shortUrl === 'string' && shortUrl.length > 0) { From 71931b0f1848fca44843ba5283d04d85f03584e0 Mon Sep 17 00:00:00 2001 From: Haocen Xu Date: Wed, 14 Aug 2019 20:36:44 -0400 Subject: [PATCH 4/4] Clear discussion if new/clone paste clicked --- js/privatebin.js | 12 +++++++++--- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index b8e63ddc..72b73403 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -1807,6 +1807,9 @@ jQuery.PrivateBin = (function($, RawDeflate) { // delete link $('#deletelink').html('' + I18n._('Delete data') + ''); + // enable shortener button + $shortenButton.removeClass('buttondisabled'); + // show result $pasteSuccess.removeClass('hidden'); // we pre-select the link so that the user only has to [Ctrl]+[c] the link @@ -4108,9 +4111,6 @@ jQuery.PrivateBin = (function($, RawDeflate) { deleteUrl = baseUri + 'pasteid=' + data.id + '&deletetoken=' + data.deletetoken; PasteStatus.createPasteNotification(url, deleteUrl); - // enable shortener button - $shortenButton.removeClass('buttondisabled'); - // show new URL in browser bar history.pushState({type: 'newpaste'}, document.title, url); @@ -4742,6 +4742,9 @@ jQuery.PrivateBin = (function($, RawDeflate) { TopNav.showCreateButtons(); Alert.hideLoading(); history.pushState({type: 'create'}, document.title, Helper.baseUri()); + + // clear discussion + DiscussionViewer.prepareNewDiscussion(); }; /** @@ -4854,6 +4857,9 @@ jQuery.PrivateBin = (function($, RawDeflate) { Editor.show(); TopNav.showCreateButtons(); + + // clear discussion + DiscussionViewer.prepareNewDiscussion(); }; /** diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index f22a87f7..bccda1ad 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -71,7 +71,7 @@ if ($MARKDOWN): endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index f048899f..49bcc107 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -49,7 +49,7 @@ if ($MARKDOWN): endif; ?> - +