hipda-img-paster

支持在发帖/回帖(高级模式)时直接粘贴图片

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         hipda-img-paster
// @namespace    https://github.com/maltoze/tampermonkey-scripts
// @version      0.1.4
// @description  支持在发帖/回帖(高级模式)时直接粘贴图片
// @author       maltoze
// @match        https://www.hi-pda.com/forum/post.php?*
// @match        https://www.4d4y.com/forum/post.php?*
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const BASE_URL = 'https://www.4d4y.com/forum/';
  const IMG_UPLOAD_URL = `${BASE_URL}misc.php?action=swfupload&operation=upload&simple=1&type=image`;
  const COMMON_OPTIONS = { credentials: 'same-origin' };
  const TIMEOUT = 30000;

  // chrome/edge
  const iframeEl = document.getElementById('e_iframe');
  // firefox
  const textAreaEl = document.getElementById('e_textarea');

  function imgListAjaxUrlGenenter(postTime) {
    return `${BASE_URL}ajax.php?action=imagelist&pid=NaN&posttime=${postTime.toFixed()}`;
  }

  function uploadImg(imgFile) {
    const userSpaceEl = document.querySelector('#umenu > cite > a');
    if (!userSpaceEl) return;

    const uidMatch = userSpaceEl.href.match(/uid=(\d+)/);
    if (!uidMatch) return;

    const hashInputEl = document.querySelector('input[name=hash]');
    if (!hashInputEl) return;

    const formData = new FormData();
    formData.append('uid', uidMatch[1]);
    formData.append('hash', hashInputEl.value);
    formData.append('Filedata', imgFile, 'image.png');

    return fetch(IMG_UPLOAD_URL, {
      method: 'POST',
      body: formData,
      ...COMMON_OPTIONS,
    });
  }

  function insertNode(node) {
    const sel = (iframeEl?.contentWindow || window).getSelection();
    if (sel.rangeCount > 0) {
      const range = sel.getRangeAt(0);
      range.insertNode(node);
      range.collapse();
    }
  }

  async function handleOnPaste(event) {
    const clipboardItems = event.clipboardData.items;
    const items = [].slice.call(clipboardItems).filter(function (item) {
      // Filter the image items only
      return item.type.indexOf('image') !== -1;
    });
    if (items.length === 0) {
      return;
    }
    event.preventDefault();

    const tmpEl = document.createElement('span');
    tmpEl.innerText = '上传中...';
    insertNode(tmpEl);

    const item = items[0];
    const resp = await uploadImg(item.getAsFile());
    tmpEl.remove();
    if (resp) {
      const respText = await resp.text();
      // DISCUZUPLOAD|0|123456|0
      const imgId = respText.split('|')[2];

      const postTime = new Date().getTime() / 1000;
      // https://img02.hi-pda.com/forum/forumdata/cache/common.js
      // eslint-disable-next-line no-undef
      ajaxget(imgListAjaxUrlGenenter(postTime), 'imgattachlist');

      let imgEl = document.getElementById(`image_${imgId}`);
      let msCount = 0;
      const sleepTime = 100;
      while (!imgEl && msCount < TIMEOUT) {
        await sleep(sleepTime);
        msCount += sleepTime;
        imgEl = document.getElementById(`image_${imgId}`);
      }
      const imgElStr = `<img src="${imgEl.src}" aid="attachimg_${imgId}" border="0" alt="" width="${imgEl.width}" />`;

      if (iframeEl) {
        // https://img02.hi-pda.com/forum/forumdata/cache/post.js
        // eslint-disable-next-line no-undef
        insertText(imgElStr, false);
      } else {
        if (textAreaEl) {
          const insertStr = `[attachimg]${imgId}[/attachimg]`;
          // eslint-disable-next-line no-undef
          insertText(insertStr, insertStr.length, 0);
        }
      }
    }
  }

  function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }

  if (iframeEl) {
    iframeEl.contentDocument.body.addEventListener('paste', handleOnPaste);
  } else {
    if (textAreaEl) {
      textAreaEl.addEventListener('paste', handleOnPaste);
    }
  }
})();