cryptpad/www/common/cryptpad-common.js
2016-06-23 16:44:20 +02:00

63 lines
1.9 KiB
JavaScript

define([
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/jquery/dist/jquery.min.js',
], function (Crypto) {
var $ = window.jQuery;
var common = {};
var getSecrets = common.getSecrets = function () {
var secret = {};
if (!/#/.test(window.location.href)) {
secret.key = Crypto.genKey();
} else {
var hash = window.location.hash.slice(1);
secret.channel = hash.slice(0, 32);
secret.key = hash.slice(32);
}
return secret;
};
var rememberPad = common.rememberPad = window.rememberPad = function () {
// bail out early
if (!/#/.test(window.location.hash)) { return; }
var storageKey = 'CryptPad_RECENTPADS';
var recentPadsStr = localStorage[storageKey];
var recentPads = [];
if (recentPadsStr) {
try {
recentPads = JSON.parse(recentPadsStr);
} catch (err) {
// couldn't parse the localStorage?
// just overwrite it.
}
}
var now = new Date();
var timeframe = 1000 * 60 * 60 * 24 * 30;
var out = recentPads.filter(function (pad) {
return (pad && pad[0] !== window.location.href &&
(now.getTime() - new Date(pad[1]).getTime()) < timeframe);
});
out.push([window.location.href, now]);
localStorage[storageKey] = JSON.stringify(out);
};
var importContent = common.importContent = function (type, f) {
return function () {
var $files = $('<input type="file">').click();
$files.on('change', function (e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function (e) { f(e.target.result, file); };
reader.readAsText(file, type);
});
};
};
return common;
});