Skip to content

Commit

Permalink
Merge pull request #254 from mkobayashime/slack-no-autofocus
Browse files Browse the repository at this point in the history
Use more general logic and support the new UI
  • Loading branch information
mkobayashime authored Oct 17, 2023
2 parents b3728d6 + ac0d422 commit 7599e4d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
39 changes: 11 additions & 28 deletions dist/slack-no-autofocus.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 });
})();
2 changes: 1 addition & 1 deletion src/userscripts/meta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 10 additions & 32 deletions src/userscripts/slack-no-autofocus.user.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
const waitForChannelNameWrapper = async (): Promise<Element> =>
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 });
})();

0 comments on commit 7599e4d

Please sign in to comment.