aboutsummaryrefslogtreecommitdiffhomepage
path: root/dev/Coq_Bugzilla_autolink.user.js
blob: 5ff618a83911243fef81f4ef2d20f3a3ad7bf799 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ==UserScript==
// @name        Coq Bugzilla autolink
// @namespace   SkySkimmer
// @include     https://github.com/coq/coq/*
// @description Makes BZ#XXXX into links to bugzilla for GitHub
// @version     1
// @grant       none
// ==/UserScript==

var regex = /BZ#(\d+)/g;
var substr = '<a href="https://coq.inria.fr/bugs/show_bug.cgi?id=$1">$&</a>';

function doTitle(node)
{
    node.innerHTML = node.innerHTML.replace(regex,substr);
}

function filter(node)
{
  if (node.nodeName == '#text')
    {
      return NodeFilter.FILTER_ACCEPT;
    }
  else if(node.nodeName == 'A')
    {
      return NodeFilter.FILTER_REJECT;
    }
  return NodeFilter.FILTER_SKIP;
}
var comments = document.getElementsByClassName("comment-body");

function doNode(parent)
{
    var nodes = document.createTreeWalker(parent,NodeFilter.SHOW_ALL,{ acceptNode : filter },false);
    var node;
    while(node=nodes.nextNode())
    {
      var content = node.textContent;
      var matches = regex.exec(content);

      if(matches && matches.length > 1)
      {
        var range = document.createRange();
        var start = content.search(regex);
        var end = start + matches[0].length;
        range.setStart(node, start);
        range.setEnd(node, end);
        var linkNode = document.createElement("a");
        linkNode.href = "https://coq.inria.fr/bugs/show_bug.cgi?id=" + matches[1];
        range.surroundContents(linkNode);

        //handle multiple matches in one text node
        doNode(linkNode.parentNode);
      }
    }
}

for(var i=0; i<comments.length; i++)
{
  doNode(comments[i]);
}

// usually 1 or 0 titles...
var titles = document.getElementsByClassName("js-issue-title");
for(var i=0; i<titles.length; i++)
{
    doTitle(titles[i]);
}