From ac0d4227d45e8fba5c5d3ca12de9084be0d62af8 Mon Sep 17 00:00:00 2001 From: Masaki Kobayashi Date: Sat, 14 Oct 2023 01:34:05 +0900 Subject: [PATCH] Use more general logic and support the new UI --- dist/slack-no-autofocus.user.js | 39 ++++++-------------- src/userscripts/meta/index.ts | 2 +- src/userscripts/slack-no-autofocus.user.ts | 42 ++++++---------------- 3 files changed, 22 insertions(+), 61 deletions(-) diff --git a/dist/slack-no-autofocus.user.js b/dist/slack-no-autofocus.user.js index edb465c..c3aec35 100644 --- a/dist/slack-no-autofocus.user.js +++ b/dist/slack-no-autofocus.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Slack - No autofocus in moving channels // @namespace mkobayashime -// @version 1.2.2 +// @version 2.0.0 // @description Disable autofocus to the message input field after moved to another channel // @author mkobayashime // @homepage https://github.com/mkobayashime/userscripts @@ -13,32 +13,15 @@ // @grant none // ==/UserScript== -const waitForChannelNameWrapper = async () => - new Promise((resolve) => { - const timeout = window.setTimeout(() => { - throw new Error("Timeout: Getting channel name wrapper"); - }, 30000); - const interval = window.setInterval(() => { - const channelNameWrapper = document.querySelector( - ".p-workspace__primary_view" - ); - if (channelNameWrapper) { - resolve(channelNameWrapper); - window.clearInterval(interval); - window.clearTimeout(timeout); - } - }, 200); +void (async () => { + let curTitle = ""; + new MutationObserver(() => { + if (document.title === curTitle) return; + curTitle = document.title; + if (!(document.activeElement instanceof HTMLElement)) return; + document.activeElement.blur(); + }).observe(document.head, { + subtree: true, + childList: true, }); -(async function () { - const channelNameWrapper = await waitForChannelNameWrapper(); - new MutationObserver((e) => { - const oldAriaLabel = e[0].oldValue; - if (!(e[0].target instanceof HTMLElement)) return; - const currentAriaLabel = e[0].target.ariaLabel; - if (!oldAriaLabel || !currentAriaLabel) return; - if (oldAriaLabel !== currentAriaLabel) { - if (!(document.activeElement instanceof HTMLElement)) return; - document.activeElement.blur(); - } - }).observe(channelNameWrapper, { attributeOldValue: true }); })(); diff --git a/src/userscripts/meta/index.ts b/src/userscripts/meta/index.ts index 5c3a25e..60e2f48 100644 --- a/src/userscripts/meta/index.ts +++ b/src/userscripts/meta/index.ts @@ -158,7 +158,7 @@ export const meta: { [name: string]: UserScriptMeta | undefined } = { icon: "https://www.google.com/s2/favicons?sz=64&domain=slack.com", match: "https://app.slack.com/client/*", name: "Slack - No autofocus in moving channels", - version: "1.2.2", + version: "2.0.0", }, "slack-no-unintended-reload": { description: "Alert when you reload/close Slack with a new draft", diff --git a/src/userscripts/slack-no-autofocus.user.ts b/src/userscripts/slack-no-autofocus.user.ts index db14814..16d1836 100644 --- a/src/userscripts/slack-no-autofocus.user.ts +++ b/src/userscripts/slack-no-autofocus.user.ts @@ -1,37 +1,15 @@ -const waitForChannelNameWrapper = async (): Promise => - new Promise((resolve) => { - const timeout = window.setTimeout(() => { - throw new Error("Timeout: Getting channel name wrapper"); - }, 30000); +void (async () => { + let curTitle = ""; - const interval = window.setInterval(() => { - const channelNameWrapper = document.querySelector( - ".p-workspace__primary_view" - ); + new MutationObserver(() => { + if (document.title === curTitle) return; - if (channelNameWrapper) { - resolve(channelNameWrapper); + curTitle = document.title; - window.clearInterval(interval); - window.clearTimeout(timeout); - } - }, 200); + if (!(document.activeElement instanceof HTMLElement)) return; + document.activeElement.blur(); + }).observe(document.head, { + subtree: true, + childList: true, }); - -(async function () { - const channelNameWrapper = await waitForChannelNameWrapper(); - - new MutationObserver((e) => { - const oldAriaLabel = e[0].oldValue; - - if (!(e[0].target instanceof HTMLElement)) return; - const currentAriaLabel = e[0].target.ariaLabel; - - if (!oldAriaLabel || !currentAriaLabel) return; - - if (oldAriaLabel !== currentAriaLabel) { - if (!(document.activeElement instanceof HTMLElement)) return; - document.activeElement.blur(); - } - }).observe(channelNameWrapper, { attributeOldValue: true }); })();