見出し画像

Misskey(とか)からXを称する事実上のTwitterをリダイレクトするChrome拡張

 Misskeyとかで役に立つChrome拡張作った。

 まぁ作ったと言っても、ChatGPTに書かせただけだが……

 Misskeyでメンションっぽい感じに、「@XXXXXXXXXX@x.com」って書く事が偶にあるけれど、それを「https://x.com/XXXXXXXXXX」にリダイレクトする拡張です。

 下記の4つのファイルをエディタかメモ帳なんか使って作ります。拡張機能の管理を開いて、デベロッパーモードにして、「パッケージ化されていない拡張機能を読み込む」で問題のフォルダを指定してやればOKです。

 一応、自分の環境だと動いてます。
 今の所、Misskey.ioと着ぐるみすきーだけで動きます。
 追加は頑張って考えるか、ChatGPTに頼みましょう。

manifest.json

{
  "manifest_version": 3,
  "name": "Misskey URL Redirector",
  "version": "1.0",
  "description": "Redirects specific Misskey URLs to their corresponding Twitter URLs.",
  "permissions": ["tabs", "webNavigation"],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": [
        "https://d.kg-misskey.net/*",
        "https://misskey.io/*"
      ],
      "js": ["content.js"],
      "run_at": "document_start"
    }
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

background.js

chrome.webNavigation.onCompleted.addListener(function(details) {
    const url = new URL(details.url);
    const host = url.hostname;
    const path = url.pathname;

    if (host === "d.kg-misskey.net" || host === "misskey.io") {
        const match = path.match(/@([^@]+)@x\.com/);
        if (match && match[1]) {
            const twitterHandle = match[1];
            const redirectUrl = `https://x.com/${twitterHandle}`;
            chrome.tabs.update(details.tabId, { url: redirectUrl });
        }
    }
}, { url: [{ urlMatches: "https://d.kg-misskey.net/.*" }, { urlMatches: "https://misskey.io/.*" }] });

content.js

// Empty script as we are handling the logic in background.js

popup.html

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Misskey URL Redirector</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h2>Misskey URL Redirector</h2>
    <p>特定のMisskey URLをX(旧Twitter)にリダイレクトします。</p>
    <p>この拡張機能は自動的に動作します。</p>
</body>
</html>

この記事が気に入ったらサポートをしてみませんか?