diff --git a/dist/twitter-shortcuts.user.js b/dist/twitter-shortcuts.user.js index dff556d..465c2c3 100644 --- a/dist/twitter-shortcuts.user.js +++ b/dist/twitter-shortcuts.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Twitter - Shortcuts // @namespace mkobayashime -// @version 0.3.2 +// @version 0.4.0 // @description Refined shortcuts in Twitter for web // @author mkobayashime // @homepage https://github.com/mkobayashime/userscripts @@ -22,6 +22,24 @@ const isTyping = () => { }; const config = {}; +const findTweetInCenter = () => { + if (window.location.href.match(RegExp("^https://twitter.com/.*/status/"))) { + return document.querySelector( + "article[data-testid='tweet'][tabindex='-1']" + ); + } else { + const tweetWrappers = Array.from( + document.querySelectorAll("article[data-testid='tweet']") + ); + if (tweetWrappers.length === 0) return; + if (tweetWrappers.length === 1) return tweetWrappers[0]; + return tweetWrappers.find((element) => { + const windowHalfHeight = window.innerHeight / 2; + const { top, height } = element.getBoundingClientRect(); + return top <= windowHalfHeight && top + height >= windowHalfHeight; + }); + } +}; // eslint-disable-next-line no-empty-pattern (({}) => { document.body.addEventListener("keypress", (e) => { @@ -71,5 +89,14 @@ const config = {}; e.preventDefault(); window.open(`${tweetURLMatch[0]}/likes`); } + if (e.ctrlKey && e.key === "l") { + e.preventDefault(); + const targetTweet = findTweetInCenter(); + if (!targetTweet) return; + const likeButton = targetTweet.querySelector( + "[data-testid='like'][role='button'], [data-testid='unlike'][role='button']" + ); + if (likeButton instanceof HTMLElement) likeButton.click(); + } }); })(config); diff --git a/src/userscripts/meta/index.ts b/src/userscripts/meta/index.ts index a0f5b9d..48e9aa9 100644 --- a/src/userscripts/meta/index.ts +++ b/src/userscripts/meta/index.ts @@ -200,7 +200,7 @@ export const meta: { [name: string]: UserScriptMeta | undefined } = { icon: "https://www.google.com/s2/favicons?domain=twitter.com", match: "https://twitter.com/*", name: "Twitter - Shortcuts", - version: "0.3.2", + version: "0.4.0", }, "zoom-web-shortcuts": { description: "Google Meet-like Ctrl-d/e shortcuts in Zoom", diff --git a/src/userscripts/twitter-shortcuts.user.ts b/src/userscripts/twitter-shortcuts.user.ts index f140efe..03334a7 100644 --- a/src/userscripts/twitter-shortcuts.user.ts +++ b/src/userscripts/twitter-shortcuts.user.ts @@ -2,6 +2,28 @@ import { isTyping } from "./utils/isTyping"; const config = {}; +const findTweetInCenter = () => { + if (window.location.href.match(RegExp("^https://twitter.com/.*/status/"))) { + return document.querySelector( + "article[data-testid='tweet'][tabindex='-1']" + ); + } else { + const tweetWrappers = Array.from( + document.querySelectorAll("article[data-testid='tweet']") + ); + + if (tweetWrappers.length === 0) return; + if (tweetWrappers.length === 1) return tweetWrappers[0]; + + return tweetWrappers.find((element) => { + const windowHalfHeight = window.innerHeight / 2; + const { top, height } = element.getBoundingClientRect(); + + return top <= windowHalfHeight && top + height >= windowHalfHeight; + }); + } +}; + // eslint-disable-next-line no-empty-pattern (({}: typeof config) => { document.body.addEventListener("keypress", (e) => { @@ -63,6 +85,18 @@ const config = {}; e.preventDefault(); window.open(`${tweetURLMatch[0]}/likes`); } + + if (e.ctrlKey && e.key === "l") { + e.preventDefault(); + + const targetTweet = findTweetInCenter(); + if (!targetTweet) return; + + const likeButton = targetTweet.querySelector( + "[data-testid='like'][role='button'], [data-testid='unlike'][role='button']" + ); + if (likeButton instanceof HTMLElement) likeButton.click(); + } }); })(config);