Catbox Droptarget for Gazelle

Upload/rehost images to catbox.moe directly from upload page

Mint 2019.10.25.. Lásd a legutóbbi verzió

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Catbox Droptarget for Gazelle
// @namespace    https://greasyfork.org/users/321857-anakunda
// @version      1.07
// @description  Upload/rehost images to catbox.moe directly from upload page
// @author       Anakunda
// @iconURL      https://catbox.moe/pictures/favicon.ico
// @match        https://oppaiti.me/upload.php*
// @match        https://oppaiti.me/torrents.php?action=edit*
// @match        https://oppaiti.me/requests.php?action=new*
// @match        https://oppaiti.me/requests.php?action=edit*
// @match        https://oppaiti.me/reportsv2.php?*
// @match        https://oppaiti.me/artist.php?action=edit*
// @connect      catbox.moe
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_log
// ==/UserScript==

'use strict';

String.prototype.toASCII = function() {
  return this.normalize("NFKD").replace(/[\x00-\x1F\u0080-\uFFFF]/g, '');
}

document.head.appendChild(document.createElement('style')).innerHTML = `
.catbox-droptarget {
  margin-left: 8px; padding: 5px;
  background-color: #fff9cc; border: solid thin black;
  align-content: center; vertical-align: 1px;
}

.catbox-img {
  height: 25px;
  vertical-align: middle;
}
`;

var userhash = GM_getValue('userhash');
var image;
bindAll();
onReportTypeChange();
if (document.location.pathname.toLowerCase() != '/requests.php') {
  var rlsTypeSelect = document.querySelector('select#categories');
  if (rlsTypeSelect != null) rlsTypeSelect.addEventListener('change', onRlsTypeChange);
}
var reportTypeSelect = document.querySelector('select#type');
if (reportTypeSelect != null) reportTypeSelect.addEventListener('change', onReportTypeChange);
GM_setValue('userhash', userhash || '');

function onReportTypeChange(evt) {
  setTimeout(function() {
	if (evt instanceof Event) {
	  image = document.querySelector('input#proofimages') || document.querySelector('input#image');
	  if (image != null) image.parentNode.append(createDropTarget(image));
	}
	bindToTextarea('extra');
  }, 1000);
}
function onRlsTypeChange(evt) { setTimeout(bindAll, 1000) }

function imageDropHandler(evt) {
  evt.preventDefault();
  uploadFiles(evt.currentTarget, evt.dataTransfer.files);
}

function clickHandler(evt) {
  evt.preventDefault();
  if (!evt.currentTarget.boundElement) throw new Error('boundElement not set');
  if (evt.currentTarget.boundElement.nodeName == 'INPUT' && /^https?:\/\//i.test(evt.currentTarget.boundElement.value)
	  && !evt.currentTarget.boundElement.value.toLowerCase().includes('catbox.moe/')) {
	rehostUrl(evt.currentTarget, evt.currentTarget.boundElement.value);
  } else {
	let currentTarget = evt.currentTarget;
	let inputElement = document.createElement("input");
	inputElement.type = "file";
	inputElement.accept = '.jpg, .jpeg, .jfif, .png, .gif, .webp';
	inputElement.multiple = true;
	inputElement.onchange = evt => { uploadFiles(currentTarget, inputElement.files) };
	inputElement.dispatchEvent(new MouseEvent("click"));
  }
}

function voidDragHandler(evt) { evt.preventDefault() }

function uploadFiles(evtSrc, files) {
  if (files.length <= 0) return;
  if (!evtSrc.boundElement) throw new Error('boundElement not set');
  if (evtSrc.busy) throw new Error('Wait till current upload finishes');
  evtSrc.busy = true;
  if (evtSrc.hTimer) {
	clearTimeout(evtSrc.hTimer);
	delete evtSrc.hTimer;
  }
  evtSrc.style.backgroundColor = 'red';
  Promise.all(upload2Catbox(files))
	.then(function(results) {
	  if (results.length > 0) {
		switch (evtSrc.boundElement.nodeName) {
		  case 'INPUT':
			evtSrc.boundElement.value = results[0];
			break;
		  case 'TEXTAREA':
			evtSrc.boundElement.value += results.map(url => '[img]' + url + '[/img]').join('\n');
			break;
		}
		evtSrc.style.backgroundColor = '#00C000';
		evtSrc.hTimer = setTimeout(function() {
		  evtSrc.style.backgroundColor = null;
		  delete evtSrc.hTimer;
		}, 3000);
	  } else evtSrc.style.backgroundColor = null;
	}).catch(function(e) {
	  alert(e);
	  evtSrc.style.backgroundColor = null;
	}).then(function() {
	  evtSrc.busy = false;
  	});
};

function rehostUrl(evtSrc, url) {
  if (!/^https?:\/\//i.test(url)) return;
  if (!evtSrc.boundElement) throw new Error('boundElement not set');
  if (evtSrc.busy) throw new Error('Wait till current upload finishes');
  evtSrc.busy = true;
  if (evtSrc.hTimer) {
	clearTimeout(evtSrc.hTimer);
	delete evtSrc.hTimer;
  }
  evtSrc.style.backgroundColor = 'red';
  rehost2Catbox(evtSrc.boundElement.value).then(function(result) {
	evtSrc.boundElement.value = result;
	evtSrc.style.backgroundColor = '#00C000';
	evtSrc.hTimer = setTimeout(function() {
	  delete evtSrc.hTimer;
	  evtSrc.style.backgroundColor = null;
	}, 3000);
  }).catch(function(e) {
	alert(e);
	evtSrc.style.backgroundColor = null;
  }).then(function() {
	evtSrc.busy = false;
  });
}

function bindAll() {
  if ((image = document.getElementById('image')) != null) {
	image.parentNode.append(createDropTarget(image));
  } else if ((image = document.querySelector('input[name="image"]')) != null) {
	image.parentNode.insertBefore(createDropTarget(image), image.parentNode.querySelector(':scope > br'));
  }
  ['album_desc', 'release_desc', 'desc', 'body', 'description', 'screenshots'].forEach(bindToTextarea);
}

function bindToTextarea(id) {
  var desc = document.querySelector('textarea#' + id);
  if (desc != null) {
	var btn = desc.parentNode.parentNode.querySelector('div > input[class^="button_preview"]');
	if (btn != null) {
	  btn.parentNode.append(createDropTarget(desc));
	} else if ((btn = desc.parentNode.parentNode.querySelector(':scope > td > strong')) != null) {
	  var div = document.createElement('div');
	  div.style.marginTop = '60px';
	  div.append(createDropTarget(desc));
	  btn.parentNode.append(div);
	} else if ((btn = desc.parentNode.querySelector('div#Bbcode_Toolbar > div[style]:last-of-type')) != null) {
	  btn.parentNode.insertBefore(createDropTarget(desc), btn);
	}
	return btn != null;
  } else if ((desc = document.querySelector('textarea[name="' + id + '"]')) != null
		&& (btn = desc.parentNode.querySelector(':scope > div > input[value="Submit"]')) != null) {
	btn.parentNode.append(createDropTarget(desc));
	return true;
  }
  return false;
}

function createDropTarget(boundElement) {
  if (!(boundElement instanceof HTMLElement)) throw new Error('invalid boundElement');
  var dropTarget = document.createElement('span');
  dropTarget.boundElement = boundElement;
  dropTarget.className = 'catbox-droptarget';
  dropTarget.ondragover = voidDragHandler;
  dropTarget.ondrop = imageDropHandler; // upload
  dropTarget.onclick = clickHandler; // rehost
  var img = document.createElement('img');
  img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAAZCAYAAABtnU33AAAACXBIWXMAAAsSAAALEgHS3X78AAAKVElEQVRYw+1Ye1BU1xn/nXPvsruwuoKIqIjSCgGjhgCiQZqQSJRWJQ+bElNf4wPEjklVMJJJTYioLUbzqNakWkwRMQi2xhIa0OKjQosCYkYcVDRBCcjyJrDL7r33fP2DXUM6SZq06XSc9Pxx595zv/P4zvn9vhcjInyXGsd3rMl38+aJACE0iXMGEsQEEXHOBWOMhNAkgJEkSWLwGHY3QpqIoGmaLEmSYIyJr5IVQkhEdEfxu05hTdMkxhhxzoXVZseRgsOzz5f/ZfnIUX51gcGTz4aHR1wwGE3tp0qPzxnpO6opNnZmFQCmaSrnXNLuGoWJiAkhuCRJmtXWz3bv/s1TZ0uPpYwbzqaOGzMcmtCos9vKKi83dxiN7u0h44YGtnVZlV74HFiX+sKmaZERnxAR+5zCmqZJRMQAkCRJGmPsG524E0JMCMFlWda+bYUZY5T77uFp27e98lqAD3/gkRmTaYhpCAEQt9u6YWnr5uYhBj7GZxi4JAtJ4viooZHn//nCp4/OW/B85i+37mFEBCJiRMRcm3ZunDPGxNdR+r99s4wx6uzsNKxMTMqoqTixbun8h1iA/yitp9cGgKSKmuuQJ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dGjR9ctXrz49ZMnT052zfnss2s27MjMoPqaEntjQz2tT0n5+4WaD3VJictfnzk9kLY//xTlvv4z9bdbl6tvblpIWb9aSYsfm6YlJq7QWm6cU2ovVlD6Kxm7aCCfFIyIWH9/P/Ly8mJSU1NT9+3bl3D79m2Ta8GKiop7kpKSXt2/f/9cIsKJEycm5eXlPUJEsFgspmPHjkW4ZI8ePTpj/vz5OydNmnQqKirqD6dOnQohIthsNqSnpyf7+/ufGz58ePWLL764uq+vDwkJCXsOHDgQT0Sor6/3Wb58eeaMGTNOFhQURDvn5B0dndKqVcmZK5YtbX/55XRKTFxZ0NfXCyLCO7/PeXBq2OTzs6ICKWPt4+LAzmQt79drlM1rHye/UcOtaWlp9rSNabdLSkoiiWggPXS6CAzOLV3+0gUR17sQgg+OuV2h6VcFSy6DOJCYCxARJEn6XMSmaZrMOde+KCojIi6EEFeuXB3W2HjTc+rUyFuenp6qi+dd3T387bf3PpGfd2A9d7RPN5v0rL7JemPz1lcXjx/n32Q2m/smT55sIaKBWNmpNBRFkRRFkRVFkZ037woGuKIosqqqEhFBVVWuKIrkHMdUVeX/LKtpGldVVXb9c84vCyE4ETFVVSVXn2teTdOYa31nkOLaG3Nmcne+Xe+qqkouWYdDwRtv7n5sxcpV265du+41SJY51/3fFAC+Biq+rHpxJ6sbPP6LKiAuJQGQM32lu7bE86+yK+ehaC4l7/qa1v/LtN+g/QOyFfU2eCLkFgAAAABJRU5ErkJggg==';
  img.onerror = function() { this.src = 'https://catbox.moe/pictures/logo.png' };
  img.className = 'catbox-img';
  dropTarget.append(img);
  return dropTarget;
}

function upload2Catbox(files) {
  if (!(files instanceof FileList)) return Promise.reject('Bad parameter (files)');
  return Array.from(files)
	.sort((file1, file2) => file1.name.localeCompare(file2.name))
	.map(file => new Promise(function(resolve, reject) {
	  var fr = new Promise(function(resolve) {
		var reader = new FileReader();
		reader.onload = function() { resolve(reader.result) }
		reader.readAsBinaryString/*readAsArrayBuffer(file)*/(file);
	  });
	  fr.then(function(result) {
		const boundary = '----WebKitFormBoundaryTID_GM';
		var data = '--' + boundary + '\r\n';
		data += 'Content-Disposition: form-data; name="reqtype"\r\n\r\n';
		data += 'fileupload\r\n';
		if (userhash) {
		  data += '--' + boundary + '\r\n';
		  data += 'Content-Disposition: form-data; name="userhash"\r\n\r\n';
		  data += userhash + '\r\n';
		}
		data += '--' + boundary + '\r\n';
		data += 'Content-Disposition: form-data; name="fileToUpload"; filename="' + file.name.toASCII() + '"\r\n';
		data += 'Content-Type: ' + file.type + '\r\n\r\n';
		data += result + '\r\n';
		data += '--' + boundary + '--\r\n';
		GM_xmlhttpRequest({
		  method: 'POST',
		  url: 'https://catbox.moe/user/api.php',
		  responseType: 'text',
		  headers: {
			'Content-Type': 'multipart/form-data; boundary=' + boundary,
			'Content-Length': data.length,
		  },
		  data: data,
		  binary: true,
		  onload: function(response) {
			if (response.status != 200) reject('Response error ' + response.status + ' (' + response.statusText + ')');
			resolve(response.response);
		  },
		  onerror: response => { reject('Response error ' + response.status + ' (' + response.statusText + ')') },
		  ontimeout: function() { reject('Timeout') },
		});
	  });
	}));
}

function rehost2Catbox(url) {
  if (typeof url != 'string' || !url) return Promise.reject('Bad parameter (url)');
  return new Promise(function(resolve, reject) {
	var data = new URLSearchParams({
	  reqtype: 'urlupload',
	  url: url.trim(),
	});
	if (userhash) data.set('userhash', userhash);
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: 'https://catbox.moe/user/api.php',
	  responseType: 'text',
	  headers: {
		'Content-Type': 'application/x-www-form-urlencoded',
		'Content-Length': data.toString().length,
	  },
	  data: data.toString(),
	  onload: function(response) {
		if (response.status != 200) reject('Response error ' + response.status + ' (' + response.statusText + ')');
		resolve(response.response);
	  },
	  onerror: response => { reject('Response error ' + response.status + ' (' + response.statusText + ')') },
	  ontimeout: function() { reject('Timeout') },
	});
  });
}