Skip to content

Instantly share code, notes, and snippets.

@0x24a
Created August 31, 2023 13:19
Show Gist options
  • Save 0x24a/77a38915ac3f96edf6cb9b2402e14d9d to your computer and use it in GitHub Desktop.
Save 0x24a/77a38915ac3f96edf6cb9b2402e14d9d to your computer and use it in GitHub Desktop.
HC++ Plugin: Image uploading
function upload_image(url, base64) {
return new Promise((resolve, reject) => {
const socket = new WebSocket(url);
socket.onopen = () => {
const message = JSON.stringify({ data: base64 });
socket.send(message);
};
socket.onmessage = (event) => {
const result = event.data;
resolve(result);
socket.close();
};
socket.onerror = (error) => {
reject(error);
socket.close();
};
});
}
function getHostname(link){
let e=document.createElement("a");
e.setAttribute("href",link)
return e.hostname
}
function bind_event() {
var chatInput = document.getElementById('chatinput');
chatInput.addEventListener('paste', function(event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.type.indexOf('image') !== -1) {
event.preventDefault()
var file = item.getAsFile();
var reader = new FileReader();
reader.onload = function(event) {
var base64 = event.target.result;
if(typeof localStorage["img-endpoint"] == "undefined"){
pushMessage({nick:"!",text:"You must config an image endpoint to use fast image-sending."})
return 1
}
pushMessage({nick:"*",text:"Uploading image..."})
upload_image(localStorage["img-endpoint"],base64).then((result)=>{
if(result.startsWith("!")){
pushMessage({nick:"*",text:"Upload success."})
insertAtCursor(result)}else{pushMessage({nick:"!",text:result})}
}).catch((error)=>{
pushMessage({nick:"!",text:`Cannot upload: ${error}`})
})
};
reader.readAsDataURL(file);
}
}
});
if(typeof localStorage["img-endpoint"] != "undefined"){
// Whitelist itself
imgHostWhitelist.push(getHostname())
}
}
run["set-img-endpoint"]=function(){
localStorageSet("img-endpoint",arguments[0])
}
bind_event()
@0x24a
Copy link
Author

0x24a commented Aug 31, 2023

使用/set-img-endpoint 来设置endpoint。
粘贴图片到输入框就会自动上传。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment