Image Uploader for Torn Forums

Uploads images from clipboard and inserts the URL into the Torn forums post editor

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Image Uploader for Torn Forums
// @version      1.01
// @description  Uploads images from clipboard and inserts the URL into the Torn forums post editor
// @match        https://www.torn.com/forums.php*
// @grant        GM_xmlhttpRequest
// @namespace https://greasyfork.org/users/1041152
// ==/UserScript==

const CLOUD_NAME = 'dhc6ecjkq';

const UPLOAD_PRESET = 'mswoyebz';

// Get the paste event
document.addEventListener('paste', function(event) {
  // Check if the clipboard item is an image
  if (event.clipboardData && event.clipboardData.items) {
    var imageItem = null;
    for (var i = 0; i < event.clipboardData.items.length; i++) {
      if (event.clipboardData.items[i].type.indexOf('image') !== -1) {
        imageItem = event.clipboardData.items[i];
        break;
      }
    }
    // If an image is found, upload it to Cloudinary
    if (imageItem) {
      var formData = new FormData();
      formData.append('file', imageItem.getAsFile(), 'image.png');
      formData.append('upload_preset', UPLOAD_PRESET);
      GM_xmlhttpRequest({
        method: "POST",
        url: `https://api.cloudinary.com/v1_1/${CLOUD_NAME}/image/upload`,
        data: formData,
        onload: function(response) {
            // Image uploaded successfully, get the hosted URL
            var imageUrl = JSON.parse(response.responseText).secure_url;
            //prompt('', imageUrl);
            var toggle = $('.icon.icon-source.source:not(.active)')[0]
            if (toggle) { toggle.click() }
          document.querySelector('#editor-textarea').value += ' [img]' + imageUrl + '[/img]';
            toggle.click();
        }
      });
    }
  }
});