Greasy Fork

Greasy Fork is available in English.

Mastodonに引用ボタンを追加する

MastodonのDeck UIにtootを引用するためのURLコピーボタンをboostボタンの隣に追加します。

当前为 2024-05-26 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// SPDX-License-Identifier: MIT
// ==UserScript==
// @name         Mastodonに引用ボタンを追加する
// @namespace    http://www.eniehack.net/~eniehack/works/firefox-userscripts
// @version      0.1.3
// @description  MastodonのDeck UIにtootを引用するためのURLコピーボタンをboostボタンの隣に追加します。
// @author       eniehack
// @license      MIT
// @match        https://fedibird.com/deck/*
// @match        https://best-friends.chat/deck/*
// @match        https://mstdn.jp/deck/*
// @match        https://pawoo.net/deck/*
// @grant        none
// ==/UserScript==

const INTERVAL = 1000;

const insertBefore = (newNode, existingNode) => {
    existingNode.parentNode.insertBefore(newNode, existingNode.previousSibling);
};

const createQuoteButtonElement = () => {
  const qtBtn = document.createElement("button");
  qtBtn.setAttribute("class", "status__action-bar__button icon-button quote-icon");
  qtBtn.setAttribute("type", "button");
  qtBtn.setAttribute("style", "font-size: 18px; width: 23.1429px; height: 23.1429px; line-height: 18px;");
  qtBtn.setAttribute("aria-label", "quote");
  qtBtn.setAttribute("aria-hidden", "false");
  qtBtn.setAttribute("title","quote");
  qtBtn.setAttribute("tabindex","0");
  const quoteIcon = document.createElement("i");
  quoteIcon.setAttribute("class", "fa fa-quote-left");
  quoteIcon.setAttribute("aria-hidden", "true");
  qtBtn.appendChild(quoteIcon);
  return qtBtn;
};

const fetchPostId = (target) => {
  return target.querySelector("a.status__relative-time").href.match(/\/[0-9]+$/)[0].substring(1);
};

const generateText = (url) =>  {
  return `QT: ${url}`;
};

const copyText = (text) => {
  navigator.clipboard.writeText(text);
};

const insertQuoteButton = (targetArticle) => {
  const target = targetArticle.querySelector(".status__action-bar__button.star-icon.icon-button");
  const qtbtn = createQuoteButtonElement();
  // console.log(targetArticle)
  const postId = fetchPostId(targetArticle);
  const textareaElem = document.querySelector("textarea.autosuggest-textarea__textarea");
  qtbtn.onclick = () => {
    fetch(`https://${location.host}/api/v1/statuses/${postId}`)
      .then(res => res.json())
      .then(json => json.url)
      .then(url => generateText(url))
      .then(txt => copyText(txt));
  };
  insertBefore(qtbtn, target);
};

let timer;

timer = setInterval(() => {
    const articles = document.getElementsByTagName("article");
    for (const article of articles) {
      if (1 <= article.querySelectorAll(".quote-icon").length) continue;
      insertQuoteButton(article);
      // console.log("insertQuoteButton");
    }
  }, INTERVAL);

document.removeEventListener("unload", () => {
  clearInterval(timer);
});