-
Notifications
You must be signed in to change notification settings - Fork 1
/
autofillbranch.user.js
91 lines (78 loc) · 2.75 KB
/
autofillbranch.user.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ==UserScript==
// @name Azure DevOps - Branch Autofill
// @namespace http://nicruo.github.io
// @version 0.1.3
// @description Autofill the branch name on Azure DevOps
// @author nicruo
// @match https://*.visualstudio.com/*
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// ==/UserScript==
(function() {
'use strict';
let fields = {
'max-words': {
'label': 'Max Words',
'type': 'int',
'default' : 5
}
};
GM_config.init({
id: 'autofillbranch_config',
title: 'Configuration',
fields: fields
});
GM_registerMenuCommand("Configure Branch Autofill", () => {
GM_config.open();
});
let dialogOpen = false;
let task;
let type;
let branchInput;
let improvClass = "bowtie-symbol-color-palette";
let bugClass = "bowtie-symbol-bug";
let timerId = setInterval(() => {
if(checkForDialog()) {
if(!dialogOpen) {
dialogOpen = true;
let maxWords = GM_config.get('max-words');
let taskString = task.textContent.trim();
let typeString = hasClass(type, improvClass) ? "improv" : (hasClass(type, bugClass) ? "bug" : "pbi");
branchInput.value = typeString + "/" + taskString.replace(/[^\w\s]/gi, '').replace(/\s+/g,' ').split(/[ \u00A0]/g).splice(0, +maxWords).join("_");
triggerEvent(branchInput, 'keyup');
}
} else {
if(dialogOpen) {
dialogOpen = false;
branchInput = null;
task = null;
}
}}, 1000);
function checkForDialog() {
branchInput = document.querySelector('[placeholder="Enter your branch name"]');
if(branchInput !== null) {
task = document.querySelector("div.vc-create-branch-dialog div.la-primary-data");
type = document.querySelector("div.vc-create-branch-dialog div.la-primary-icon .bowtie-icon");
return task !== null;
}
return false;
}
function hasClass(element, className) {
return (' ' + element.className + ' ').indexOf(' ' + className+ ' ') > -1;
}
function triggerEvent(el, type) {
if ('createEvent' in document) {
// modern browsers, IE9+
var e = document.createEvent('HTMLEvents');
e.initEvent(type, false, true);
el.dispatchEvent(e);
} else {
// IE 8
let e = document.createEventObject();
e.eventType = type;
el.fireEvent('on'+e.eventType, e);
}
}
})();