summary refs log tree commit diff
path: root/content.js
blob: ab6931ebc8c444bdac63c9fdf38403f8710df23c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function sprongulate() {
  // Since GitHub is an SPA, we can't limit ourselves to only loading on
  // Nixpkgs PRs, as synthetic browser navigation doesn't trigger content
  // script loading. Thus, we have to load on any GitHub URL, rerun on
  // navigation, and ensure we're on a Nixpkgs PR before continuing.
  const [, NixOS, nixpkgs, pull, number] = new URL(window.location.href).pathname.split('/')
  if (NixOS !== 'NixOS' || nixpkgs !== 'nixpkgs' || pull !== 'pull') return

  // There are (at the time of writing) two contexts in which the PR
  // number is visible:
  //
  //   1. In the header (following the PR title).
  //   2. Sufficiently far down that the header is no longer visible (at
  //   which point the header shrinks and affixes itself to the top of
  //   the page).
  //
  // These are actually two distinct elements: the visibility of each
  // is dependent on your scroll position. They also have completely
  // different (and nondescript) CSS selectors, so instead of looking
  // for them directly, we locate the immediately preceding PR title
  // element (which *does* have a common CSS selector), to find them.
  for (let node of document.querySelectorAll('.js-issue-title')) {
    node = node.nextElementSibling
    const a = document.createElement('a')
    a.setAttribute('href', `https://nixpk.gs/pr-tracker.html?pr=${number}`)
    node.replaceWith(a)
    a.appendChild(node)
  }
}

// Once on first page load...
sprongulate()

// ...and once every time there is new page content.
document.addEventListener('turbo:render', sprongulate)