Skip to content

Commit

Permalink
Merge pull request #252 from mkobayashime/twitter-like
Browse files Browse the repository at this point in the history
Like tweet in the center with `Ctrl-L`
  • Loading branch information
mkobayashime authored Oct 10, 2023
2 parents fabb08d + a38f2fb commit b3728d6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
29 changes: 28 additions & 1 deletion dist/twitter-shortcuts.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion src/userscripts/meta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions src/userscripts/twitter-shortcuts.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>(
"article[data-testid='tweet'][tabindex='-1']"
);
} else {
const tweetWrappers = Array.from(
document.querySelectorAll<HTMLElement>("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) => {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b3728d6

Please sign in to comment.