]> src.twobees.de Git - tampermonkeyscripts.git/blob - AzureDevOpsChangesetCommentSearch.js
add update urls
[tampermonkeyscripts.git] / AzureDevOpsChangesetCommentSearch.js
1 // ==UserScript==
2 // @name         ADS changeset comment search
3 // @version      0.1
4 // @description  Places a searchbox somewhere to search commit messages
5 // @author       Tobias Sachs
6 // @match        https://ads/*
7 // @grant        none
8 // @updateURL    https://src.twobees.de/?p=tampermonkeyscripts.git;a=blob_plain;f=AzureDevOpsChangesetCommentSearch.js;hb=HEAD
9 // @downloadURL  https://src.twobees.de/?p=tampermonkeyscripts.git;a=blob_plain;f=AzureDevOpsChangesetCommentSearch.js;hb=HEAD
10 // ==/UserScript==
11
12 /* jshint esversion:6 */
13 (function() {
14     'use strict';
15     let timerId;
16     let timerSuche;
17     let pollUrl = 'https://ads/HeBa/Entwicklung/_apis/tfvc/changesets?$top=1000?maxCommentLength=500';
18     let skipParm = '&$skip=';
19     let changeSetUrl = `/HeBa/Entwicklung/_versionControl/changeset/`;
20
21     let totalReceived = 0;
22     let matchesFound = 0;
23
24     let suchWasDelayed = function(){
25         console.debug("blub");
26         if (timerSuche) {
27             clearTimeout(timerSuche);
28         }
29         timerSuche = setTimeout( () =>
30             {
31                 timerSuche = undefined;
32                 suchWas();
33             }, 200);
34     };
35
36     let requestNext = function(skipCnt){
37         console.debug("get next. skip: "+ skipCnt);
38         let xhr = new XMLHttpRequest();
39         xhr.onload = () => searchItems(xhr);
40         xhr.onerror = function (e){
41             console.info(`poll error: ${e.type}: ${e.loaded} bytes transferred\n` + JSON.stringify(e));
42         };
43         xhr.open('GET', pollUrl + skipParm + skipCnt, true);
44         xhr.send();
45     };
46
47     let searchItems = function(xhr){
48
49         console.info("Die daten sind da...");
50         if (xhr.status !== 200){
51             console.info("poll failed: " + xhr.statusText);
52             return;
53         }
54         let s = document.getElementById('suchBox').value;
55         let resultDiv = document.getElementById('suchErg');
56         let statsSpan = document.getElementById('suchStats');
57
58         var d = JSON.parse(xhr.responseText);
59         var regex = new RegExp("("+s+")", 'gi');
60         totalReceived += d.value.length;
61         for (let e in d.value){
62             let cs = d.value[e];
63
64             if (cs.comment && regex.test(cs.comment)){
65                 matchesFound++;
66                 let comment = cs.comment.replace(regex, `<span style="background: orange;">$1</span>`);
67                 let item =
68                     `<div style='margin: 2px;' >`
69                     + `<a href='` + changeSetUrl
70                     + cs.changesetId + `'>` + cs.changesetId + '</a>'
71                     + ": " + comment + " (" + cs.author.displayName + ")"
72                     + `</div>`;
73                 resultDiv.innerHTML += item;
74             }
75         }
76         statsSpan.innerHTML = "matches: " + matchesFound +" searched comments: " + totalReceived;
77         if (d.value.length > 0)
78         {
79             requestNext(totalReceived);
80         }
81         else
82         {
83             statsSpan.innerHTML += " -- All comments searched.";
84         }
85     };
86
87     let suchWas = function (){
88         let s = document.getElementById('suchBox');
89         let results = document.getElementById('suchErg');
90         totalReceived = 0;
91         matchesFound = 0;
92         results.innerHTML = "";
93         document.getElementById('suchStats').innerHTML = "";
94         if (s.value.length < 3){
95             return;
96         }
97
98         requestNext(0);
99     };
100
101     let addSearch = () => {
102         if (timerId) {
103             clearTimeout(timerId);
104         }
105         observer.disconnect();
106
107         timerId = setTimeout(function() {
108             timerId = undefined;
109             if (window.location.href.includes("_apis")){
110                 return;
111             }
112             if (window.location.href.includes("_sprints")){
113                 // macht sonst irgendwie das man im sprintboard nix anklicken kann ¯\_(ツ)_/¯
114                 return;
115             }
116             let search = document.getElementById('supersearch');
117             if (!search){
118                 let html = `<div id="supersearch" style="display: block;position: absolute; left: 450px; top: 0px; height: auto; z-index: 9999;" >
119                               <input id="suchBox" placeholder="search commit" />
120                               <span id="suchStats"> </span>
121                               <div id="suchErg" style="max-height: 500px; background:#CCCCCC; overflow: auto;" ></div>
122                             </div>`;
123
124                 document.body.insertAdjacentHTML('afterbegin', html);
125                 let s = document.getElementById('suchBox');
126                 s.addEventListener("keyup", suchWasDelayed);
127             }
128
129             // keep watching for changes
130             observer.observe(document, { subtree: true, childList: true, characterData: true });
131         }, 500);
132     };
133
134     const observer = new MutationObserver(function() {
135         addSearch();
136     });
137
138     addSearch();
139
140 })();