From 307124b6f43deb0a2742950f243fa9fcad3b776e Mon Sep 17 00:00:00 2001 From: Masaki Kobayashi Date: Fri, 25 Aug 2023 00:43:35 +0900 Subject: [PATCH] Enable to switch between tabs in pages other than the Home --- dist/twitter-shortcuts.user.js | 17 +++++++---------- src/userscripts/meta/index.ts | 2 +- src/userscripts/twitter-shortcuts.user.ts | 16 ++++++---------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/dist/twitter-shortcuts.user.js b/dist/twitter-shortcuts.user.js index 2ec419a..385ea80 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.1.0 +// @version 0.2.0 // @description Refined shortcuts in Twitter for web // @author mkobayashime // @homepage https://github.com/mkobayashime/userscripts @@ -43,13 +43,10 @@ const config = {}; e.preventDefault(); } if (e.key === "[" || e.key === "]") { - if (!window.location.href.startsWith("https://twitter.com/home")) return; - const timelineTabs = Array.from( - document.querySelectorAll( - "[role='tablist'] a[href='/home'][role='tab']" - ) + const tabs = Array.from( + document.querySelectorAll("[role='tablist'] a[role='tab']") ); - const activeTabIndex = timelineTabs.findIndex( + const activeTabIndex = tabs.findIndex( (element) => element.ariaSelected === "true" ); if (activeTabIndex < 0) return; @@ -58,12 +55,12 @@ const config = {}; ? activeTabIndex === 0 ? -1 : activeTabIndex - 1 - : activeTabIndex === timelineTabs.length - 1 + : activeTabIndex === tabs.length - 1 ? -1 : activeTabIndex + 1; if (indexOfTabToClick < 0) return; - if (timelineTabs[indexOfTabToClick]) { - timelineTabs[indexOfTabToClick].click(); + if (tabs[indexOfTabToClick]) { + tabs[indexOfTabToClick].click(); } } }); diff --git a/src/userscripts/meta/index.ts b/src/userscripts/meta/index.ts index 85bf420..50352d8 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.1.0", + version: "0.2.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 6a67b23..4316ab1 100644 --- a/src/userscripts/twitter-shortcuts.user.ts +++ b/src/userscripts/twitter-shortcuts.user.ts @@ -30,15 +30,11 @@ const config = {}; } if (e.key === "[" || e.key === "]") { - if (!window.location.href.startsWith("https://twitter.com/home")) return; - - const timelineTabs = Array.from( - document.querySelectorAll( - "[role='tablist'] a[href='/home'][role='tab']" - ) + const tabs = Array.from( + document.querySelectorAll("[role='tablist'] a[role='tab']") ); - const activeTabIndex = timelineTabs.findIndex( + const activeTabIndex = tabs.findIndex( (element) => element.ariaSelected === "true" ); if (activeTabIndex < 0) return; @@ -48,13 +44,13 @@ const config = {}; ? activeTabIndex === 0 ? -1 : activeTabIndex - 1 - : activeTabIndex === timelineTabs.length - 1 + : activeTabIndex === tabs.length - 1 ? -1 : activeTabIndex + 1; if (indexOfTabToClick < 0) return; - if (timelineTabs[indexOfTabToClick]) { - timelineTabs[indexOfTabToClick].click(); + if (tabs[indexOfTabToClick]) { + tabs[indexOfTabToClick].click(); } } });