diff --git a/js/privatebin.js b/js/privatebin.js index 4e765db3..0ae54eaf 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -311,11 +311,11 @@ jQuery.PrivateBin = (function($) { /** * cache for script location * - * @name Helper.baseUri + * @name Helper.windowLocationBaseUri * @private * @enum {string|null} */ - let baseUri = null; + let windowLocationBaseUri = null; /** * converts a duration (in seconds) into human friendly approximation @@ -505,19 +505,32 @@ jQuery.PrivateBin = (function($) { * get the current location (without search or hash part of the URL), * eg. https://example.com/path/?aaaa#bbbb --> https://example.com/path/ * - * @name Helper.baseUri + * @name Helper.windowLocationBaseUri * @function * @return {string} */ - me.baseUri = function() + me.windowLocationBaseUri = function() { // check for cached version - if (baseUri !== null) { - return baseUri; + if (windowLocationBaseUri !== null) { + return windowLocationBaseUri; } - baseUri = window.location.origin + window.location.pathname; - return baseUri; + windowLocationBaseUri = window.location.origin + window.location.pathname; + return windowLocationBaseUri; + }; + + /** + * get the basepath from config file, + * eg. https://privatebin.example.com/ + * + * @name Helper.configBaseUri + * @function + * @return {string} + */ + me.configBaseUri = function() + { + return configBaseUri || me.windowLocationBaseUri(); }; /** @@ -627,7 +640,7 @@ jQuery.PrivateBin = (function($) { */ me.reset = function() { - baseUri = null; + windowLocationBaseUri = null; }; /** @@ -1446,7 +1459,7 @@ jQuery.PrivateBin = (function($) { // reload data ServerInteraction.prepare(); - ServerInteraction.setUrl(Helper.baseUri() + '?pasteid=' + me.getPasteId()); + ServerInteraction.setUrl(Helper.windowLocationBaseUri() + '?pasteid=' + me.getPasteId()); ServerInteraction.setFailure(function (status, data) { // revert loading status… @@ -1624,7 +1637,7 @@ jQuery.PrivateBin = (function($) { */ function historyChange(event) { - let currentLocation = Helper.baseUri(); + let currentLocation = Helper.windowLocationBaseUri(); if (event.originalEvent.state === null && // no state object passed event.target.location.href === currentLocation && // target location is home page window.location.href === currentLocation // and we are not already on the home page @@ -1644,7 +1657,7 @@ jQuery.PrivateBin = (function($) { */ me.reloadHome = function() { - window.location.href = Helper.baseUri(); + window.location.href = Helper.windowLocationBaseUri(); }; /** @@ -1743,7 +1756,7 @@ jQuery.PrivateBin = (function($) { me.init = function() { // update link to home page - $('.reloadlink').prop('href', Helper.baseUri()); + $('.reloadlink').prop('href', Helper.windowLocationBaseUri()); $(window).on('popstate', historyChange); }; @@ -3925,7 +3938,7 @@ jQuery.PrivateBin = (function($) { {type: 'raw'}, document.title, // recreate document URL - Helper.baseUri() + '?' + Model.getPasteId() + '#' + + Helper.windowLocationBaseUri() + '?' + Model.getPasteId() + '#' + CryptTool.base58encode(Model.getPasteKey()) ); @@ -4924,7 +4937,7 @@ jQuery.PrivateBin = (function($) { // reset data successFunc = null; failureFunc = null; - url = Helper.baseUri(); + url = Helper.windowLocationBaseUri(); data = {}; }; @@ -5026,7 +5039,7 @@ jQuery.PrivateBin = (function($) { Alert.hideMessages(); // show notification - const baseUri = Helper.baseUri() + '?', + const baseUri = Helper.configBaseUri() + '?', url = baseUri + data.id + (TopNav.getBurnAfterReading() ? loadConfirmPrefix : '#') + CryptTool.base58encode(data.encryptionKey), deleteUrl = baseUri + 'pasteid=' + data.id + '&deletetoken=' + data.deletetoken; PasteStatus.createPasteNotification(url, deleteUrl); @@ -5729,8 +5742,8 @@ jQuery.PrivateBin = (function($) { Alert.hideLoading(); // only push new state if we are coming from a different one - if (Helper.baseUri() != window.location) { - history.pushState({type: 'create'}, document.title, Helper.baseUri()); + if (Helper.windowLocationBaseUri() != window.location) { + history.pushState({type: 'create'}, document.title, Helper.windowLocationBaseUri()); } // clear discussion @@ -5777,7 +5790,7 @@ jQuery.PrivateBin = (function($) { Model.getPasteData(function (data) { ServerInteraction.prepare(); - ServerInteraction.setUrl(Helper.baseUri() + '?pasteid=' + Model.getPasteId()); + ServerInteraction.setUrl(Helper.windowLocationBaseUri() + '?pasteid=' + Model.getPasteId()); ServerInteraction.setFailure(function (status, data) { // revert loading status… @@ -5819,7 +5832,7 @@ jQuery.PrivateBin = (function($) { me.hideStatusMessages(); // erase the id and the key in url - history.pushState({type: 'clone'}, document.title, Helper.baseUri()); + history.pushState({type: 'clone'}, document.title, Helper.windowLocationBaseUri()); if (AttachmentViewer.hasAttachment()) { const attachments = AttachmentViewer.getAttachments(); diff --git a/js/test/Helper.js b/js/test/Helper.js index eef2ac5e..f4ed9cd0 100644 --- a/js/test/Helper.js +++ b/js/test/Helper.js @@ -255,10 +255,10 @@ describe('Helper', function () { ); */ }); - describe('baseUri', function () { + describe('windowLocationBaseUri', function () { this.timeout(30000); jsc.property( - 'returns the URL without query & fragment', + 'returns the window location URL without query & fragment', common.jscSchemas(false), common.jscUrl(), function (schema, url) { @@ -269,7 +269,7 @@ describe('Helper', function () { $.PrivateBin.Helper.reset(); const expected = common.urlToString(url), clean = jsdom('', {url: fullUrl}), - result = $.PrivateBin.Helper.baseUri(); + result = $.PrivateBin.Helper.windowLocationBaseUri(); clean(); return expected === result; } diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 099b2558..970a7ff1 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -67,6 +67,7 @@ endif; ?> _scriptTag('js/purify-3.2.6.js', 'defer'); ?> _scriptTag('js/legacy.js', 'defer'); ?> + _scriptTag('js/privatebin.js', 'defer'); ?> diff --git a/tpl/bootstrap5.php b/tpl/bootstrap5.php index b3644ddd..97840221 100644 --- a/tpl/bootstrap5.php +++ b/tpl/bootstrap5.php @@ -51,6 +51,7 @@ endif; ?> _scriptTag('js/purify-3.2.6.js', 'defer'); ?> _scriptTag('js/legacy.js', 'defer'); ?> + _scriptTag('js/privatebin.js', 'defer'); ?>