Image Uploader for Torn Forums

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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();
        }
      });
    }
  }
});