2017-08-03 12:32:30 -04:00
|
|
|
securePassword = null
|
|
|
|
|
|
|
|
createSecret = () ->
|
|
|
|
secret = $('#formCreateSecret').find('textarea').val()
|
|
|
|
|
2017-08-04 09:17:30 -04:00
|
|
|
securePassword = Math.random().toString(36).substring(2)
|
|
|
|
secret = GibberishAES.enc(secret, securePassword)
|
2017-08-03 12:32:30 -04:00
|
|
|
|
|
|
|
$.ajax 'api/create',
|
2018-08-22 14:21:39 -04:00
|
|
|
method: "post"
|
2017-08-03 12:32:30 -04:00
|
|
|
data:
|
|
|
|
secret: secret
|
|
|
|
dataType: "json"
|
|
|
|
statusCode:
|
|
|
|
201: secretCreated
|
|
|
|
400: somethingWrong
|
|
|
|
500: somethingWrong
|
|
|
|
404: () ->
|
|
|
|
# Mock for interface testing
|
|
|
|
secretCreated
|
|
|
|
secret_id: 'foobar'
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
dataNotFound = () ->
|
|
|
|
$('#notfound').show()
|
|
|
|
|
|
|
|
hashLoad = () ->
|
|
|
|
hash = window.location.hash
|
|
|
|
if hash.length == 0
|
|
|
|
return
|
|
|
|
|
2018-05-05 11:34:13 -04:00
|
|
|
$('#cardNewSecret').hide()
|
|
|
|
$('#cardSecretURL').hide()
|
2017-08-19 09:49:15 -04:00
|
|
|
$('#notfound').hide()
|
|
|
|
$('#somethingwrong').hide()
|
2018-05-05 11:34:13 -04:00
|
|
|
$('#cardReadSecretPre').show()
|
2017-08-19 09:49:15 -04:00
|
|
|
|
|
|
|
requestSecret = () ->
|
|
|
|
hash = window.location.hash
|
2017-08-03 15:43:37 -04:00
|
|
|
hash = decodeURIComponent(hash)
|
|
|
|
|
2017-08-03 12:32:30 -04:00
|
|
|
parts = hash.split '|'
|
|
|
|
if parts.length == 2
|
|
|
|
hash = parts[0]
|
|
|
|
securePassword = parts[1]
|
|
|
|
|
|
|
|
id = hash.substring(1)
|
|
|
|
$.ajax "api/get/#{id}",
|
|
|
|
dataType: "json"
|
|
|
|
statusCode:
|
|
|
|
404: dataNotFound
|
|
|
|
200: showData
|
|
|
|
|
|
|
|
initBinds = () ->
|
|
|
|
$('#formCreateSecret').bind 'submit', createSecret
|
2017-08-03 14:01:22 -04:00
|
|
|
$('#newSecret, .navbar-brand').bind 'click', newSecret
|
2017-08-03 13:44:13 -04:00
|
|
|
$(window).bind 'hashchange', hashLoad
|
2017-08-19 09:49:15 -04:00
|
|
|
$('#revealSecret').bind 'click', requestSecret
|
2018-08-22 15:08:48 -04:00
|
|
|
bindResizeTextarea()
|
2017-08-03 12:32:30 -04:00
|
|
|
|
|
|
|
newSecret = () ->
|
|
|
|
location.href = location.href.split('#')[0]
|
|
|
|
false
|
|
|
|
|
2018-08-22 15:08:48 -04:00
|
|
|
bindResizeTextarea = () ->
|
|
|
|
$('textarea').each(() ->
|
|
|
|
text = this
|
|
|
|
|
|
|
|
doResize = () =>
|
|
|
|
#text.style.height = 'auto'
|
|
|
|
text.style.height = (this.scrollHeight) + 'px'
|
|
|
|
|
|
|
|
delayedResize = () =>
|
|
|
|
window.setTimeout doResize, 0
|
|
|
|
|
|
|
|
text.setAttribute('style', "height: #{this.scrollHeight}px; min-height: #{this.scrollHeight}px; overflow-y:hidden;")
|
|
|
|
|
|
|
|
$(text)
|
|
|
|
.on('change', doResize)
|
|
|
|
.on('cut', delayedResize)
|
|
|
|
.on('paste', delayedResize)
|
|
|
|
.on('drop', delayedResize)
|
|
|
|
.on('keydown', delayedResize)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-08-03 12:32:30 -04:00
|
|
|
secretCreated = (data) ->
|
|
|
|
secretHash = data.secret_id
|
|
|
|
if securePassword != null
|
|
|
|
secretHash = "#{secretHash}|#{securePassword}"
|
|
|
|
url = "#{location.href.split('#')[0]}##{secretHash}"
|
|
|
|
|
2018-05-05 11:34:13 -04:00
|
|
|
$('#cardNewSecret').hide()
|
|
|
|
$('#cardReadSecretPre').hide()
|
|
|
|
$('#cardSecretURL').show()
|
|
|
|
$('#cardSecretURL').find('input').val url
|
|
|
|
$('#cardSecretURL').find('input').focus()
|
|
|
|
$('#cardSecretURL').find('input').select()
|
2017-08-03 12:32:30 -04:00
|
|
|
|
2017-08-03 14:01:22 -04:00
|
|
|
securePassword = null
|
|
|
|
|
2017-08-03 12:32:30 -04:00
|
|
|
showData = (data) ->
|
|
|
|
secret = data.secret
|
|
|
|
if securePassword != null
|
|
|
|
secret = GibberishAES.dec(secret, securePassword)
|
|
|
|
|
2018-05-05 11:34:13 -04:00
|
|
|
$('#cardNewSecret').hide()
|
|
|
|
$('#cardSecretURL').hide()
|
2017-08-03 14:01:22 -04:00
|
|
|
$('#notfound').hide()
|
|
|
|
$('#somethingwrong').hide()
|
2018-05-05 11:34:13 -04:00
|
|
|
$('#cardReadSecretPre').hide()
|
|
|
|
$('#cardReadSecret').show()
|
|
|
|
$('#cardReadSecret').find('textarea').val secret
|
2018-08-22 15:08:48 -04:00
|
|
|
$('#cardReadSecret').find('textarea').trigger 'change'
|
2017-08-03 12:32:30 -04:00
|
|
|
|
|
|
|
somethingWrong = () ->
|
|
|
|
$('#somethingwrong').show()
|
|
|
|
|
|
|
|
|
|
|
|
$ ->
|
|
|
|
initBinds()
|
|
|
|
hashLoad()
|