Skip to content

Commit 8d2797f

Browse files
committed
Usage of DOM to create HTML
1 parent b53a4e7 commit 8d2797f

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

src/popup.ts

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,62 @@ function getAuthorNote (notif: Notification): string {
110110
return "";
111111
}
112112

113-
function genNotif (notif: NotifElm): string {
114-
return notif.authorNote && `
115-
<div class="new-notif">
116-
<a target="_blank" href="${notif.href}">
117-
<div class="notif-wrap">
118-
<img class="notif-img" src="${notif.imgSrc}">
119-
<p class="author-note">${notif.authorNote}</p>
120-
${notif.content && `<p class="notif-content">${notif.content}</p>`}
121-
<div class="notif-date">${notif.date}</div>
122-
</div>
123-
</a>
124-
${(() => {
125-
if (!notif.isComment) { return ""; }
126-
return `
127-
<div class="reply" programID="${notif.programID}" feedback="${notif.feedback}">
128-
<a class="reply-button">Reply</a>
129-
<textarea class="reply-text hide"></textarea>
130-
</div>`;
131-
})()}
132-
<div>`;
113+
function genNotif (notif: NotifElm): Node {
114+
let container = document.createElement("div")
115+
container.className = "new-notif";
116+
117+
let linkTag = document.createElement("a");
118+
linkTag.target = "_blank";
119+
linkTag.href = notif.href;
120+
121+
let wrapTag = document.createElement("div");
122+
wrapTag.className = "notif-wrap";
123+
124+
let imgTag = document.createElement("img");
125+
imgTag.className = "notif-img";
126+
imgTag.src = notif.imgSrc;
127+
128+
let noteTag = document.createElement("p");
129+
noteTag.className = "author-note";
130+
noteTag.innerText = notif.authorNote;
131+
132+
let contentTag = document.createElement("p");
133+
contentTag.className = "notif-content";
134+
contentTag.innerText = notif.content;
135+
136+
let dateTag = document.createElement("div");
137+
dateTag.className = "notif-date";
138+
dateTag.innerText = notif.date;
139+
140+
wrapTag.appendChild(imgTag);
141+
wrapTag.appendChild(noteTag);
142+
if (notif.content) { wrapTag.appendChild(contentTag); }
143+
wrapTag.appendChild(dateTag);
144+
linkTag.appendChild(wrapTag);
145+
container.appendChild(linkTag);
146+
147+
let replyTag = document.createElement("div");
148+
replyTag.className = "reply";
149+
replyTag.setAttribute("programID", notif.programID);
150+
replyTag.setAttribute("feedback", notif.feedback);
151+
152+
let replyButtonTag = document.createElement("a")
153+
replyButtonTag.className = "reply-button";
154+
replyButtonTag.innerText = "Reply";
155+
156+
let replyTextTag = document.createElement("textarea");
157+
replyTextTag.className = "reply-text hide";
158+
159+
if (notif.isComment) {
160+
replyTag.appendChild(replyButtonTag);
161+
replyTag.appendChild(replyTextTag);
162+
container.appendChild(replyTag);
163+
}
164+
165+
return container;
133166
}
134167

135-
function newNotif (notif: Notification): string {
168+
function newNotif (notif: Notification): Node {
136169
const notifToReturn: NotifElm = {
137170
href: `https://www.khanacademy.org/notifications/read?keys=${notif.urlsafeKey}&redirect_url=${notif.url || "/"}`,
138171
imgSrc: getImageSrc(notif),
@@ -218,10 +251,10 @@ function displayNotifs (notifJson: NotifObj) {
218251
notifJson.notifications.forEach((notif: Notification) => {
219252
if (notif.notes) {
220253
notif.notes.forEach((note: Notification) => {
221-
notifsContainer!.innerHTML += newNotif(note);
254+
notifsContainer!.appendChild(newNotif(note));
222255
});
223256
} else {
224-
notifsContainer!.innerHTML += newNotif(notif);
257+
notifsContainer!.appendChild(newNotif(notif));
225258
}
226259
});
227260
}

0 commit comments

Comments
 (0)