Debian Codegraph @

doc/_static/external_links.js

raw
1 // Open external links in a new browser tab.
2 //
3 // Any anchor pointing to a different host than the current site (e.g. the
4 // GitHub repository, contributor profiles, or release downloads) gets
5 // target="_blank" plus rel="noopener noreferrer" so it opens in a new tab
6 // without exposing the opener. Same-site links (including absolute
7 // https://colmap.github.io/ URLs) are left untouched. Covers both
8 // reStructuredText links and raw-HTML anchors (e.g. the landing page hero
9 // buttons), as well as anchors injected after load (e.g. the install
10 // selector's download links), which a MutationObserver picks up.
11 (function () {
12 "use strict";
13
14 function markExternal(anchor) {
15 if (anchor.hostname && anchor.hostname !== window.location.hostname) {
16 anchor.target = "_blank";
17 anchor.rel = "noopener noreferrer";
18 }
19 }
20
21 function scan(root) {
22 var anchors = root.querySelectorAll(
23 'a[href^="http://"], a[href^="https://"]'
24 );
25 anchors.forEach(markExternal);
26 }
27
28 document.addEventListener("DOMContentLoaded", function () {
29 scan(document);
30
31 // Re-scan anchors added after the initial load (e.g. the install
32 // selector rebuilds its output on every click).
33 var observer = new MutationObserver(function (mutations) {
34 mutations.forEach(function (mutation) {
35 mutation.addedNodes.forEach(function (node) {
36 if (node.nodeType !== Node.ELEMENT_NODE) return;
37 if (node.tagName === "A") markExternal(node);
38 if (node.querySelectorAll) scan(node);
39 });
40 });
41 });
42 observer.observe(document.body, { childList: true, subtree: true });
43 });
44 })();