Greasy Fork

Greasy Fork is available in English.

Twitterの共有リンクをMisskeyの共有リンクに置換するスクリプト

X(Twitter)の共有リンクを開いたときにMisskeyの共有リンクに置換するスクリプトです。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Twitterの共有リンクをMisskeyの共有リンクに置換するスクリプト
// @namespace       https://midra.me
// @version         1.0.8
// @description     X(Twitter)の共有リンクを開いたときにMisskeyの共有リンクに置換するスクリプトです。
// @author          Midra
// @license         MIT
// @match           https://twitter.com/*
// @match           https://x.com/*
// @icon            https://www.google.com/s2/favicons?sz=64&domain=x.com
// @run-at          document-start
// @noframes
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_registerMenuCommand
// @require         http://greasyfork.icu/scripts/7212-gm-config-eight-s-version/code/GM_config%20(eight's%20version).js?version=156587
// ==/UserScript==

void (() => {
  'use strict'

  const configInitData = {
    instance: {
      label: 'サーバー (httpsは省略)',
      type: 'text',
      default: 'misskey.io',
    },
    replace: {
      label: 'オプション',
      type: 'select',
      default: 'afterConfirm',
      options: {
        auto: '自動で置換する',
        afterConfirm: '置換する前に確認する',
      },
    },
  }

  GM_config.init(
    'X(Twitter)の共有リンクをMisskeyの共有リンクに置換するスクリプト 設定',
    configInitData
  )

  GM_config.onload = () => {
    setTimeout(() => {
      alert('設定を反映させるにはページを再読み込みしてください。')
    }, 200)
  }

  GM_registerMenuCommand('設定', GM_config.open)

  // 設定取得
  const config = Object.fromEntries(
    Object.keys(configInitData).map((key) => [key, GM_config.get(key)])
  )

  if (
    window.location.href.startsWith('https://x.com/intent/tweet') ||
    window.location.href.startsWith('https://twitter.com/intent/tweet') ||
    window.location.href.startsWith('https://x.com/share') ||
    window.location.href.startsWith('https://twitter.com/share')
  ) {
    const { text, url, hashtags, via } = Object.fromEntries(
      new URLSearchParams(window.location.search).entries()
    )

    let shareText = ''
    if (text) {
      shareText = text.replace(/@([a-zA-Z0-9_]+)/g, '?[@$1](https://x.com/$1)')
    }
    if (url) {
      shareText += ` ${url}`
    }
    if (hashtags) {
      shareText += ` #${hashtags.split(',').join(' #')}`
    }
    if (via) {
      shareText += ` ?[@${via}](https://x.com/${via})より`
    }

    if (
      config['replace'] === 'auto' ||
      window.confirm(
        `指定したMisskeyのサーバー(${config['instance']})で共有しますか?`
      )
    ) {
      window.location.href = `https://${
        config['instance']
      }/share?text=${window.encodeURIComponent(shareText)}`
    }
  }
})()