X from search

Web版Xのユーザープロフ画面にその人の発言(from:@[screenname])を検索する窓を追加

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         X from search
// @namespace    https://spfx.jp/
// @version      2024-05-18
// @license      MIT
// @description  Web版Xのユーザープロフ画面にその人の発言(from:@[screenname])を検索する窓を追加
// @author       https://spfx.jp/
// @match        https://twitter.com/*
// @match        https://x.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant        none
// ==/UserScript==

(function() {
  'use strict';
  setInterval(() => {
    const userActions = document.querySelector('button[data-testid="userActions"');
    if (! userActions) {
      return;
    }

    let screenName = document.body.innerHTML.match(/<span[^>]+>(@.*?)<\/span>/)[1];
    const searchInput = document.getElementById('from-search-input');

    if (searchInput) {
      if (searchInput.value.includes(screenName)) {
        return;
      } else {
        searchInput.value = `from:${screenName} `;
        return;
      }
    }

    const searchDiv = document.createElement('div');
    searchDiv.setAttribute('style', 'margin: 1rem;');
    searchDiv.innerHTML = `<form action="/search"><input id="from-search-input" type="text" name="q" value="from:${screenName} "></form>`;
    userActions.parentNode.insertBefore(searchDiv, userActions);

    // NOTE: return後なのでsearchInputは使い回せない
    document.getElementById('from-search-input').addEventListener('focus', function() {
      const length = this.value.length;
      this.setSelectionRange(length, length);
    });

  }, 500);
})();