Greasy Fork

来自缓存

Greasy Fork is available in English.

X (Twitter) - own tweets

Adds a link to users' pages to search for tweets only from them (no retweets without comment).

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name     X (Twitter) - own tweets
// @version  5
// @grant    none
// @require  https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @match    https://x.com/*
// @author   monnef
// @description Adds a link to users' pages to search for tweets only from them (no retweets without comment).
// @namespace   monnef.eu
// ==/UserScript==

// config
const debug = false;
const numberOfAttempts = 5;
const workInterval = 1000;
// end of config

const linkMarker = 'monnef--no-retweets';
const dLog = (...xs) => debug && console.log('[OwnTweets]', ...xs);
const state = { lastUrl: null, attempts: 0 };

const insertLink = (nameEl) => {
  if (nameEl.parent().parent().parent().parent().find(`.${linkMarker}`).length) return;
  const handle = nameEl.text();
  const linkEl = $("<a/>")
    .attr('href', `/search?q=from%3A%40${handle.slice(1)}&src=typed_query`)
    .text('[Own Tweets]')
    .addClass(linkMarker)
    .css({
      color: 'rgb(29, 161, 242)',
      marginLeft: '5px'
    })
  ;
  nameEl.parent().parent().after(linkEl);
};

const isHandleEl = (el) => el.length && el.text().startsWith('@');

const tryGetAndProcessNameEl = (x, y) => {
  const nameEl = $(document.elementFromPoint(x, y));
  const handle = nameEl.text();
  dLog('tryGetAndProcessNameEl', x, y, ';nameEl', nameEl, ';handle', handle);
  if (isHandleEl(nameEl)) {
    state.attempts = numberOfAttempts;
    insertLink(nameEl);
    return true;
  } else {
    return false;
  }
}

const work = () => {
  const curUrl = window.location.href;
  if (state.lastUrl === curUrl) {
    state.attempts++;
    if (state.attempts >= numberOfAttempts) {
    	dLog('work - url didn\'t change, skipping');
    	return;
    }
  } else {
    state.attempts = 0;
  }
  dLog('work', curUrl, state.lastUrl, state.attempts);
  state.lastUrl = curUrl;
  const handleEl = $('div[data-testid="UserName"]')
    .find('span')
    .filter((_, el) => $(el).text().trim().startsWith('@'));

  dLog('handleEl', handleEl);
  if (handleEl.length) {
    insertLink(handleEl);
  } else {
    dLog('failed to locate handleEl');
  }
}

$(() => setInterval(work, workInterval))