Skip to content

Commit f0eab56

Browse files
committed
Don't do so many DOM lookups
1 parent 7039597 commit f0eab56

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

popup.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ document.addEventListener('DOMContentLoaded', function () {
2121
let extRect = document.body.getBoundingClientRect();
2222
document.body.style.width = extRect.width.toString() + 'px';
2323

24+
let fontLabel = document.getElementById("fontLabel");
25+
let displayarea = document.getElementById("displayarea");
26+
let input = document.getElementById("code");
27+
let loader = document.getElementById("loader");
28+
let clipboardStatus = document.getElementById("clipboardstatus");
29+
2430
// The maximum extension dimensions are 800px by 600px. So we want the image
2531
// area to not make the whole extension larger than 600px tall, otherwise
2632
// unsightly scroll bars will appear.
27-
document.getElementById("displayarea").style.maxHeight = (600 - extRect.height).toString() + 'px';
33+
displayarea.style.maxHeight = (600 - extRect.height).toString() + 'px';
2834

2935
// Populate the selectors from server
3036
let font = document.getElementById("font");
@@ -45,10 +51,10 @@ document.addEventListener('DOMContentLoaded', function () {
4551
);
4652
});
4753
persistentOptions("font");
48-
document.getElementById("fontLabel").innerHTML = font_data[font.value]["formal"];
54+
fontLabel.innerHTML = font_data[font.value]["formal"];
4955

5056
// DPI
51-
let dpi_data = data["dpis"];
57+
var dpi_data = data["dpis"];
5258
$.each(dpi_data["options"], function (key, entry) {
5359
dpi.append(
5460
$('<option></option>').attr('value', key).text(entry)
@@ -58,7 +64,7 @@ document.addEventListener('DOMContentLoaded', function () {
5864
persistentOptions("DPI");
5965

6066
// Formats
61-
let format_data = data["formats"];
67+
var format_data = data["formats"];
6268
$.each(format_data["options"], function (key, entry) {
6369
format.append(
6470
$('<option></option>').attr('value', key).text(entry)
@@ -68,7 +74,7 @@ document.addEventListener('DOMContentLoaded', function () {
6874
persistentOptions("format");
6975

7076
// Displaystyle
71-
document.getElementById("displaystyle").checked = data["displaystyle"];
77+
displaystyle.checked = data["displaystyle"];
7278
persistentOptions("displaystyle");
7379
}).fail(function() {
7480
alert('TeX Math Here: popup.js: Cannot contact compilation server. Please try again later.');
@@ -77,8 +83,8 @@ document.addEventListener('DOMContentLoaded', function () {
7783
});
7884

7985
// Update the font dynamic name whenever the font selection changes.
80-
font.addEventListener("input", function(){
81-
document.getElementById("fontLabel").innerHTML = font_data[font.value]["formal"];
86+
font.addEventListener("input", function() {
87+
fontLabel.innerHTML = font_data[font.value]["formal"];
8288
});
8389

8490
// Keep option values/selections persistent after extension closes
@@ -111,11 +117,9 @@ document.addEventListener('DOMContentLoaded', function () {
111117
persistentOptions("code");
112118
persistentOptions("blobbed");
113119

114-
var input = document.getElementById("code");
115-
116120
function finishLoading(range) {
117-
document.getElementById("loader").style.display = "none";
118-
document.getElementById("displayarea").style.display = "block";
121+
loader.style.display = "none";
122+
displayarea.style.display = "block";
119123

120124
// Clears the selection so that nothing but what it selects next is selected on copy.
121125
var sel = window.getSelection();
@@ -124,7 +128,6 @@ document.addEventListener('DOMContentLoaded', function () {
124128
document.execCommand('Copy');
125129
sel.removeAllRanges();
126130

127-
var clipboardStatus = document.getElementById("clipboardstatus");
128131
if (browser == browser) {
129132
clipboardStatus.textContent = "(drag or right-click to copy)";
130133
} else {
@@ -149,7 +152,7 @@ document.addEventListener('DOMContentLoaded', function () {
149152
input.value = retrieved_latex;
150153
localStorage.setItem("code", retrieved_latex);
151154
}
152-
document.getElementById("code").select();
155+
input.select();
153156
});
154157

155158
// LISTENS FOR KEY COMBO TO CONVERT IMAGE -- SEE BELOW
@@ -175,13 +178,11 @@ document.addEventListener('DOMContentLoaded', function () {
175178
inputs.forEach(element => saveInputOptions(element["selectid"], element["input"]));
176179

177180
// Change user display
178-
document.getElementById("clipboardstatus").style.display = "none";
179-
let displayarea = document.getElementById("displayarea");
180-
let code = document.getElementById("code");
181+
clipboardstatus.style.display = "none";
181182
displayarea.style.display = "none";
182183
displayarea.textContent = '';
183184

184-
document.getElementById("loader").style.display = "block";
185+
loader.style.display = "block";
185186

186187
// Get values from user configuration
187188
var data = JSON.stringify({
@@ -202,6 +203,7 @@ document.addEventListener('DOMContentLoaded', function () {
202203
} else {
203204
postxhr.open("POST", server + '/post', true);
204205
}
206+
205207
postxhr.setRequestHeader("Content-Type", "application/json");
206208

207209
postxhr.onreadystatechange = function() {
@@ -210,18 +212,18 @@ document.addEventListener('DOMContentLoaded', function () {
210212
let range = document.createRange();
211213
if (format.value == "png" || format.value == "svg" || format.value == "gif") {
212214
// Image track
213-
let result = JSON.parse(postxhr.responseText);
215+
var result = JSON.parse(postxhr.responseText);
214216
var img = document.createElement('img');
215217
img.onload = function () {
216-
document.getElementById("loader").style.display = "none";
218+
loader.style.display = "none";
217219
img.alt = code.value;
218220
img.title = code.value;
219221

220222
if (!!document.getElementById('output')) {
221223
document.getElementById('output').remove();
222224
}
223225

224-
document.getElementById('displayarea').appendChild(img);
226+
displayarea.appendChild(img);
225227
range.selectNode(img);
226228
finishLoading(range);
227229
};
@@ -241,7 +243,7 @@ document.addEventListener('DOMContentLoaded', function () {
241243
par.id = 'output';
242244
par.className = "enable-select";
243245

244-
document.getElementById('displayarea').appendChild(par);
246+
displayarea.appendChild(par);
245247
range.selectNode(par);
246248
finishLoading(range);
247249
}

0 commit comments

Comments
 (0)