From 4c5dd23c88808481b34a2b2dc071b1403a9a0555 Mon Sep 17 00:00:00 2001 From: gzezFISHER Date: Wed, 21 Sep 2022 15:40:34 +0800 Subject: [PATCH] [feat] upload files Co-authored-by: szdytom < szdytom@163.com > --- public/index.html | 7 +++-- public/script.js | 67 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/public/index.html b/public/index.html index e90d85a..2bf4026 100644 --- a/public/index.html +++ b/public/index.html @@ -13,13 +13,14 @@
- +

Enable auto scroll

Version: SOCKET.IO-2.2

Press Ctrl + Enter to send - + +
@@ -30,6 +31,8 @@ + + \ No newline at end of file diff --git a/public/script.js b/public/script.js index 943e0e2..f26e60c 100644 --- a/public/script.js +++ b/public/script.js @@ -16,6 +16,8 @@ let ws; let md; let unread_message = 0; +let assets = []; + $(() => { init().then(x => ws = x).catch(err => console.log(err)) }); @@ -222,6 +224,23 @@ async function init() { }); }); + $('#clear-button').click(function() { + clear_message(); + }); + + $('#send-button').click(async function() { + await send(); + }) + + $('#uploader').click(function() { + $('#real-uploader').click(); + }); + + $('#real-uploader').change(async function() { + const content = await upload(this.files[0]); + $('#send').val($('#send').val() + content); + }); + $('#send').focus(); return ws; @@ -249,27 +268,46 @@ function open_prompt(title) { return res; } -async function get_url_query(query_name) { - return new Promise(resolve => { - var reg = new RegExp("(^|&)" + query_name + "=([^&]*)(&|$)", "i"); - var r = window.location.search.substr(1).match(reg); - var context = ""; - if (r != null) - context = r[2]; - reg = null; - r = null; - resolve(context == null || context == "" || context == "undefined" ? "" : context); - }); +function get_url_query(query_name) { + var reg = new RegExp("(^|&)" + query_name + "=([^&]*)(&|$)", "i"); + var r = window.location.search.substr(1).match(reg); + var context = ""; + if (r != null) { + context = r[2]; + } + reg = null; + r = null; + return context == null || context == "" || context == "undefined" ? "" : context; } async function login_name() { - username = await get_url_query("name") + username = get_url_query("name"); if (username === undefined || username.length == 0) { username = await open_prompt('[Login] Input your name'); } } +function encode_file(file) { + return new Promise((resolve,) => { + let reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = (e) => { + resolve(e.target.result); + }; + }); +} + +async function upload(file) { + const dataurl = await encode_file(file); + const id = assets.length; + assets.push(dataurl); + if (file.type.startsWith('image/')) { + return `![](%asset${id})`; + } + return `[](%asset${id})`; +} + async function send() { let data = $('#send').val(); if (data === '') { @@ -287,6 +325,11 @@ async function send() { last_command = data; } + for (let i in assets) { + data = data.replaceAll(`%asset${i}`, assets[i]); + } + assets = []; + if (data.startsWith('/su')) { // a administrator login command. // now ask for passcode