0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Xの共有リンクから ?s=46&t=... を消すワンライナーとブックマークレット

0
Posted at

はじめに

チャットにポストのリンクを貼ろうとして、末尾の ?s=46&t=xxxx が毎回気になっていました。手で消すのが面倒になったので、消す道具を3つ用意した記録です。

前提

  • ポストの実体は https://x.com/<user>/status/<id> まで。? 以降は共有時に付く付加情報で、消してもポストは開けます
  • s= の数字の意味は公式に説明されていないので、ここでは「不要なクエリ」として一律に扱います

1. シェル(macOS / Linux)

# クリップボードのURLからクエリを落として戻す(macOS)
pbpaste | sed -E 's/\?.*$//' | pbcopy
# Linux (xclip)
xclip -o -sel clip | sed -E 's/\?.*$//' | xclip -sel clip

2. ブックマークレット(PCブラウザ)

javascript:(()=>{const u=new URL(location.href);u.search='';navigator.clipboard.writeText(u.toString()).then(()=>alert('copied: '+u))})()

ポストを開いた状態で実行すると、クエリなしURLがコピーされます。

3. Pythonで汎用化(他サービスのutm_*も落とす)

from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode
DROP = {"s", "t", "utm_source", "utm_medium", "utm_campaign", "si"}
def clean(url: str) -> str:
    p = urlsplit(url)
    q = [(k, v) for k, v in parse_qsl(p.query) if k not in DROP]
    return urlunsplit((p.scheme, p.netloc, p.path, urlencode(q), ""))
print(clean("https://x.com/a/status/1?s=46&t=abc"))  # https://x.com/a/status/1

全部消すのではなくブラックリスト方式にしているのは、YouTubeの ?v= のように消すと壊れるクエリがあるためです。最初に雑に全部消すスクリプトを書いて、動画リンクを壊したのが私の失敗でした。

まとめ

  • Xのリンクは ? 以降を消してOK
  • 汎用化するなら「消すキーを列挙」する方式が安全

スマホ(iPhone・Android)で3秒で消す手順は、別記事でまとめています。


スマホでの消し方や「t=」が気になる理由は、noteのほうに書きました。
https://note.com/sumaho_lab/n/n6cf93ce594b7

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?