HJ-OTMOP / PTP @-Tack

// ==UserScript==
// @name         PTP @-Tack
// @namespace    http://tampermonkey.net/
// @version      0.1.3
// @description  Add an @-link to each user's avatar in the forum.
// @author       HJ-OTMOP
// @license MIT
// @match        https://passthepopcorn.me/*
// @exclude      https://passthepopcorn.me/*threadid=13617*
// @copyright 2018, HJ-OTMOP (https://openuserjs.org/users/HJ-OTMOP)
// ==/UserScript==

// Changelog 0.1.3 - Fixed error on mislabelling a property as a function
// Changelog 0.1.2 - Revised the @ links from hrefs to the user page to username text to integrate with PTP's notification feature

// Remove the exclude line above if you're not running the HJ Admin Toolkit


(function() {
    'use strict';
    var elements = document.querySelectorAll('.forum-post__heading a.username');
    for (let index = 0; index < elements.length; index++) {
        var userLink = elements[index].textContent;
        var ancestor = findAncestor(elements[index], 'forum-post');
        var image = ancestor.querySelector('.forum-post__avatar img');
        image.setAttribute('title', 'Reply to User');
        image.setAttribute('href', userLink);
        addListener(image);
    }
})();

function addListener(image) {
    image.addEventListener("click", function(event){
            var target = event.target || event.srcElement;
            insertQuickpostText("@" + target.getAttribute('href') + " – ");
        });
}

function findAncestor (el, cls) {
    while ((el = el.parentElement) && !el.classList.contains(cls));
    return el;
}

function insertQuickpostText(insert) {
   var qp = document.getElementById("quickpost");
   var caretPos = qp.selectionStart;
   var caretEnd = qp.selectionEnd;
   var textAreaTxt = qp.value;
   qp.value = (textAreaTxt.substring(0, caretPos) + insert + textAreaTxt.substring( caretEnd ) );
   qp.focus();
   document.getElementById('quickpost').selectionStart = caretPos + insert.length;
   document.getElementById('quickpost').selectionEnd = caretPos + insert.length;
}