ensure code can be tested, handle async indexedDB, don't let user add same URL more then once
This commit is contained in:
parent
af02731002
commit
ea93c474ea
4 changed files with 50 additions and 22 deletions
|
@ -4336,9 +4336,27 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
const Memory = (function (document, window) {
|
const Memory = (function (document, window) {
|
||||||
const me = {};
|
const me = {};
|
||||||
|
|
||||||
let urls = [],
|
let pastes = [],
|
||||||
db;
|
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
|
* called after successfully connecting to the indexedDB
|
||||||
*
|
*
|
||||||
|
@ -4352,10 +4370,13 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
memory.openCursor().onsuccess = function(e) {
|
memory.openCursor().onsuccess = function(e) {
|
||||||
let cursor = e.target.result;
|
let cursor = e.target.result;
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
urls.push(cursor.value.url);
|
pastes.push(cursor.value);
|
||||||
cursor.continue();
|
cursor.continue();
|
||||||
} else {
|
} else {
|
||||||
me.refreshList();
|
me.refreshList();
|
||||||
|
if (isInMemory(window.location.href)) {
|
||||||
|
$('#rememberbutton').addClass('disabled');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -4370,12 +4391,8 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
*/
|
*/
|
||||||
me.add = function(pasteUrl)
|
me.add = function(pasteUrl)
|
||||||
{
|
{
|
||||||
if (!window.indexedDB || !db) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const url = new URL(pasteUrl);
|
const url = new URL(pasteUrl);
|
||||||
const memory = db.transaction('pastes', 'readwrite').objectStore('pastes');
|
const newPaste = {
|
||||||
const request = memory.add({
|
|
||||||
'https': url.protocol == 'https:',
|
'https': url.protocol == 'https:',
|
||||||
'service': url.hostname + url.pathname,
|
'service': url.hostname + url.pathname,
|
||||||
'pasteid': url.search.replace(/^\?+/, ''),
|
'pasteid': url.search.replace(/^\?+/, ''),
|
||||||
|
@ -4383,11 +4400,17 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
// we store the full URL as it may contain additonal information
|
// we store the full URL as it may contain additonal information
|
||||||
// required to open the paste, like port, username and password
|
// required to open the paste, like port, username and password
|
||||||
'url': pasteUrl
|
'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;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4400,11 +4423,14 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
me.refreshList = function()
|
me.refreshList = function()
|
||||||
{
|
{
|
||||||
const $tbody = $('#sidebar-wrapper table tbody')[0];
|
const $tbody = $('#sidebar-wrapper table tbody')[0];
|
||||||
|
if (!$tbody) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$tbody.textContent = '';
|
$tbody.textContent = '';
|
||||||
urls.forEach(function(url) {
|
pastes.forEach(function(paste) {
|
||||||
const row = document.createElement('tr'),
|
const row = document.createElement('tr'),
|
||||||
cell = document.createElement('td');
|
cell = document.createElement('td');
|
||||||
cell.textContent = url;
|
cell.textContent = paste.url;
|
||||||
row.appendChild(cell);
|
row.appendChild(cell);
|
||||||
$tbody.appendChild(row);
|
$tbody.appendChild(row);
|
||||||
});
|
});
|
||||||
|
@ -4420,7 +4446,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
*/
|
*/
|
||||||
me.init = function()
|
me.init = function()
|
||||||
{
|
{
|
||||||
urls = [];
|
pastes = [];
|
||||||
if (!window.indexedDB) {
|
if (!window.indexedDB) {
|
||||||
$('#menu-toggle').hide();
|
$('#menu-toggle').hide();
|
||||||
return;
|
return;
|
||||||
|
@ -4455,8 +4481,8 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#rememberbutton').on('click', function(e) {
|
$('#rememberbutton').on('click', function(e) {
|
||||||
me.add(window.location.href);
|
if (me.add(window.location.href))
|
||||||
$('#menu-toggle').click();
|
$('#menu-toggle').click();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
|
|
||||||
describe('Memory', function () {
|
describe('Memory', function () {
|
||||||
describe('add', function () {
|
describe('add & refreshList', function () {
|
||||||
this.timeout(30000);
|
this.timeout(30000);
|
||||||
|
|
||||||
jsc.property(
|
jsc.property(
|
||||||
|
@ -24,8 +24,10 @@ describe('Memory', function () {
|
||||||
// clear cache, then the first cell will match what we add
|
// clear cache, then the first cell will match what we add
|
||||||
$.PrivateBin.Memory.init();
|
$.PrivateBin.Memory.init();
|
||||||
$.PrivateBin.Memory.add(expected);
|
$.PrivateBin.Memory.add(expected);
|
||||||
|
$.PrivateBin.Memory.refreshList();
|
||||||
|
const result = $('#sidebar-wrapper table tbody tr td')[0].textContent;
|
||||||
clean();
|
clean();
|
||||||
return true;
|
return result === expected;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -35,8 +37,8 @@ describe('Memory', function () {
|
||||||
'enables toggling the memory sidebar',
|
'enables toggling the memory sidebar',
|
||||||
function() {
|
function() {
|
||||||
$('body').html(
|
$('body').html(
|
||||||
'<main><div id="sidebar-wrapper"><table><tbody></tbody>' +
|
'<main><div id="sidebar-wrapper"></div>' +
|
||||||
'</table></div><button id="menu-toggle"></button></main>'
|
'<button id="menu-toggle"></button></main>'
|
||||||
);
|
);
|
||||||
assert.ok(!$('main').hasClass('toggled'));
|
assert.ok(!$('main').hasClass('toggled'));
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-pydUuue2tfUH/5qy4OcIQtlWnWthLBNh4AqXjDlWoEoanFG10LUDEIdr6H7MIrW1RfxJtReq8qoJRTSRRCo9vg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-+gTE0A/WPe61SJOCwyY3Xbo7Hyfs8fhnYn4OLiqBJpW6qnWVa/o0ycwwZ2iH6fM8XgpF4fFExMswE45cyLS/AA==" crossorigin="anonymous"></script>
|
||||||
<!-- icon -->
|
<!-- icon -->
|
||||||
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
||||||
|
|
|
@ -50,7 +50,7 @@ endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-pydUuue2tfUH/5qy4OcIQtlWnWthLBNh4AqXjDlWoEoanFG10LUDEIdr6H7MIrW1RfxJtReq8qoJRTSRRCo9vg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-+gTE0A/WPe61SJOCwyY3Xbo7Hyfs8fhnYn4OLiqBJpW6qnWVa/o0ycwwZ2iH6fM8XgpF4fFExMswE45cyLS/AA==" crossorigin="anonymous"></script>
|
||||||
<!-- icon -->
|
<!-- icon -->
|
||||||
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
||||||
|
|
Loading…
Reference in a new issue