Blomaga Image Paste

ブロマガにクリップボードから画像アップロード

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Blomaga Image Paste
// @namespace    https://github.com/segabito/
// @version      0.0.5
// @description  ブロマガにクリップボードから画像アップロード
// @author       segabito macmoto
// @match        *://ch.nicovideo.jp/tool/blomaga/edit?article_id=*
// @match        *://ch.nicovideo.jp/tool/blomaga/edit
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js
// @license      public domain
// @noframes
// ==/UserScript==

(function() {
  const monkey = function() {
    const $ = window.jQuery;
    const Blomaga = window.Blomaga;
    const Editor = Blomaga.Editor;

    const post = function(file) {
      const transfer = new Blomaga.EditorUploader.FileReadTransfer();
      const progressBar = new Blomaga.EditorUploader.ProgressBar();
      progressBar.setPercent(0);
      transfer.progress_bar = progressBar;
      progressBar.show();

      transfer.onProgress = (per) => {
        progressBar.setPercent(per);
      };

      transfer.onLoad = (resp) => {
        console.info('success: ', resp);
        progressBar.setPercent(100);
        progressBar.showSuccess();
        window.setTimeout(() => { progressBar.hide(); }, 1000);

        Blomaga.EditorSideBar.Image.addList(
          resp.src,
          resp.image_id,
          resp.width,
          resp.height,
          resp.csrf_token,
          resp.csrf_token_time
        );
        Editor.insertUploadImage(
          resp.image_id,
          resp.src,
          resp.width,
          resp.height
        );
      };

      transfer.onError = (json) => {
        progressBar.showError(json);
      };

      transfer.upload(file);
    };

    const onPaste = function(e) {
      let file;
      const items =
        (e.clipboardData || e.originalEvent.clipboardData).items;
      console.info('clipboard items', e, JSON.stringify(items));

      for (let i = 0, len = items.length; i < len; i++) {
        let item = items[i];
        if (item.type.match(/^image/i)) {
          file = item.getAsFile();
          break;
        }
      }
      if (!file) { return; }
      e.preventDefault();
      e.stopPropagation();
      post(file);
    };

    const initialize = function() {
      Editor.insertUploadImage =
        _.debounce(Editor.insertUploadImage.bind(Editor), 300);

      window.setTimeout(() => {
        $('iframe#wysiwyg_editor_ifr')
          .contents().find('html').on('paste', onPaste);
      }, 1000);
    };
    initialize();
  };


  const script = document.createElement('script');
  script.setAttribute('type', 'text/javascript');
  script.setAttribute('charset', 'UTF-8');
  script.appendChild(document.createTextNode('(' + monkey + ')()'));
  document.body.appendChild(script);
})();