Enable image pasting from clipboard in Poe

Simulate drag and drop when pasting an image from the clipboard

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Enable image pasting from clipboard in Poe
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Simulate drag and drop when pasting an image from the clipboard
// @author       You
// @match        https://poe.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Helper function to dispatch an event
    function dispatchEvent(type, target, dataTransfer) {
        const event = new Event(type, {
            bubbles: true,
            cancelable: true
        });

        // Set the dataTransfer property if available
        if (dataTransfer) {
            event.dataTransfer = dataTransfer;
        }

        // Dispatch the event on the target element
        target.dispatchEvent(event);
    }

    // Add event listener for paste events
    window.addEventListener('paste', (event) => {
        let items = (event.clipboardData || event.originalEvent.clipboardData).items;

        for (let index in items) {
            let item = items[index];
            if (item.kind === 'file') {
                let blob = item.getAsFile();
                let dataTransfer = new DataTransfer();
                dataTransfer.items.add(blob);

                // Identify the drop zone element. You need to adjust this selector for the webpage.
                let dropZone = document.querySelector("div[class*=ChatDragDropTarget_dropTarget]");

                // Simulate the drag and drop events
                dispatchEvent('dragenter', dropZone, dataTransfer);
                dispatchEvent('dragover', dropZone, dataTransfer);
                dispatchEvent('drop', dropZone, dataTransfer);

                // Prevent the default paste action
                event.preventDefault();
            }
        }
    });
})();