DaaS in context-menu

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

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

// ==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 = -1;
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) => {
  console.log(res.responseText);
  if (JSON.parse(res.responseText).is_dajare === true) {
    is_dajare = true;
  } else {
    is_dajare = false;
  }
};

const check_score = (res) => {
  console.log(res.responseText);
  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",
    synchronous: true,
    url: api_uri + "?dajare=${t}",
    onload: check_is_dajare,
    timeout: 5000,
    ontimeout: () => {
      throw "Commection timeout!";
    },
  });
  GM.xmlHttpRequest({
    method: "GET",
    synchronous: true,
    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 ( is_dajare === -1 && score === -1 )  {
      throw "requestFailure";
    } if (confirm(make_dialog(is_dajare, t, score))) {
      GM_openInTab(make_tweet_url(is_dajare, t, score), false);
    }
  }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址