DaaS in context-menu

選択した文のダジャレ判定を行い、ツイートを作る項目を右クリックメニューに追加する

当前为 2021-11-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DaaS in context-menu
// @namespace    http://tampermonkey.net/
// @version      1.0
// @author       eggplants
// @homepage     https://github.com/eggplants
// @description  選択した文のダジャレ判定を行い、ツイートを作る項目を右クリックメニューに追加する
// @include      *
// @grant        GM_openInTab
// @grant        GM.xmlHttpRequest
// @run-at       context-menu
// @license      MIT
// ==/UserScript==

/*jshint esversion: 6 */

var is_dajare = false;
var score = -1;

const daas_site = "https%3A%2F%2Frits-dajare.github.io%2Fjudge";
const api_uri = "https://api.abelab.dev/daas/judge";

const check_is_dajare = (res) => {
  if (JSON.parse(res.responseText).is_dajare === true) {
    is_dajare = true;
  } else {
    is_dajare = false;
  }
};

const check_score = (res) => {
  var s = JSON.parse(res.responseText).score;
  if (typeof s === "number") {
    score = Math.round(s);
  } else {
    throw "returned score is not number!";
  }
};

const q = (text) => {
  const t = encodeURIComponent(text);
  GM.xmlHttpRequest({
    method: "GET",
    url: api_uri + "?dajare=${t}",
    onload: check_is_dajare,
    timeout: 5000,
    ontimeout: () => {
      throw "Commection timeout!";
    },
  });
  GM.xmlHttpRequest({
    method: "GET",
    url: api_uri + "?eval=${t}",
    onload: check_score,
    timeout: 5000,
    ontimeout: () => {
      throw "Commection timeout!";
    },
  });
};

const star = (n) => {
  return "★".repeat(n) + "☆".repeat(5 - n);
};

const make_dialog = (is_d, t, s) => {
  if (is_d) {
    return `「${t}」はダジャレ!\nスコア: ${star(s)}\nツイートする?`;
  } else {
    return `「${t}」はダジャレじゃない...\nツイートする?`;
  }
};

const make_tweet_url = (is_d, t, s) => {
  const et = encodeURIComponent(
    `ダジャレ: ${t}\n${is_d ? "スコア:" + star(s) : "ダジャレではありません"}`
  );
  return `https://twitter.com/intent/tweet?url=%0A${daas_site}&text=${et}&via=rits_dajare&hashtags=%E3%83%80%E3%82%B8%E3%83%A3%E3%83%AC%E5%88%A4%E5%AE%9A`;
};

(function () {
  "use strict";
  const t = window.getSelection().toString();
  if (t.length === 0) {
    alert("「ダジャレなんかなあ」と思った文を選択してくれ");
  } else {
    q(t);
    if (confirm(make_dialog(is_dajare, t, score))) {
      GM_openInTab(make_tweet_url(is_dajare, t, score), false);
    }
  }
})();