Skip to content

Commit 1983d02

Browse files
committed
only load custom.js if present
1 parent cab2da5 commit 1983d02

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

‎server/scripts/custom.sample.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
// it is intended to allow for customizations that do not get published back to the git repo
33
// for example, changing the logo
44

5-
// start running after all content is loaded
6-
document.addEventListener('DOMContentLoaded', () => {
5+
const customTask = () => {
76
// get all of the logo images
87
const logos = document.querySelectorAll('.logo img');
98
// loop through each logo
109
logos.forEach((elem) => {
1110
// change the source
1211
elem.src = 'my-custom-logo.gif';
1312
});
13+
};
14+
15+
// start running after all content is loaded, or immediately if page content is already loaded
16+
if (document.readyState === 'loading') {
17+
// Loading hasn't finished yet
18+
document.addEventListener('DOMContentLoaded', customTask);
19+
} else {
20+
// `DOMContentLoaded` has already fired
21+
customTask();
22+
}
23+
document.addEventListener('DOMContentLoaded', () => {
24+
1425
});

‎server/scripts/index.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import settings from './modules/settings.mjs';
99

1010
document.addEventListener('DOMContentLoaded', () => {
1111
init();
12+
getCustomCode();
1213
});
1314

1415
const categories = [
@@ -413,3 +414,15 @@ const fullScreenResizeCheck = () => {
413414
// store state of fullscreen element for next change detection
414415
fullScreenResizeCheck.wasFull = !!document.fullscreenElement;
415416
};
417+
418+
const getCustomCode = async () => {
419+
// fetch the custom file and see if it returns a 200 status
420+
const response = await fetch('scripts/custom.js', { method: 'HEAD' });
421+
if (response.ok) {
422+
// add the script element to the page
423+
const customElem = document.createElement('script');
424+
customElem.src = 'scripts/custom.js';
425+
customElem.type = 'text/javascript';
426+
document.body.append(customElem);
427+
}
428+
};

‎views/index.ejs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22

33
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
44

@@ -20,7 +20,6 @@
2020
<script type="text/javascript" src="resources/data.min.js?_=<%=production%>"></script>
2121
<script type="text/javascript" src="resources/vendor.min.js?_=<%=production%>"></script>
2222
<script type="text/javascript" src="resources/ws.min.js?_=<%=production%>"></script>
23-
<script type="text/javascript" src="scripts/custom.js?_=<%=production%>"></script>
2423
<% } else { %>
2524
<link rel="stylesheet" type="text/css" href="styles/main.css" />
2625
<script type="text/javascript" src="scripts/vendor/auto/jquery.js"></script>
@@ -45,14 +44,10 @@
4544
<script type="module" src="scripts/modules/radar.mjs"></script>
4645
<script type="module" src="scripts/modules/settings.mjs"></script>
4746
<script type="module" src="scripts/index.mjs"></script>
48-
<script type="text/javascript" src="scripts/custom.js"></script>
4947
<!-- data -->
5048
<script type="text/javascript" src="scripts/data/travelcities.js"></script>
5149
<script type="text/javascript" src="scripts/data/regionalcities.js"></script>
5250
<script type="text/javascript" src="scripts/data/stations.js"></script>
53-
54-
<script type="text/javascript" src="scripts/custom.js"></script>
55-
5651
<% } %>
5752

5853
</head>

0 commit comments

Comments
 (0)