Skip to content

Commit beefe8c

Browse files
committed
fix(dom): add null checks for element references to prevent runtime errors
1 parent 2913687 commit beefe8c

1 file changed

Lines changed: 72 additions & 38 deletions

File tree

src/scripts/main.js

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function handleBodyOnLoad() {
4545
// Load platform-specific username
4646
const platform = items.platform || 'github';
4747
const platformUsernameKey = `${platform}Username`;
48-
if (items[platformUsernameKey]) {
48+
if (platformUsernameElement && items[platformUsernameKey]) {
4949
platformUsernameElement.value = items[platformUsernameKey];
5050
}
5151

@@ -55,38 +55,44 @@ function handleBodyOnLoad() {
5555
if (items.gitlabToken && gitlabTokenElement) {
5656
gitlabTokenElement.value = items.gitlabToken;
5757
}
58-
if (items.projectName) {
58+
if (items.projectName && projectNameElement) {
5959
projectNameElement.value = items.projectName;
6060
}
61-
if (items.cacheInput) {
61+
if (items.cacheInput && cacheInputElement) {
6262
cacheInputElement.value = items.cacheInput;
6363
}
64-
if (items.endingDate) {
64+
if (items.endingDate && endingDateElement) {
6565
endingDateElement.value = items.endingDate;
6666
}
67-
if (items.startingDate) {
67+
if (items.startingDate && startingDateElement) {
6868
startingDateElement.value = items.startingDate;
6969
}
70-
if (items.showOpenLabel) {
71-
showOpenLabelElement.checked = items.showOpenLabel;
72-
} else if (items.showOpenLabel !== false) {
73-
// undefined
74-
showOpenLabelElement.checked = true;
75-
handleOpenLabelChange();
70+
if (showOpenLabelElement) {
71+
if (items.showOpenLabel) {
72+
showOpenLabelElement.checked = items.showOpenLabel;
73+
} else if (items.showOpenLabel !== false) {
74+
// undefined
75+
showOpenLabelElement.checked = true;
76+
handleOpenLabelChange();
77+
}
7678
}
7779

78-
if (items.yesterdayContribution) {
79-
yesterdayContributionElement.checked = items.yesterdayContribution;
80-
handleYesterdayContributionChange();
81-
} else if (items.yesterdayContribution !== false) {
82-
yesterdayContributionElement.checked = true;
83-
handleYesterdayContributionChange();
80+
if (yesterdayContributionElement) {
81+
if (items.yesterdayContribution) {
82+
yesterdayContributionElement.checked = items.yesterdayContribution;
83+
handleYesterdayContributionChange();
84+
} else if (items.yesterdayContribution !== false) {
85+
yesterdayContributionElement.checked = true;
86+
handleYesterdayContributionChange();
87+
}
8488
}
85-
if (items.showCommits) {
86-
showCommitsElement.checked = items.showCommits;
87-
} else {
88-
showCommitsElement.checked = false;
89-
handleShowCommitsChange();
89+
if (showCommitsElement) {
90+
if (items.showCommits) {
91+
showCommitsElement.checked = items.showCommits;
92+
} else {
93+
showCommitsElement.checked = false;
94+
handleShowCommitsChange();
95+
}
9096
}
9197
});
9298
}
@@ -103,21 +109,24 @@ document.getElementById('refreshCache').addEventListener('click', async (e) => {
103109
});
104110

105111
function handleStartingDateChange() {
112+
if (!startingDateElement) return;
106113
const value = startingDateElement.value;
107114
browser.storage.local.set({ startingDate: value });
108115
}
109116
function handleEndingDateChange() {
117+
if (!endingDateElement) return;
110118
const value = endingDateElement.value;
111119
browser.storage.local.set({ endingDate: value });
112120
}
113121

114122
function handleYesterdayContributionChange() {
123+
if (!yesterdayContributionElement) return;
115124
const value = yesterdayContributionElement.checked;
116125
const labelElement = document.querySelector("label[for='yesterdayContribution']");
117126

118127
if (value) {
119-
startingDateElement.readOnly = true;
120-
endingDateElement.readOnly = true;
128+
if (startingDateElement) startingDateElement.readOnly = true;
129+
if (endingDateElement) endingDateElement.readOnly = true;
121130
endingDateElement.value = getToday();
122131
startingDateElement.value = getYesterday();
123132
handleEndingDateChange();
@@ -145,6 +154,7 @@ function getToday() {
145154
}
146155

147156
function handlePlatformUsernameChange() {
157+
if (!platformUsernameElement) return;
148158
const value = platformUsernameElement.value;
149159
browser.storage.local.get(['platform']).then((result) => {
150160
const platform = result.platform || 'github';
@@ -153,54 +163,78 @@ function handlePlatformUsernameChange() {
153163
});
154164
}
155165
function handleGithubTokenChange() {
166+
if (!githubTokenElement) return;
156167
const value = githubTokenElement.value;
157168
browser.storage.local.set({ githubToken: value });
158169
}
159170
function handleGitlabTokenChange() {
171+
if (!gitlabTokenElement) return;
160172
const value = gitlabTokenElement.value;
161173
browser.storage.local.set({ gitlabToken: value });
162174
}
163175
function handleProjectNameChange() {
176+
if (!projectNameElement) return;
164177
const value = projectNameElement.value;
165178
browser.storage.local.set({ projectName: value });
166179
}
167180
function handleCacheInputChange() {
181+
if (!cacheInputElement) return;
168182
const value = cacheInputElement.value;
169183
browser.storage.local.set({ cacheInput: value });
170184
}
171185
function handleOpenLabelChange() {
186+
if (!showOpenLabelElement) return;
172187
const value = showOpenLabelElement.checked;
173188
const labelElement = document.querySelector("label[for='showOpenLabel']");
174189

175-
if (value) {
176-
labelElement.classList.add('selectedLabel');
177-
labelElement.classList.remove('unselectedLabel');
178-
} else {
179-
labelElement.classList.add('unselectedLabel');
180-
labelElement.classList.remove('selectedLabel');
190+
if (labelElement) {
191+
if (value) {
192+
labelElement.classList.add('selectedLabel');
193+
labelElement.classList.remove('unselectedLabel');
194+
} else {
195+
labelElement.classList.add('unselectedLabel');
196+
labelElement.classList.remove('selectedLabel');
197+
}
181198
}
182199

183200
browser.storage.local.set({ showOpenLabel: value });
184201
}
185202

186203
function handleShowCommitsChange() {
204+
if (!showCommitsElement) return;
187205
const value = showCommitsElement.checked;
188206
browser.storage.local.set({ showCommits: value });
189207
}
190208

191-
platformUsernameElement.addEventListener('keyup', handlePlatformUsernameChange);
209+
if (platformUsernameElement) {
210+
platformUsernameElement.addEventListener('keyup', handlePlatformUsernameChange);
211+
}
192212
if (githubTokenElement) {
193213
githubTokenElement.addEventListener('keyup', handleGithubTokenChange);
194214
}
195215
if (gitlabTokenElement) {
196216
gitlabTokenElement.addEventListener('keyup', handleGitlabTokenChange);
197217
}
198-
cacheInputElement.addEventListener('keyup', handleCacheInputChange);
199-
projectNameElement.addEventListener('keyup', handleProjectNameChange);
200-
startingDateElement.addEventListener('change', handleStartingDateChange);
201-
showCommitsElement.addEventListener('change', handleShowCommitsChange);
202-
endingDateElement.addEventListener('change', handleEndingDateChange);
203-
yesterdayContributionElement.addEventListener('change', handleYesterdayContributionChange);
204-
showOpenLabelElement.addEventListener('change', handleOpenLabelChange);
218+
if (cacheInputElement) {
219+
cacheInputElement.addEventListener('keyup', handleCacheInputChange);
220+
}
221+
if (projectNameElement) {
222+
projectNameElement.addEventListener('keyup', handleProjectNameChange);
223+
}
224+
if (startingDateElement) {
225+
startingDateElement.addEventListener('change', handleStartingDateChange);
226+
}
227+
if (showCommitsElement) {
228+
showCommitsElement.addEventListener('change', handleShowCommitsChange);
229+
}
230+
if (endingDateElement) {
231+
endingDateElement.addEventListener('change', handleEndingDateChange);
232+
}
233+
if (yesterdayContributionElement) {
234+
yesterdayContributionElement.addEventListener('change', handleYesterdayContributionChange);
235+
}
236+
if (showOpenLabelElement) {
237+
showOpenLabelElement.addEventListener('change', handleOpenLabelChange);
238+
}
205239

206240
document.addEventListener('DOMContentLoaded', handleBodyOnLoad);

0 commit comments

Comments
 (0)