-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
41 lines (35 loc) · 1.35 KB
/
content.js
File metadata and controls
41 lines (35 loc) · 1.35 KB
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
let isContentEdited = false;
function checkTextAreaContent() {
var textArea = document.querySelector('#new_comment_field');
if (textArea) {
isContentEdited = textArea.value && textArea.value.length > 10;
console.log('Textarea checked. isContentEdited:', isContentEdited); // Debugging log
}
}
function attachListenerToTextArea() {
var textArea = document.querySelector('#new_comment_field');
if (textArea) {
textArea.addEventListener('input', checkTextAreaContent);
console.log('Listener attached to textarea'); // Debugging log
}
}
// Initial check and listener attachment
checkTextAreaContent();
attachListenerToTextArea();
window.addEventListener('beforeunload', function (e) {
if (isContentEdited) {
var confirmationMessage = 'It looks like you have been editing something. If you leave before saving, your changes will be lost.';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
});
// MutationObserver for dynamically added textarea (if needed)
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
attachListenerToTextArea();
checkTextAreaContent();
}
});
});
observer.observe(document.body, { childList: true, subtree: true });