var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
  if (links[i].hostname != window.location.hostname) {
    links[i].target = '_blank';
    links[i].rel = 'noopener';
  } 
}

Minified one-line version:

d=document,ll=d.links;for(var i=0,l8=ll.length;i<l8;i++)ll[i].hostname!=window.location.hostname&&(ll[i].target="_blank",ll[i].rel="noopener");