@@ -62,8 +62,12 @@ var tags = [
62
62
]
63
63
64
64
// Firstly, initialize the formatter, and its icons
65
- window . onload = function ( ) {
66
- document . getElementsByName ( "content" ) [ 0 ] . placeholder = "Click here to activate ScratchFormat" ;
65
+ // This is a 1 second timeout for page load, since I am
66
+ // too lazy to figure out real page load times
67
+ setTimeout ( function ( ) {
68
+ var textareaFinder = "[name=compose-comment],[name=content]" ;
69
+
70
+ document . querySelectorAll ( textareaFinder ) [ 0 ] . placeholder = "Click here to activate ScratchFormat" ;
67
71
68
72
formatter = document . createElement ( "div" ) ;
69
73
formatter . id = "formatter" ;
@@ -111,22 +115,23 @@ window.onload = function() {
111
115
112
116
// Move formatter if user clicks on textarea.
113
117
document . body . onclick = function ( event ) {
114
- if ( event . target . name == "content" ) {
118
+ if ( event . target . name == "content" || event . target . name == "compose-comment" ) {
115
119
event . target . parentElement . prepend ( formatter ) ;
120
+ formatter . style . width = event . target . style . width ;
116
121
}
117
122
}
118
123
119
124
// Initial background formatting loop.
120
125
setInterval ( function ( ) {
121
126
format ( ) ;
122
127
} , 300 ) ;
123
- }
128
+ } , 1000 ) ;
124
129
125
130
var oldComments = 0 ;
126
131
function format ( ) {
127
132
// Quit if we already formatted those comments.
128
133
// Checks for last vs new length.
129
- var comments = document . getElementsByClassName ( " content") ;
134
+ var comments = document . querySelectorAll ( ". content, .emoji-text ") ;
130
135
if ( oldComments == comments . length ) {
131
136
return ;
132
137
}
@@ -135,12 +140,16 @@ function format() {
135
140
136
141
for ( var c = 0 ; c < comments . length ; c ++ ) {
137
142
comments [ c ] . style . whiteSpace = "pre" ;
143
+ comments [ c ] . style . marginLeft = "5px" ;
138
144
comments [ c ] . innerHTML = parse ( comments [ c ] . innerHTML ) ;
139
145
}
140
146
}
141
147
142
148
// Custom regex parser. Easy to maintain.
143
149
function parse ( text ) {
150
+ // Note that the new scratchformat standard is [],
151
+ // and the () is outdated, and a bit harder to type.
152
+ // But, we will detect both for historical reasons
144
153
var startBracket = "[\\(|\\[]" ;
145
154
var endBracket = "[\\)|\\]]" ;
146
155
@@ -155,7 +164,8 @@ function parse(text) {
155
164
156
165
// If just 1 tag (Ex [br])
157
166
if ( tags [ t ] . fillers . length > 1 ) {
158
- regex += "(.*)" ;
167
+ // Regex statement to parse anything but (), []
168
+ regex += "([^\\)\\]\\[\\(]*)" ;
159
169
160
170
// Second part of tag
161
171
regex += startBracket ;
@@ -164,6 +174,7 @@ function parse(text) {
164
174
regex += endBracket ;
165
175
}
166
176
177
+ console . log ( regex ) ;
167
178
regex = new RegExp ( regex , "gm" ) ;
168
179
text = text . replace ( regex , tags [ t ] . formatter ( "$2" , "$3" ) ) ;
169
180
}
0 commit comments