title: Implementing links in realtime chat
body: <p>So I'm making a realtime chat with socket.io and node.js, and I have the chat and everything working, but if somebody links a website in the chat, I want it to be an hyperlink automatically.</p>
  
  <p>I'm using autolinker.js to do this, and it's making the links as I want, but my problem is the way I output messages to avoid HTML injections.</p>
  
  <pre><code>for(var x = data.length - 1; x &gt;= 0; x--) {
      var autolinker = new Autolinker();
      var linkedText = autolinker.link(data[x].message);
      var message = document.createElement('div');
      var linkOfMessage;
      message.setAttribute('class', 'chat-message');
      //message.textContent = data[x].name + ': ';
  
      message.innerHTML = data[x].name + ': ' + linkedText;
  
      // Append
      messages.insertBefore(message, messages.firstChild);
      messages.appendChild(message);
      messages.scrollTop = messages.scrollHeight;
      }
  </code></pre>
  
  <p>So the message is getting handled correctly, but with innerHTML, they can basically use scripts inside my chat, which is bad. But</p>
  
  <pre><code>message.textContent = data[x].name + ': ' + linkedText;
  </code></pre>
  
  <p>Will just display my hyperlink in plain text without it being clickable, is there anyway to do this without setting the site at risk?</p>
  
  <p>I've spent a couple of hours looking around and couldn't fint anything related to this.</p>
  
  <p>Thanks in advance!</p>