Moved page.prepareShareX() to a single JS file named sharex.js. It'll be loaded in home and dashboard pages, and both home.js and dashboard.js will rely on the function provided by it.

The generated ShareX config file will also properly support cases where the safe is being hosted in a subdir now (e.i. https://example.com/lolisafe).
This commit is contained in:
Bobby Wibowo 2018-10-09 02:30:22 +07:00
parent 5bd343638a
commit 2f6c728e64
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
6 changed files with 30 additions and 51 deletions

View File

@ -114,7 +114,7 @@ page.prepareDashboard = function () {
page.getAlbumsSidebar()
page.prepareShareX()
if (typeof page.prepareShareX === 'function') { page.prepareShareX() }
}
page.logout = function () {
@ -1399,30 +1399,6 @@ page.setActiveMenu = function (activeItem) {
activeItem.classList.add('is-active')
}
page.prepareShareX = function () {
if (page.token) {
// TODO: "location.origin" is unsuitable if the safe is hosted in a subdir (e.i. http://example.com/safe)
var sharexElement = document.getElementById('ShareX')
var sharexFile =
'{\r\n' +
' "Name": "' + location.hostname + '",\r\n' +
' "DestinationType": "ImageUploader, FileUploader",\r\n' +
' "RequestType": "POST",\r\n' +
' "RequestURL": "' + location.origin + '/api/upload",\r\n' +
' "FileFormName": "files[]",\r\n' +
' "Headers": {\r\n' +
' "token": "' + page.token + '"\r\n' +
' },\r\n' +
' "ResponseType": "Text",\r\n' +
' "URL": "$json:files[0].url$",\r\n' +
' "ThumbnailURL": "$json:files[0].url$"\r\n' +
'}'
var sharexBlob = new Blob([sharexFile], { type: 'application/octet-binary' })
sharexElement.setAttribute('href', URL.createObjectURL(sharexBlob))
sharexElement.setAttribute('download', location.hostname + '.sxcu')
}
}
page.getPrettyDate = date => {
return date.getFullYear() + '-' +
(date.getMonth() < 9 ? '0' : '') + // month's index starts from zero

View File

@ -292,7 +292,7 @@ page.prepareDropzone = function () {
file.previewElement.querySelector('.error').innerHTML = error
})
page.prepareShareX()
if (typeof page.prepareShareX === 'function') { page.prepareShareX() }
}
page.uploadUrls = function (button) {
@ -391,30 +391,6 @@ page.updateTemplate = function (file, response) {
}
}
page.prepareShareX = function () {
if (page.token) {
// TODO: "location.origin" is unsuitable if the safe is hosted in a subdir (e.i. http://example.com/safe)
var sharexElement = document.getElementById('ShareX')
var sharexFile =
'{\r\n' +
' "Name": "' + location.hostname + '",\r\n' +
' "DestinationType": "ImageUploader, FileUploader",\r\n' +
' "RequestType": "POST",\r\n' +
' "RequestURL": "' + location.origin + '/api/upload",\r\n' +
' "FileFormName": "files[]",\r\n' +
' "Headers": {\r\n' +
' "token": "' + page.token + '"\r\n' +
' },\r\n' +
' "ResponseType": "Text",\r\n' +
' "URL": "$json:files[0].url$",\r\n' +
' "ThumbnailURL": "$json:files[0].url$"\r\n' +
'}'
var sharexBlob = new Blob([sharexFile], { type: 'application/octet-binary' })
sharexElement.setAttribute('href', URL.createObjectURL(sharexBlob))
sharexElement.setAttribute('download', location.hostname + '.sxcu')
}
}
page.createAlbum = function () {
var div = document.createElement('div')
div.innerHTML =

25
public/js/sharex.js Normal file
View File

@ -0,0 +1,25 @@
/* global page */
page.prepareShareX = function () {
if (!page.token) { return }
var origin = (location.hostname + location.pathname).replace(/\/(dashboard)?$/, '')
var originClean = origin.replace(/\//g, '_')
var sharexElement = document.getElementById('ShareX')
var sharexFile =
'{\r\n' +
' "Name": "' + originClean + '",\r\n' +
' "DestinationType": "ImageUploader, FileUploader",\r\n' +
' "RequestType": "POST",\r\n' +
' "RequestURL": "' + origin + '/api/upload",\r\n' +
' "FileFormName": "files[]",\r\n' +
' "Headers": {\r\n' +
' "token": "' + page.token + '"\r\n' +
' },\r\n' +
' "ResponseType": "Text",\r\n' +
' "URL": "$json:files[0].url$",\r\n' +
' "ThumbnailURL": "$json:files[0].url$"\r\n' +
'}'
var sharexBlob = new Blob([sharexFile], { type: 'application/octet-binary' })
sharexElement.setAttribute('href', URL.createObjectURL(sharexBlob))
sharexElement.setAttribute('download', originClean + '.sxcu')
}

View File

@ -15,7 +15,7 @@
v2: Images and config files (manifest.json, browserconfig.xml, etc).
v3: CSS and JS files (libs such as bulma, lazyload, etc).
#}
{% set v1 = "dK4iWQ3CEe" %}
{% set v1 = "gmQt09yByV" %}
{% set v2 = "Ii3JYKIhb0" %}
{% set v3 = "HrvcYD3KTh" %}

View File

@ -14,6 +14,7 @@
<script type="text/javascript" src="libs/clipboard.js/clipboard.min.js?v={{ globals.v3 }}"></script>
<script type="text/javascript" src="libs/lazyload/lazyload.min.js?v={{ globals.v3 }}"></script>
<script type="text/javascript" src="js/dashboard.js?v={{ globals.v1 }}"></script>
<script type="text/javascript" src="js/sharex.js?v={{ globals.v1 }}"></script>
{% endblock %}
{% block content %}

View File

@ -22,6 +22,7 @@
<script type="text/javascript" src="libs/clipboard.js/clipboard.min.js?v={{ globals.v3 }}"></script>
<script type="text/javascript" src="libs/lazyload/lazyload.min.js?v={{ globals.v3 }}"></script>
<script type="text/javascript" src="js/home.js?v={{ globals.v1 }}"></script>
<script type="text/javascript" src="js/sharex.js?v={{ globals.v1 }}"></script>
{% endblock %}
{% block content %}