diff --git a/js/privatebin.js b/js/privatebin.js index 342f6bb0..5d3fef8c 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -4336,9 +4336,27 @@ jQuery.PrivateBin = (function($, RawDeflate) { const Memory = (function (document, window) { const me = {}; - let urls = [], + let pastes = [], db; + /** + * checks if the given URL was already memorized + * + * @name Memory.isInMemory + * @private + * @function + * @param {string} pasteUrl + * @return {bool} + */ + function isInMemory(pasteUrl) + { + return pastes.filter( + function(memorizedPaste) { + return memorizedPaste.url === pasteUrl; + } + ).length > 0; + } + /** * called after successfully connecting to the indexedDB * @@ -4352,10 +4370,13 @@ jQuery.PrivateBin = (function($, RawDeflate) { memory.openCursor().onsuccess = function(e) { let cursor = e.target.result; if (cursor) { - urls.push(cursor.value.url); + pastes.push(cursor.value); cursor.continue(); } else { me.refreshList(); + if (isInMemory(window.location.href)) { + $('#rememberbutton').addClass('disabled'); + } } }; } @@ -4370,12 +4391,8 @@ jQuery.PrivateBin = (function($, RawDeflate) { */ me.add = function(pasteUrl) { - if (!window.indexedDB || !db) { - return false; - } const url = new URL(pasteUrl); - const memory = db.transaction('pastes', 'readwrite').objectStore('pastes'); - const request = memory.add({ + const newPaste = { 'https': url.protocol == 'https:', 'service': url.hostname + url.pathname, 'pasteid': url.search.replace(/^\?+/, ''), @@ -4383,11 +4400,17 @@ jQuery.PrivateBin = (function($, RawDeflate) { // we store the full URL as it may contain additonal information // required to open the paste, like port, username and password 'url': pasteUrl - }); - request.onsuccess = function(e) { - urls.push(pasteUrl); - me.refreshList(); }; + // don't add an already memorized paste + if (isInMemory(pasteUrl)) { + return false; + } + pastes.push(newPaste); + if (!window.indexedDB || !db) { + return false; + } + const memory = db.transaction('pastes', 'readwrite').objectStore('pastes'); + const request = memory.add(newPaste); return true; }; @@ -4400,11 +4423,14 @@ jQuery.PrivateBin = (function($, RawDeflate) { me.refreshList = function() { const $tbody = $('#sidebar-wrapper table tbody')[0]; + if (!$tbody) { + return; + } $tbody.textContent = ''; - urls.forEach(function(url) { + pastes.forEach(function(paste) { const row = document.createElement('tr'), cell = document.createElement('td'); - cell.textContent = url; + cell.textContent = paste.url; row.appendChild(cell); $tbody.appendChild(row); }); @@ -4420,7 +4446,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { */ me.init = function() { - urls = []; + pastes = []; if (!window.indexedDB) { $('#menu-toggle').hide(); return; @@ -4455,8 +4481,8 @@ jQuery.PrivateBin = (function($, RawDeflate) { }); $('#rememberbutton').on('click', function(e) { - me.add(window.location.href); - $('#menu-toggle').click(); + if (me.add(window.location.href)) + $('#menu-toggle').click(); }); }; diff --git a/js/test/Memory.js b/js/test/Memory.js index 173dcd2a..deee173c 100644 --- a/js/test/Memory.js +++ b/js/test/Memory.js @@ -2,7 +2,7 @@ const common = require('../common'); describe('Memory', function () { - describe('add', function () { + describe('add & refreshList', function () { this.timeout(30000); jsc.property( @@ -24,8 +24,10 @@ describe('Memory', function () { // clear cache, then the first cell will match what we add $.PrivateBin.Memory.init(); $.PrivateBin.Memory.add(expected); + $.PrivateBin.Memory.refreshList(); + const result = $('#sidebar-wrapper table tbody tr td')[0].textContent; clean(); - return true; + return result === expected; } ); }); @@ -35,8 +37,8 @@ describe('Memory', function () { 'enables toggling the memory sidebar', function() { $('body').html( - '
' + '
' + + '
' ); assert.ok(!$('main').hasClass('toggled')); diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index eb3fb019..8db79fef 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -72,7 +72,7 @@ endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index 2bf1e577..5aa7abc0 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -50,7 +50,7 @@ endif; ?> - +