This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
forked from csznet/tgState
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
23 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{{define "public/footer"}} | ||
<script defer>const $uploadButton=document.getElementById("uploadButton"),$response=document.getElementById("response"),$uploadFile=document.getElementById("uploadFile"),$uploadFileLabel=document.getElementById("uploadFileLabel"),$copyCodeButtons_text=".copy-code",errorClassName="response-item response-error",successClassName="response-item response-success",serverUrl=window.location.origin,serverUrl_API=`${serverUrl}/api`,SizeThreshold=20971520,retryCount=5,retryDelay=2e3,sleep=e=>new Promise((t=>setTimeout(t,e))),uploadFile=e=>{const t=e.name,o=e=>console.log(`${t}: ${serverUrl}${e}`),n=e=>($uploadButton.disabled=!1,$uploadButton.textContent="点击上传",console.error("上传失败! ",e),Promise.reject("上传失败! ",e));if(e.size<=20971520)handleFileUpload(e,1,t).then(o).catch(n);else{let l=`tgstate-blob\n${t}`,a=`${t}上传块第一次失败,将以每2秒重试(5)次!`;const s=(o,n,r=0)=>{if(o<e.size){const d=e.slice(o,n);return handleFileUpload(d,0,t,a).then((t=>(l+=`\n${t.substring(14)}`,o=n,n=Math.min(o+20971520,e.size),s(o,n))),(l=>r<5?(a=`${t}上传块失败,2秒后重试 (${r+1}/5)`,console.warn(a),creatErrorElement(e,t,a,l),sleep(2e3).then((()=>s(o,n,r+1)))):(a=`${t}上传失败!已达到最大重试次数(5)!`,console.error(a),creatErrorElement(e,t,a,l),Promise.reject(a))))}return Promise.resolve()};s(0,20971520).then((()=>{console.log(l);const e=new Blob([l],{type:"text/plain"}),o=new File([e],`${t}_fileAll.txt`,{type:"text/plain"});return handleFileUpload(o,1,t)})).then(o).catch(n)}},creatErrorElement=(e,t,o,n)=>{const l=`${t}_${e.size}_${e.lastModified}`,a=$response.querySelector(`.response-item.response-error[data-identifier="${l}"]`),s=o?`${o} (${n})`:`${t}上传失败! (${n})`;if(a)a.textContent=s;else{const e=document.createElement("div");e.className=errorClassName,e.textContent=s,$response.prepend(e)}},handleFileUpload=(e,t,o,n)=>new Promise(((l,a)=>{const s=new FormData;s.append("file",e),$uploadButton.disabled||($uploadButton.disabled=!0,$uploadButton.textContent=`${o}上传中`),fetch(serverUrl_API,{method:"POST",body:s,headers:{}}).then((e=>e.json())).then((n=>{if(1===n.code){if(l(n.message),t){const t=`${serverUrl}${n.message}?filename=${o}`,l=document.createElement("div"),a=e.type.startsWith("image/")?"图片":"文件";l.className=successClassName,l.innerHTML=`<p>${o}上传成功,${a}外链: </p><a target="_blank" href="${t}">${t}</a><div class="copy-links"><span class="copy-code" data-clipboard-text="<img src="${t}" alt="Your Alt Text">">HTML</span><span class="copy-code" data-clipboard-text="">Markdown</span><span class="copy-code" data-clipboard-text="[img]${t}[/img]">BBCode</span><span class="copy-code" data-clipboard-text="${t}">复制外链</span></div>`,$response.prepend(l),$uploadFile.value="",$uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF",$uploadButton.disabled=!1,$uploadButton.textContent="点击上传"}}else a(n.message)})).catch((()=>{creatErrorElement(e,o,n,response.message)}))})),$copyCodeButtonsContainer=document.body;$copyCodeButtonsContainer.addEventListener("click",(function(e){const t=e.target.closest(".copy-code");if(t){const e=t.dataset.clipboardText,o=t.textContent;try{navigator.clipboard.writeText(e)}catch(e){console.error("使用Clipboard API复制失败:",e);const o=t.dataset.clipboardText,n=document.createElement("input");document.body.appendChild(n),n.value=o,n.select(),document.execCommand("copy"),document.body.removeChild(n)}t.textContent="复制成功",setTimeout((()=>t.textContent=o),500)}})),document.addEventListener("DOMContentLoaded",(function(){$uploadFile.addEventListener("change",(function(){handleFileSelection(this.files)})),$uploadButton.addEventListener("click",(function(){if($uploadFile.files.length>0)for(let e=0;e<$uploadFile.files.length;e++)uploadFile($uploadFile.files[e]);else alert("请至少选择一个文件")}));let e=0;document.documentElement.addEventListener("dragover",(function(e){e.preventDefault()})),document.documentElement.addEventListener("dragenter",(function(t){t.preventDefault(),0===e&&(document.documentElement.style.backgroundColor="rgba(0, 123, 255, 0.1)"),e++})),document.documentElement.addEventListener("dragleave",(function(t){e--,0===e&&(document.documentElement.style.backgroundColor="transparent")})),document.documentElement.addEventListener("drop",(function(t){t.preventDefault(),document.documentElement.style.backgroundColor="transparent",$uploadFile.files=t.dataTransfer.files,handleFileSelection($uploadFile.files),e=0}))}));const handleFileSelection=e=>{e.length>0?(1===e.length?$uploadFileLabel.textContent="已选择文件: "+e[0].name:$uploadFileLabel.textContent=`已选择${e.length}个文件`,$uploadFileLabel.style.backgroundColor="#0056b3"):($uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF")}</script> | ||
<script defer>const $uploadButton=document.getElementById("uploadButton"),$response=document.getElementById("response"),$uploadFile=document.getElementById("uploadFile"),$uploadFileLabel=document.getElementById("uploadFileLabel"),$copyCodeButtons_text=".copy-code",errorClassName="response-item response-error",successClassName="response-item response-success",serverUrl=window.location.origin,serverUrl_API=`${serverUrl}/api`,SizeThreshold=20971520,retryCount=5,retryDelay=2e3,sleep=e=>new Promise((t=>setTimeout(t,e))),uploadFile=e=>{const t=e.name,o=e=>console.log(`${t}: ${serverUrl}${e}`),n=e=>(recoverUploadButton(),console.error("上传失败! ",e),Promise.reject("上传失败! ",e));if(e.size<=20971520)handleFileUpload(e,1,t).then(o).catch(n);else{let l=`tgstate-blob\n${t}`,a=`${t}上传块第一次失败,将以每2秒重试(5)次!`;const r=(o,n,s=0)=>{if(o<e.size){const d=e.slice(o,n);return handleFileUpload(d,0,t,a).then((t=>(l+=`\n${t.substring(14)}`,o=n,n=Math.min(o+20971520,e.size),r(o,n))),(l=>s<5?(a=`${t}上传块失败,2秒后重试 (${s+1}/5)`,console.warn(a),creatErrorElement(e,t,a,l),sleep(2e3).then((()=>r(o,n,s+1)))):(a=`${t}上传失败!已达到最大重试次数(5)!`,console.error(a),creatErrorElement(e,t,a,l),Promise.reject(a))))}return Promise.resolve()};r(0,20971520).then((()=>{console.log(l);const e=new Blob([l],{type:"text/plain"}),o=new File([e],`${t}_fileAll.txt`,{type:"text/plain"});return handleFileUpload(o,1,t)})).then(o).catch(n)}},creatErrorElement=(e,t,o,n)=>{const l=`${t}_${e.size}_${e.lastModified}`,a=$response.querySelector(`.response-item.response-error[data-identifier="${l}"]`),r=o?`${o} (${n})`:`${t}上传失败! (${n})`;if(a)a.textContent=r;else{const e=document.createElement("div");e.className=errorClassName,e.textContent=r,$response.prepend(e)}},recoverUploadButton=()=>{$uploadFile.value="",$uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF",$uploadButton.disabled=!1,$uploadButton.textContent="点击上传"},handleFileUpload=(e,t,o,n)=>new Promise(((l,a)=>{const r=new FormData;r.append("file",e),$uploadButton.disabled||($uploadButton.disabled=!0,$uploadButton.textContent=`${o}上传中`),fetch(serverUrl_API,{method:"POST",body:r,headers:{}}).then((e=>e.json())).then((n=>{if(1===n.code){if(l(n.message),t){const t=`${serverUrl}${n.message}?filename=${o}`,l=document.createElement("div"),a=e.type.startsWith("image/")?"图片":"文件";l.className=successClassName,l.innerHTML=`<p>${o}上传成功,${a}外链: </p><a target="_blank" href="${t}">${t}</a><div class="copy-links"><span class="copy-code" data-clipboard-text="<img src="${t}" alt="Your Alt Text">">HTML</span><span class="copy-code" data-clipboard-text="">Markdown</span><span class="copy-code" data-clipboard-text="[img]${t}[/img]">BBCode</span><span class="copy-code" data-clipboard-text="${t}">复制外链</span></div>`,$response.prepend(l),recoverUploadButton()}}else void 0===n.message&&(recoverUploadButton(),n.message="未返回JSON数据!"),a(n.message)})).catch((()=>{creatErrorElement(e,o,n,response.message)}))})),$copyCodeButtonsContainer=document.body;$copyCodeButtonsContainer.addEventListener("click",(function(e){const t=e.target.closest(".copy-code");if(t){const e=t.dataset.clipboardText,o=t.textContent;try{navigator.clipboard.writeText(e)}catch(e){console.error("使用Clipboard API复制失败:",e);const o=t.dataset.clipboardText,n=document.createElement("input");document.body.appendChild(n),n.value=o,n.select(),document.execCommand("copy"),document.body.removeChild(n)}t.textContent="复制成功",setTimeout((()=>t.textContent=o),500)}})),document.addEventListener("DOMContentLoaded",(function(){$uploadFile.addEventListener("change",(function(){handleFileSelection(this.files)})),$uploadButton.addEventListener("click",(function(){if($uploadFile.files.length>0)for(let e=0;e<$uploadFile.files.length;e++)uploadFile($uploadFile.files[e]);else alert("请至少选择一个文件")}));let e=0;document.documentElement.addEventListener("dragover",(function(e){e.preventDefault()})),document.documentElement.addEventListener("dragenter",(function(t){t.preventDefault(),0===e&&(document.documentElement.style.backgroundColor="rgba(0, 123, 255, 0.1)"),e++})),document.documentElement.addEventListener("dragleave",(function(t){e--,0===e&&(document.documentElement.style.backgroundColor="transparent")})),document.documentElement.addEventListener("drop",(function(t){t.preventDefault(),document.documentElement.style.backgroundColor="transparent",$uploadFile.files=t.dataTransfer.files,handleFileSelection($uploadFile.files),e=0}))}));const handleFileSelection=e=>{e.length>0?(1===e.length?$uploadFileLabel.textContent="已选择文件: "+e[0].name:$uploadFileLabel.textContent=`已选择${e.length}个文件`,$uploadFileLabel.style.backgroundColor="#0056b3"):($uploadFileLabel.textContent="选择文件或拖拽到此区域",$uploadFileLabel.style.backgroundColor="#007BFF")}</script> | ||
{{end}} |
Binary file not shown.
Binary file not shown.