LoginSignup
3
2

More than 3 years have passed since last update.

いま見ているTwitterアカウントに限定してツイートを検索するブックマークレット

Posted at

はじめに

ブラウザ版Twitter用ブックマークレットです。
アカウントを指定してツイートを検索する時に、コピー&ペーストする手間をひとつ省く便利なやつです。

javascript:(()=>{const t=location.href.split("/");if(t[2].endsWith("twitter.com")&&t[3]){const o=t[3].split("?")[0],i=prompt("Search from @"+o,"");i&&(location.href="//twitter.com/search?f=live&q="+i+"%20from:"+o)}})();

使い方

  1. 検索対象とするTwitterアカウントのホーム、またはツイートのページを予め開いておく。
  2. ブックマークレットを実行。
  3. 入力画面が表示されるので、キーワードを入力する。

簡潔な説明

圧縮前のコードはこちら。

(
    () => {
// ----------

const path = location.href.split('/');

// twitter ID check
if (path[2].endsWith('twitter.com') && path[3]) {
    // id
    const id = path[3].split('?')[0];// no parameters
    // keywords
    const keywords = prompt('Search from @' + id, '');

    // search results
    if (keywords) {
        location.href = '//twitter.com/search?f=live&q=' + keywords + '%20from:' + id;
    }
}

// ----------
    }
)();

大体の流れは次の通りです。

  1. URLからtwitter IDを取得する => path[3]
  2. window.promptで検索キーワードを取得する => keywords
  3. 取得した値をもとに検索パラメータをセットする
  4. 組み立てたURLを開く

  • iPhone: Safari, Chromeで動作確認しました。
  • 以下の例のように、https://twitter.com/ にアカウントが続くURLでのみ実行可能です。

OK :smiley:
https://twitter.com/Qiita
https://twitter.com/Qiita/status/1351820856100487168

NG :confounded:
https://twitter.com
https://twitter.com/search?q=qiita

あとがき

今どきwindow.promptを使うのがちょっとアレなんですが、取り敢えず動けばOKです。

いつもお世話様です。
本日もありがとうございました。

参考

【簡単にできる】ブックマークレットのすゝめ【初心者向け】
https://qiita.com/len_crow/items/189603f2c5f462bb670c

Twitterの高度な検索
1. https://twitter.com/search-advanced?lang=ja
2. https://help.twitter.com/ja/using-twitter/twitter-advanced-search

3
2
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
3
2