i'm sleepy so english not very good looking
This commit is contained in:
2022-10-11 20:31:13 +02:00
parent a5ce23472a
commit 39d9c23dee
6 changed files with 62 additions and 9 deletions

172
_client/FileSaver.js Normal file
View File

@@ -0,0 +1,172 @@
/*
* FileSaver.js
* A saveAs() FileSaver implementation.
*
* By Eli Grey, http://eligrey.com
*
* License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
* source : http://purl.eligrey.com/github/FileSaver.js
*/
// The one and only way of getting global scope in all environments
// https://stackoverflow.com/q/3277182/1008999
var _global = typeof window === 'object' && window.window === window
? window : typeof self === 'object' && self.self === self
? self : typeof global === 'object' && global.global === global
? global
: this
function bom (blob, opts) {
if (typeof opts === 'undefined') opts = { autoBom: false }
else if (typeof opts !== 'object') {
console.warn('Deprecated: Expected third argument to be a object')
opts = { autoBom: !opts }
}
// prepend BOM for UTF-8 XML and text/* types (including HTML)
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type })
}
return blob
}
function download (url, name, opts) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = 'blob'
xhr.onload = function () {
saveAs(xhr.response, name, opts)
}
xhr.onerror = function () {
console.error('could not download file')
}
xhr.send()
}
function corsEnabled (url) {
var xhr = new XMLHttpRequest()
// use sync to avoid popup blocker
xhr.open('HEAD', url, false)
try {
xhr.send()
} catch (e) {}
return xhr.status >= 200 && xhr.status <= 299
}
// `a.click()` doesn't work for all browsers (#465)
function click (node) {
try {
node.dispatchEvent(new MouseEvent('click'))
} catch (e) {
var evt = document.createEvent('MouseEvents')
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
20, false, false, false, false, 0, null)
node.dispatchEvent(evt)
}
}
// Detect WebView inside a native macOS app by ruling out all browsers
// We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
// https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
var isMacOSWebView = _global.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent)
var saveAs = _global.saveAs || (
// probably in some web worker
(typeof window !== 'object' || window !== _global)
? function saveAs () { /* noop */ }
// Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
: ('download' in HTMLAnchorElement.prototype && !isMacOSWebView)
? function saveAs (blob, name, opts) {
var URL = _global.URL || _global.webkitURL
// Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue #561)
var a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
name = name || blob.name || 'download'
a.download = name
a.rel = 'noopener' // tabnabbing
// TODO: detect chrome extensions & packaged apps
// a.target = '_blank'
if (typeof blob === 'string') {
// Support regular links
a.href = blob
if (a.origin !== location.origin) {
corsEnabled(a.href)
? download(blob, name, opts)
: click(a, a.target = '_blank')
} else {
click(a)
}
} else {
// Support blobs
a.href = URL.createObjectURL(blob)
setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
setTimeout(function () { click(a) }, 0)
}
}
// Use msSaveOrOpenBlob as a second approach
: 'msSaveOrOpenBlob' in navigator
? function saveAs (blob, name, opts) {
name = name || blob.name || 'download'
if (typeof blob === 'string') {
if (corsEnabled(blob)) {
download(blob, name, opts)
} else {
var a = document.createElement('a')
a.href = blob
a.target = '_blank'
setTimeout(function () { click(a) })
}
} else {
navigator.msSaveOrOpenBlob(bom(blob, opts), name)
}
}
// Fallback to using FileReader and a popup
: function saveAs (blob, name, opts, popup) {
// Open a popup immediately do go around popup blocker
// Mostly only available on user interaction and the fileReader is async so...
popup = popup || open('', '_blank')
if (popup) {
popup.document.title =
popup.document.body.innerText = 'downloading...'
}
if (typeof blob === 'string') return download(blob, name, opts)
var force = blob.type === 'application/octet-stream'
var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
if ((isChromeIOS || (force && isSafari) || isMacOSWebView) && typeof FileReader !== 'undefined') {
// Safari doesn't allow downloading of blob URLs
var reader = new FileReader()
reader.onloadend = function () {
var url = reader.result
url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
if (popup) popup.location.href = url
else location = url
popup = null // reverse-tabnabbing #460
}
reader.readAsDataURL(blob)
} else {
var URL = _global.URL || _global.webkitURL
var url = URL.createObjectURL(blob)
if (popup) popup.location = url
else location.href = url
popup = null // reverse-tabnabbing #460
setTimeout(function () { URL.revokeObjectURL(url) }, 4E4) // 40s
}
}
)
_global.saveAs = saveAs.saveAs = saveAs
if (typeof module !== 'undefined') {
module.exports = saveAs;
}

124
_client/ajax.js Normal file
View File

@@ -0,0 +1,124 @@
const ws = new WebSocket("wss://HOSTNAMEHERE:443");
let downloadname
ws.onopen = () => {
ws.send("hello server")
}
ws.onclose = () => {
console.log('SERVER DISCONNECTED')
}
ws.onmessage = (message) => {
console.log('Server response received')
const BlobReader = new FileReader()
let data = message.data
if (isJSON(data)) {
document.getElementById('FileList').innerHTML = ""
document.getElementById('message').innerHTML = ""
data = JSON.parse(data)
if (data instanceof Array) {
for (let n = 0; n < data.length; n++) {
let list = document.createElement("input")
const br = document.createElement("br")
list.setAttribute('type', 'radio')
list.setAttribute('value', data[n])
list.setAttribute('name', 'option')
document.getElementById('FileList').append(list)
document.getElementById('FileList').append(data[n])
document.getElementById('FileList').append(br)
}
let button = document.createElement('button')
button.setAttribute('onclick', 'download()')
button.innerText = 'PayLoad'
document.getElementById('FileList').append(button)
}
}
if (isJSON(data) == false && data == 'hello, connection success!') {
document.getElementById('message').append(data)
}
if (data instanceof Blob) {
const cv = document.createElement('canvas')
const img = new Image();
const reader = new FileReader();
let src
blobToDataURL(data, (url) => {
src = url.replace('application/octet-stream', 'png')
img.src = src
img.onload = function () {
if (this.width && this.height) {
const ctx = cv.getContext('2d');
cv.setAttribute('width', this.width)
cv.setAttribute('height', this.height)
ctx.drawImage(this, 0, 0, this.width, this.height, 0, 0, this.width, this.height);
document.getElementById('PictureShow').append(cv)
}
}
})
console.log('starting download')
console.log(data)
var file = new File([data], downloadname);
saveAs(file);
}
}
ws.onerror = (err) => {
console.log(err)
}
function getFileList() {
ws.send('getFile')
}
function download() {
var radio = document.getElementsByName("option");
for (i = 0; i < radio.length; i++) {
if (radio[i].checked) {
downloadname = radio[i].value
ws.send(radio[i].value)
}
}
}
function isJSON(str) {
if (typeof str == 'string') {
try {
var obj = JSON.parse(str);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
}
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function (e) {
callback(e.target.result);
}
a.readAsDataURL(blob);
}

1
_client/base64.js Normal file
View File

@@ -0,0 +1 @@
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.base64js=a()}})(function(){return function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b}()({"/":[function(a,b,c){'use strict';function d(a){const b=a.length;if(0<b%4)throw new Error("Invalid string. Length must be a multiple of 4");let c=a.indexOf("=");-1===c&&(c=b);const d=c===b?0:4-c%4;return[c,d]}function e(a,b,c){return 3*(b+c)/4-c}function f(a){return h[63&a>>18]+h[63&a>>12]+h[63&a>>6]+h[63&a]}function g(a,b,c){let d;const e=[];for(let g=b;g<c;g+=3)d=(16711680&a[g]<<16)+(65280&a[g+1]<<8)+(255&a[g+2]),e.push(f(d));return e.join("")}c.byteLength=function(a){const b=d(a),c=b[0],e=b[1];return 3*(c+e)/4-e},c.toByteArray=function(a){let b;const c=d(a),f=c[0],g=c[1],h=new k(e(a,f,g));let l=0;const m=0<g?f-4:f;let n;for(n=0;n<m;n+=4)b=j[a.charCodeAt(n)]<<18|j[a.charCodeAt(n+1)]<<12|j[a.charCodeAt(n+2)]<<6|j[a.charCodeAt(n+3)],h[l++]=255&b>>16,h[l++]=255&b>>8,h[l++]=255&b;return 2===g&&(b=j[a.charCodeAt(n)]<<2|j[a.charCodeAt(n+1)]>>4,h[l++]=255&b),1===g&&(b=j[a.charCodeAt(n)]<<10|j[a.charCodeAt(n+1)]<<4|j[a.charCodeAt(n+2)]>>2,h[l++]=255&b>>8,h[l++]=255&b),h},c.fromByteArray=function(a){let b;const c=a.length,d=c%3,e=[],f=16383;for(let b=0,h=c-d;b<h;b+=f)e.push(g(a,b,b+f>h?h:b+f));return 1===d?(b=a[c-1],e.push(h[b>>2]+h[63&b<<4]+"==")):2===d&&(b=(a[c-2]<<8)+a[c-1],e.push(h[b>>10]+h[63&b>>4]+h[63&b<<2]+"=")),e.join("")};const h=[],j=[],k="undefined"==typeof Uint8Array?Array:Uint8Array,l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(let d=0,e=l.length;d<e;++d)h[d]=l[d],j[l.charCodeAt(d)]=d;j[45]=62,j[95]=63},{}]},{},[])("/")});

21
_client/file.html Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="FileSaver.js"></script>
<script src="base64.js"> </script>
<script src="ajax.js" defer></script>
<title>sWS FileTransfer</title>
</head>
<body style="text-align: center;">
<button onclick=getFileList()>ListFiles</button><br><br>
<div id="FileList"></div><br><br>
<div id="PictureShow"></div><br>
<div id="message"></div><br>
</body>
</html>