{"size": 2154, "ext": "js", "max_stars_repo_path": "dist/ZenzaBlogPartsButton.user.js", "max_stars_repo_name": "takker99/ZenzaWatch-old", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "dist/ZenzaBlogPartsButton.user.js", "max_issues_repo_name": "takker99/ZenzaWatch-old", "max_issues_repo_issues_event_min_datetime": "2020-08-13T14:30:55.000Z", "max_issues_repo_issues_event_max_datetime": "2020-08-13T14:31:30.000Z", "max_forks_repo_path": "dist/ZenzaBlogPartsButton.user.js", "max_forks_repo_name": "takker99/ZenzaWatch-old", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 24.202247191, "max_line_length": 57, "alphanum_fraction": 0.5385329619}
// ==UserScript==
// @name           ZenzaBlogPartsButton
// @namespace      https://github.com/segabito/
// @description    ニコニコ動画のブログパーツにZenzaWatch起動用ボタンを追加
// @match          *://ext.nicovideo.jp/thumb/*
// @grant          none
// @author         segabito macmoto
// @license        public domain
// @version        0.0.3
// ==/UserScript==
/* eslint-disable */


(window => {
  const addStyle = (styles, id) => {
    const elm = document.createElement('style');
    elm.type = 'text/css';
    if (id) { elm.id = id; }
    elm.append(styles);
    document.head.append(elm);
    return elm;
  };

  const postMessage = (type, message, token) => {
    const origin = document.referrer;
    const {command, watchId} = message;
    try {
          parent.postMessage(JSON.stringify({ // 互換のため冗長
          id: 'ZenzaWatch',
          type,
          token,
          body: {
            token,
            url: location.href,
            message: {command, watchId},
            command: 'message', params: {
              command, params: { watchId }
            }
          }
        }),
        origin);
    } catch (e) {
      alert(e);
      console.log('err', e);
    }
  };

  const __css__ = (`
    #zenzaButton {
      position: fixed;
      left: 0;
      top: 0;
      z-index: 10000;
      line-height: 24px;
      padding: 4px 4px;
      cursor: pointer;
      font-weight: bolder;
      display: none;
    }
    body:hover #zenzaButton {
      display: inline-block;
    }
  `).trim();

  const blogPartsApi = () => {
    const [watchId] = location.href.split('/').reverse();

    const [,,parentHost] = document.referrer.split('/');
    if (!parentHost.endsWith('.nicovideo.jp')) {
      window.console.log('disable bridge');
      return;
    }

    addStyle(__css__);
    const button = document.createElement('button');
    button.innerHTML = '<span>Zen</span>';
    button.id = 'zenzaButton';
    document.body.append(button);
    button.onclick = e => {
      postMessage('blogParts', {
        command: e.shiftKey ? 'send' : 'open',
        watchId
      });
    };
  };

  blogPartsApi();
})(globalThis ? globalThis.window : window);
