Skip to content

Commit

Permalink
Fix total cards button bug + safely inject HTML
Browse files Browse the repository at this point in the history
Manually create new DOM elements to inject rather than use unsafe innerHTML
  • Loading branch information
Adamouization committed Aug 10, 2022
1 parent 28bba03 commit 35d8fe1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions content_scripts/create_total_cards_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
// Check if button already exists before creating it.
let buttonInstance = document.getElementById("button-total-cards-count");
if (buttonInstance === null) {
// Create HTML for button.
let numberOfCardsButtonHTML = '<a id="button-total-cards-count" class="board-header-btn board-header-btn-invite ' +
'board-header-btn-without-icon" title="Invite to board"><span class="board-header-btn-text">' +
'Total cards: <b>' + numberOfCards + '</b></span></a>'
// Build HTML button using native DOM manipulation methods.

// Create parent hyperlink tag with relevant classes.
let totalCardsCounterBtn = document.createElement("a");
totalCardsCounterBtn.setAttribute("id", "button-total-cards-count");
totalCardsCounterBtn.setAttribute("class", "board-header-btn board-header-btn-invite board-header-btn-without-icon");

// Create text span for total cards.
let totalCardsSpan = document.createElement("span");
totalCardsSpan.textContent = "Total Cards: ";
totalCardsSpan.setAttribute("class", "board-header-btn-text");

// Have total cards number be bold.
let boldText = document.createElement("b");
boldText.textContent = String(numberOfCards);

// Append children elements to parent element.
totalCardsSpan.append(boldText);
totalCardsCounterBtn.append(totalCardsSpan);

// Add button to Trello board.
document.getElementsByClassName("board-header-btns mod-left")[1].innerHTML += numberOfCardsButtonHTML;
document.getElementsByClassName("board-header-btns mod-left")[1].append(totalCardsCounterBtn);
} else {
// Do nothing.
}
Expand Down

0 comments on commit 35d8fe1

Please sign in to comment.