BetterNoelshack

Drag n drop ou copier coller d'image vers noelshack sur les forums JVC

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         BetterNoelshack
// @version      0.1
// @description  Drag n drop ou copier coller d'image vers noelshack sur les forums JVC 
// @author       cogis
// @match        *://*.jeuxvideo.com/forums*
// @run-at       document-end
// @icon         https://www.google.com/s2/favicons?sz=64&domain=noelshack.com
// @grant        GM_xmlhttpRequest
// @connect      www.noelshack.com
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    const textArea = document.querySelector('#message_topic');

    textArea.addEventListener('drop', handleDrop);
    textArea.addEventListener('paste', handlePaste);

    async function handleDrop(event) {
        const dataTransfer = event.dataTransfer;
        if (dataTransfer.types && Array.from(dataTransfer.types).includes('Files')) {
            event.preventDefault();
            event.stopPropagation();
            const files = dataTransfer.files;
            await uploadFiles(files);
        }
    }

    async function handlePaste(event) {
        const clipboardData = event.clipboardData;
        if (clipboardData.types.includes('Files')) {
            event.preventDefault();
            const files = clipboardData.files;
            await uploadFiles(files);
        }
    }

    async function uploadFiles(files) {
        for (let i = 0; i < files.length; i++) {
            let file = files[i];
            if (file.type.includes("image")) {
                if (file.type === "image/webp") {
                    file = await convertWebPToJPEG(file);
                }
                const imageUrl = await uploadFile(file);
                if (imageUrl) {
                    updateTextArea(imageUrl);
                }
                await new Promise(resolve => setTimeout(resolve, 200));
                if (i >= 1) {
                    await new Promise(resolve => setTimeout(resolve, 2000));
                }
            }
        }
    }

    async function uploadFile(file) {
        const formData = new FormData();
        formData.append('publication', '0');
        formData.append('domain', 'https://www.jeuxvideo.com');
        formData.append('fichier', file);

        const response = await sendRequest(formData);

        try {
            const data = JSON.parse(response.responseText);
            return data.url || null;
        } catch (error) {
            console.error("Error parsing JSON:", error);
            return null;
        }
    }

    function updateTextArea(imageUrl) {
        imageUrl = " " + imageUrl + " ";
        const position = textArea.selectionStart;
        const before = textArea.value.substring(0, position);
        const after = textArea.value.substring(position, textArea.value.length);
        textArea.value = before + imageUrl + after;
        textArea.selectionStart = textArea.selectionEnd = position + imageUrl.length;
        const changeEvent = new Event('change', { bubbles: true });
        textArea.dispatchEvent(changeEvent);
    }

    async function convertWebPToJPEG(webpBlob) {
        const img = document.createElement('img');
        img.src = URL.createObjectURL(webpBlob);

        return new Promise((resolve) => {
            img.onload = function () {
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');
                canvas.width = img.width;
                canvas.height = img.height;

                ctx.drawImage(img, 0, 0, img.width, img.height);
                canvas.toBlob((blob) => {
                    resolve(blob);
                }, 'image/jpeg');
            };
        });
    }

    function sendRequest(formData) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'POST',
                url: 'https://www.noelshack.com/webservice/envoi.json',
                data: formData,
                onload: function(response) {
                    resolve(response);
                },
                onerror: function(error) {
                    console.error(error);
                    reject(error);
                }
            });
        });
    }

})();