DaaS in context-menu

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

目前為 2021-11-16 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    }
  }
})();