-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll-conversation-into-view.user.js
138 lines (126 loc) · 3.37 KB
/
scroll-conversation-into-view.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ==UserScript==
// @name Scroll Conversation Into View
// @namespace https://spot.im/
// @version 0.6
// @description Scroll Conversation Into View
// @author dutzi
// @match http*://*/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
let clickCounter = 0;
let clickCounterResetTimeout;
let isScrolling;
let scrollingInterval;
let scrollingMessage;
let hasScrolledDown;
let hasAddedStyleTag;
function handleKeyDown(e) {
if (e.key?.toLowerCase() === "escape" && isScrolling) {
stopScrolling();
} else if (e.key?.toLowerCase() === "s") {
clearTimeout(clickCounterResetTimeout);
clickCounter++;
if (clickCounter === 3) {
clickCounter = 0;
if (isScrolling) {
stopScrolling();
} else {
startScrolling();
}
} else {
clickCounterResetTimeout = setTimeout(() => {
clickCounter = 0;
}, 500);
}
}
}
document.addEventListener("keydown", handleKeyDown);
function setScrollMessage(message) {
scrollingMessage.innerText =
"Scroll To Conversation" + (message ? ` (${message})` : "");
}
function setScrollColor(color) {
scrollingMessage.style.backgroundColor = color;
}
function addStyleTag() {
if (hasAddedStyleTag) {
return;
}
hasAddedStyleTag = true;
const style = document.createElement("style");
style.innerHTML = `
@keyframes spotim-scroll-to-comments-appear {
0% {
transform: scale(0.7);
opacity: 0;
}
90% {
transform: scale(1.02);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 1;
}
}
`;
document.head.appendChild(style);
}
function addScrollingMessage() {
addStyleTag();
scrollingMessage = document.createElement("div");
Object.assign(scrollingMessage.style, {
position: "fixed",
top: 0,
left: 0,
right: 0,
textAlign: "center",
background: "red",
color: "white",
fontWeight: "bold",
fontFamily: "Helvetica",
fontSize: "18px",
padding: "10px",
zIndex: 100000000000,
animation: "spotim-scroll-to-comments-appear 0.2s ease-out",
});
setScrollMessage();
setScrollColor("#467FDB");
document.body.appendChild(scrollingMessage);
}
function removeScrollingMessage() {
scrollingMessage.parentNode.removeChild(scrollingMessage);
}
function scrollDown() {
if (!hasScrolledDown) {
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
hasScrolledDown = true;
}
}
function startScrolling() {
addScrollingMessage();
isScrolling = true;
scrollingInterval = setInterval(() => {
let conversation;
conversation =
document.querySelector("[data-conversation-id]") ||
document.querySelector('[data-spotim-app="conversation"]');
if (conversation) {
conversation.scrollIntoView();
setScrollMessage("found! 😃");
setScrollColor("#4caf50");
} else {
scrollDown();
setScrollMessage("not found 😕 try scrolling up and down a bit");
setScrollColor("#f44336");
}
}, 100);
}
function stopScrolling() {
removeScrollingMessage();
clearInterval(scrollingInterval);
isScrolling = false;
}
})();