-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappychat-floating-note.js
277 lines (244 loc) · 9.6 KB
/
happychat-floating-note.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// ==UserScript==
// @name Happychat Floating Note
// @namespace https://github.com/samiff/happychat-floating-note
// @version 1.2.1
// @description Creates a floating note entry for each Happychat.
// @author samifett
// @downloadURL none
// @grant none
// @match https://hud.happychat.io/*
// ==/UserScript==
// Script Options - Customize Me!
const options = {
NOTE_TOGGLE_BTN_TXT: '👻 Floating Note',
NOTE_TEMPLATE: '', // Customize a new note's starting text, or leave blank. Use "\n" for line breaks.
STYLE: {
NOTE_BACKGROUND_COLOR: '#fff59d',
NOTE_TEXT_COLOR: '#2e4453',
NOTE_WIDTH: '340px',
NOTE_FONT_SIZE: '16px'
},
USE_GRAMMARLY: false, // Changing this to true wiil enable Grammarly for note input, but there bugs with that.
LOCAL_STORAGE_KEY: 'hcfn_', // Locale storage key prefix
NS: 'hcfn-', // CSS namespace prefix
STORAGE_FLUSH_INTERVAL_MS: 86400000, // 24 hours
KEYBOARD_SHORTCUTS: {
// More info on shortcuts: https://github.com/samiff/happychat-floating-note/issues/1#issuecomment-481107336
USE_SHORTCUTS: false,
OPEN_CLOSE_NOTE_KEY: 70 // "F" key by default.
},
MANAGE_FOCUS: true, // When opening a note, obtain focus. When closing a note, returns focus to chat input.
};
// Script state
const state = {
activeChatId: null,
noteIsOpen: false,
noteRef: null,
noteInputRef: null
};
// Monkey patch history.pushState() so we can see URL changes in the SPA
const pushState = history.pushState;
history.pushState = function () {
pushState.apply(history, arguments);
handleNoteClose();
};
// Attach floating note element to page, attach listeners, and store element references
const floatingNoteHtml = `
<div class="${options.NS}note">
<div class="${options.NS}note__container-inner">
<textarea class="${options.NS}note__input" data-enable-grammarly="${options.USE_GRAMMARLY}"></textarea>
<div class="${options.NS}note__controls">
<div class="${options.NS}note__controls__control-container">
<button class="${options.NS}note__controls__control ${options.NS}note__controls__close">Close</button>
</div>
<div class="${options.NS}note__controls__control-container">
<button class="${options.NS}note__controls__control ${options.NS}note__controls__send">Send</button>
</div>
</div>
</div>
</div>
`;
document.body.insertAdjacentHTML('beforeend', floatingNoteHtml);
document.getElementsByClassName(`${options.NS}note__controls__close`)[0]
.onclick = handleNoteClose;
document.getElementsByClassName(`${options.NS}note__controls__send`)[0]
.onclick = window._.throttle(handleNoteSend, 500, { 'trailing': false });
state.noteRef = document.getElementsByClassName(`${options.NS}note`)[0];
state.noteInputRef = document.getElementsByClassName(`${options.NS}note__input`)[0];
// Attach keyboard shortcut listener
if (options.KEYBOARD_SHORTCUTS.USE_SHORTCUTS) {
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.shiftKey && e.keyCode === options.KEYBOARD_SHORTCUTS.OPEN_CLOSE_NOTE_KEY) {
e.preventDefault();
handleNoteToggleBtnClicked();
}
});
}
// Attach the note toggle button near the "+1" button
const attachNoteToggleBtnIntervalId = window.setInterval(() => {
if (document.getElementsByClassName('chat-list__title-bar')[0]) {
const noteToggleBtnHtml = `<button class="${options.NS}note-toggle-btn"`
+ `title="Click to toggle floating note for active chat">${options.NOTE_TOGGLE_BTN_TXT}</button>`;
document.getElementsByClassName('chat-list__title-bar')[0]
.insertAdjacentHTML('afterend', noteToggleBtnHtml);
document.getElementsByClassName(`${options.NS}note-toggle-btn`)[0]
.onclick = handleNoteToggleBtnClicked;
window.clearInterval(attachNoteToggleBtnIntervalId);
}
}, 150);
// Initialize localStorage and flushes it out periodically
function initializeLocalStorage() {
let storedChatIds = localStorage.getItem(`${options.LOCAL_STORAGE_KEY}chatIds`);
if (!storedChatIds) {
// Initialize local storage if it doesn't exist
localStorage.setItem(`${options.LOCAL_STORAGE_KEY}chatIds`, JSON.stringify({ resetTimestamp: Date.now() }));
} else {
storedChatIds = JSON.parse(storedChatIds);
if (Date.now() > (parseInt(storedChatIds.resetTimestamp) + options.STORAGE_FLUSH_INTERVAL_MS)) {
localStorage.setItem(`${options.LOCAL_STORAGE_KEY}chatIds`, JSON.stringify({ resetTimestamp: Date.now() }));
}
}
}
initializeLocalStorage();
// Opens and closes note for an active chat
function handleNoteToggleBtnClicked() {
if (!state.noteIsOpen) {
const chatId = getActiveChatId();
if (chatId) {
const noteData = getNoteDataByChatId(chatId);
state.noteInputRef.value = noteData.note;
state.noteRef.classList.toggle(`${options.NS}note--is-visible`);
state.noteIsOpen = !state.noteIsOpen;
state.activeChatId = chatId;
options.MANAGE_FOCUS && state.noteInputRef.focus();
}
} else {
handleNoteClose();
}
}
// Returns active chat ID from URL
function getActiveChatId() {
let chatId = null;
const path = location.pathname.split('/');
if (path[1] === 'chat' && path[2]) {
chatId = path[2];
}
return chatId;
}
// Returns stored note data
function getNoteDataByChatId(chatId) {
let noteData = null;
const storedChatIds = JSON.parse(localStorage.getItem(`${options.LOCAL_STORAGE_KEY}chatIds`));
if (storedChatIds.hasOwnProperty(chatId)) {
noteData = storedChatIds[chatId];
} else {
const username = document.querySelector('.user-data-panel__login input') ?
document.querySelector('.user-data-panel__login input').value : 'Not captured.'; // May not be in DOM yet
noteData = {
username,
createdAt: new Date(Date.now()).toLocaleString(),
note: options.NOTE_TEMPLATE,
};
handleNoteSave(chatId, noteData);
}
return noteData;
}
// Saves note data to browser's local storage
function handleNoteSave(chatId, noteData) {
const storedChatIds = JSON.parse(localStorage.getItem(`${options.LOCAL_STORAGE_KEY}chatIds`));
if (!storedChatIds.hasOwnProperty(chatId)) {
storedChatIds[chatId] = noteData;
} else {
storedChatIds[chatId].note = state.noteInputRef.value;
}
localStorage.setItem(`${options.LOCAL_STORAGE_KEY}chatIds`, JSON.stringify(storedChatIds));
}
// Notes are saved when they are closed
function handleNoteClose() {
if (state.noteIsOpen) {
const noteData = getNoteDataByChatId(state.activeChatId);
handleNoteSave(state.activeChatId, noteData);
state.noteRef.classList.remove(`${options.NS}note--is-visible`);
state.noteIsOpen = false;
state.activeChatId = null;
if (options.MANAGE_FOCUS) {
const happychatCompose = document.querySelector('.chat-actions__current-chat-action-compose textarea');
happychatCompose && happychatCompose.focus();
}
}
}
// Submits note data to an active chat
function handleNoteSend() {
const happychatCompose = document.querySelector('.chat-actions__current-chat-action-compose textarea');
if (happychatCompose) {
const noteMessage = '/note ' + state.noteInputRef.value;
happychatCompose.value = noteMessage;
const syntehticChangeEvent = new Event('input', { bubbles: true });
syntehticChangeEvent.simulated = true;
const syntehticKeyboardEvent = new KeyboardEvent('keydown', { bubbles: true, keyCode: 13 });
syntehticKeyboardEvent.simulated = true;
happychatCompose.dispatchEvent(syntehticChangeEvent);
happychatCompose.dispatchEvent(syntehticKeyboardEvent);
const storedChatIds = JSON.parse(localStorage.getItem(`${options.LOCAL_STORAGE_KEY}chatIds`));
storedChatIds[state.activeChatId] = undefined;
localStorage.setItem(`${options.LOCAL_STORAGE_KEY}chatIds`, JSON.stringify(storedChatIds));
state.noteRef.classList.remove(`${options.NS}note--is-visible`);
state.noteIsOpen = false;
state.activeChatId = null;
}
}
// Styles that are specific to Happychat Floating Note
const styles = `
<style type="text/css">
.${options.NS}note-toggle-btn {
margin: 0;
margin-bottom: 14px;
width: 100%;
}
.${options.NS}note {
position: fixed;
z-index: 9999;
top: 0;
right: -${options.STYLE.NOTE_WIDTH};
height: 100%;
width: ${options.STYLE.NOTE_WIDTH};
display: flex;
flex-direction: column;
justify-content: center;
transition: right .5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}
.${options.NS}note--is-visible {
right: 0;
}
.${options.NS}note__container-inner {
height: 95%;
display: flex;
flex-direction: column;
box-shadow: 0 1px 3px 0 rgba(60,64,67,0.302), 0 4px 8px 3px rgba(60,64,67,0.149);
}
.${options.NS}note__input {
flex: 1;
background: ${options.STYLE.NOTE_BACKGROUND_COLOR};
color: ${options.STYLE.NOTE_TEXT_COLOR};
font-size: ${options.STYLE.NOTE_FONT_SIZE};
padding: 12px;
resize: none;
border: none;
outline: none;
}
.${options.NS}note__controls {
display: flex;
background: ${options.STYLE.NOTE_BACKGROUND_COLOR};
}
.${options.NS}note__controls__control-container {
width: 50%;
padding: 8px 16px;
}
.${options.NS}note__controls__control {
width: 100%;
height: 100%;
}
</style>
`;
// Attach styles to <head>
document.head.insertAdjacentHTML('beforeend', styles);