Greasy Fork is available in English.
在hostloc.com的论坛中自动上传图片到图床并插入BB链接
当前为
// ==UserScript==
// @name Hostloc自动上传到屋舍图床并插入BB链接
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 在hostloc.com的论坛中自动上传图片到图床并插入BB链接
// @author WiseScripts
// @match *://*.hostloc.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
document.addEventListener('paste', (event) => handlePaste(event));
async function handlePaste(event) {
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
let imageFiles = [];
for (let item of items) {
if (item.kind === 'file' && item.type.indexOf('image/') !== -1) {
let blob = item.getAsFile();
imageFiles.push(blob);
}
}
if (imageFiles.length > 0) {
event.preventDefault();
for (let file of imageFiles) {
let formData = new FormData();
formData.append('file', file);
await uploadToImageHost(formData);
}
}
}
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function uploadToImageHost(formData) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'POST',
url: 'https://uhsea.com/Frontend/upload',
data: formData,
onload: (response) => {
let jsonResponse = JSON.parse(response.responseText);
if (response.status === 200 && jsonResponse && jsonResponse.data) {
insertToEditor(jsonResponse.data);
} else {
showErrorPopup('图片上传成功,但未获取到图片链接');
}
resolve();
},
onerror: (error) => {
showErrorPopup('图片上传遇到错误,请检查网络或稍后重试。');
reject(error);
}
});
});
}
function insertToEditor(Link) {
let e = document.querySelector("#e_textarea")
if (!e) {
e = document.querySelector("#fastpostmessage")
}
if (e) {
let bbLink = '[img]' + Link + '[/img]';
copyTextToClipboard(bbLink);
e.setRangeText('\n' + bbLink + '\n');
}
}
function showErrorPopup(errorMessage) {
const popup = document.createElement('div');
popup.style.position = 'fixed';
popup.style.right = '20px';
popup.style.bottom = '20px';
popup.style.backgroundColor = 'red';
popup.style.color = 'white';
popup.style.padding = '10px';
popup.style.borderRadius = '5px';
popup.style.zIndex = '10000';
popup.textContent = errorMessage;
const closeButton = document.createElement('button');
closeButton.textContent = 'X';
closeButton.style.marginLeft = '10px';
closeButton.style.color = 'white';
closeButton.style.backgroundColor = 'transparent';
closeButton.style.border = 'none';
closeButton.onclick = () => popup.remove();
popup.appendChild(closeButton);
document.body.appendChild(popup);
setTimeout(() => {
if (popup) {
popup.remove();
}
}, 5000);
}
})();