From 519fca2e32d5cdd8511c0b6c8f0af508977d8636 Mon Sep 17 00:00:00 2001 From: Jesse Ashmore <29103230+JeeZeh@users.noreply.github.com> Date: Mon, 10 Aug 2020 20:47:36 +0100 Subject: [PATCH 1/2] Open external links in OSM notes in a new tab --- modules/ui/note_comments.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/ui/note_comments.js b/modules/ui/note_comments.js index 4ace9a2cb..81bafb6b1 100644 --- a/modules/ui/note_comments.js +++ b/modules/ui/note_comments.js @@ -68,7 +68,12 @@ export function uiNoteComments() { mainEnter .append('div') .attr('class', 'comment-text') - .html(function(d) { return d.html; }); + .html(function(d) { return d.html; }) + .selectAll('a') + .filter(isExternalLink) + .attr('rel', 'noopener') + .attr('rel', 'nofollow') + .attr('target', '_blank'); comments .call(replaceAvatars); @@ -110,6 +115,22 @@ export function uiNoteComments() { } + // A quick test for external links. 'this' is the node passed in by selection.filter() + function isExternalLink() { + try { + // Possibly more domains to be added + const internalDomains = ['.openstreetmap.org', '.osm.org']; + const hostname = new URL(this.href).hostname; + + // If the link's hostname comprises any internalDomains, return false + return internalDomains.findIndex((d) => hostname.includes(d)) === -1; + } catch (error) { + // If anything goes wrong, bail and assume not external (original behaviour) + return false; + } + } + + noteComments.note = function(val) { if (!arguments.length) return _note; _note = val; From 93c249fdd11721c1a30aabfce68822791a42503c Mon Sep 17 00:00:00 2001 From: Jesse Ashmore <29103230+JeeZeh@users.noreply.github.com> Date: Wed, 2 Sep 2020 21:13:53 +0100 Subject: [PATCH 2/2] Open all links externally and preserve attrs --- modules/ui/note_comments.js | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/modules/ui/note_comments.js b/modules/ui/note_comments.js index 81bafb6b1..b8ea4ba5c 100644 --- a/modules/ui/note_comments.js +++ b/modules/ui/note_comments.js @@ -70,9 +70,7 @@ export function uiNoteComments() { .attr('class', 'comment-text') .html(function(d) { return d.html; }) .selectAll('a') - .filter(isExternalLink) - .attr('rel', 'noopener') - .attr('rel', 'nofollow') + .attr('rel', 'noopener nofollow') .attr('target', '_blank'); comments @@ -115,22 +113,6 @@ export function uiNoteComments() { } - // A quick test for external links. 'this' is the node passed in by selection.filter() - function isExternalLink() { - try { - // Possibly more domains to be added - const internalDomains = ['.openstreetmap.org', '.osm.org']; - const hostname = new URL(this.href).hostname; - - // If the link's hostname comprises any internalDomains, return false - return internalDomains.findIndex((d) => hostname.includes(d)) === -1; - } catch (error) { - // If anything goes wrong, bail and assume not external (original behaviour) - return false; - } - } - - noteComments.note = function(val) { if (!arguments.length) return _note; _note = val;