Add timeline link to questions and answers on Stack Exchange
Direct per-attribute DOM building belongs to the '00s but, since you're using ES2015's let, let's use multiline template strings and make the final result of our code instantly readable:

let question = document.getElementById("question");
if (question) {
    question.querySelector(".post-menu").insertAdjacentHTML('beforeend', `
        &lt;span&gt;
            &lt;a class="userscript-timeline" title="timeline for this question"
                href="/posts/${question.dataset.questionid}/timeline"&gt;timeline&lt;/a&gt;
        &lt;/span&gt;
    `);
}

for (let answer of document.getElementsByClassName("answer")) {
    answer.querySelector(".post-menu").insertAdjacentHTML('beforeend', `
        &lt;span&gt;
            &lt;a class="userscript-timeline" title="timeline for this answer"
                href="/posts/${answer.dataset.answerid}/timeline"&gt;timeline&lt;/a&gt;
        &lt;/span&gt;
    `);
}


Notes:


.getElementsByClassName("abc")[0] ———> .querySelector(".abc")
Of course the former is faster but it doesn't matter in our case.
getElementsByClassName can never be null, it's a live HTMLCollection, so we can directly use it in a loop.
insertAdjacentHTML speed is about the same or faster than manual node building.