Skip to content

Commit 35d8fe1

Browse files
committed
Fix total cards button bug + safely inject HTML
Manually create new DOM elements to inject rather than use unsafe innerHTML
1 parent 28bba03 commit 35d8fe1

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

content_scripts/create_total_cards_button.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@
99
// Check if button already exists before creating it.
1010
let buttonInstance = document.getElementById("button-total-cards-count");
1111
if (buttonInstance === null) {
12-
// Create HTML for button.
13-
let numberOfCardsButtonHTML = '<a id="button-total-cards-count" class="board-header-btn board-header-btn-invite ' +
14-
'board-header-btn-without-icon" title="Invite to board"><span class="board-header-btn-text">' +
15-
'Total cards: <b>' + numberOfCards + '</b></span></a>'
12+
// Build HTML button using native DOM manipulation methods.
13+
14+
// Create parent hyperlink tag with relevant classes.
15+
let totalCardsCounterBtn = document.createElement("a");
16+
totalCardsCounterBtn.setAttribute("id", "button-total-cards-count");
17+
totalCardsCounterBtn.setAttribute("class", "board-header-btn board-header-btn-invite board-header-btn-without-icon");
18+
19+
// Create text span for total cards.
20+
let totalCardsSpan = document.createElement("span");
21+
totalCardsSpan.textContent = "Total Cards: ";
22+
totalCardsSpan.setAttribute("class", "board-header-btn-text");
23+
24+
// Have total cards number be bold.
25+
let boldText = document.createElement("b");
26+
boldText.textContent = String(numberOfCards);
27+
28+
// Append children elements to parent element.
29+
totalCardsSpan.append(boldText);
30+
totalCardsCounterBtn.append(totalCardsSpan);
31+
1632
// Add button to Trello board.
17-
document.getElementsByClassName("board-header-btns mod-left")[1].innerHTML += numberOfCardsButtonHTML;
33+
document.getElementsByClassName("board-header-btns mod-left")[1].append(totalCardsCounterBtn);
1834
} else {
1935
// Do nothing.
2036
}

0 commit comments

Comments
 (0)