-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadertoy_comment_preview.user.js
77 lines (66 loc) · 2.56 KB
/
shadertoy_comment_preview.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
// ==UserScript==
// @name Shadertoy comment preview
// @namespace http://tampermonkey.net/
// @version 0.3.20190214
// @description Preview comments before submitting them
// @author Andrei Drexler
// @match https://www.shadertoy.com/view/*
// @run-at document-end
// @grant none
// ==/UserScript==
(function() {
'use strict';
let myComments = document.getElementById("myComments"),
commentTextArea = document.getElementById("commentTextArea"),
postButton = document.getElementById("myCommentButton");
if (!myComments || !commentTextArea || !postButton) {
return;
}
myComments.style.tabSize = 4; // for Fabrice :)
commentTextArea.style.resize = "vertical";
commentTextArea.style.minHeight = "4em";
commentTextArea.style.marginBottom = "12px";
let previewArea = document.createElement("div");
previewArea.style = "margin-top: 0px; margin-bottom: 12px; width: 100%; min-height: 3em; display: none;";
previewArea.className = "commentSelf";
previewArea.classList.add("commentContent");
commentTextArea.parentElement.insertBefore(previewArea, commentTextArea);
let previewButton = document.createElement("input");
previewButton.id = "myPreviewButton";
previewButton.type = "submit";
previewButton.style = "display:inline-block;right:95px;position:absolute;";
previewButton.className = "formButton";
postButton.parentElement.appendChild(previewButton);
let editing = true;
function updatePreview(set_focus) {
if (editing) {
previewButton.value = "Preview";
commentTextArea.style.display = "";
previewArea.style.display = "none";
if (set_focus) {
commentTextArea.focus();
}
} else {
previewButton.value = "Edit";
commentTextArea.style.display = "none";
previewArea.style.display = "inline-block";
previewArea.innerHTML = window.bbc2html(window.htmlEntities(commentTextArea.value),true);
if (set_focus) {
previewButton.focus();
}
}
}
let oldValidateComment = window.validateComment;
window.validateComment = function(form) {
let result = oldValidateComment(form);
editing = true;
updatePreview(true);
return result;
}
previewButton.onclick = function() {
editing = !editing || !window.checkFormComment(commentTextArea.value);
updatePreview(true);
return false;
};
updatePreview(false);
})();