-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira.user.js
199 lines (173 loc) · 7.34 KB
/
jira.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// ==UserScript==
// @name JIRA Service Desk Severity Info
// @namespace http://github.com/mirogta/
// @version 0.0.6
// @description Add an explanation to severity levels on JIRA Service Desk issues
// @author mirogta
// @license MIT
// @homepageURL https://github.com/mirogta/tampermonkey-jira-severityinfo
// @match https://*.atlassian.net/browse/*
// @match https://*.atlassian.net/jira/servicedesk/*
// @match https://*.atlassian.net/secure/*
// @inject-into content
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @compatible firefox >=14
// @compatible chrome >=18
// ==/UserScript==
// ==OpenUserJS==
// @author mirogta
// ==/OpenUserJS==
(function() {
'use strict';
console.log(`Initialising JIRA Service Desk Tampermonkey Script`);
const configKeys = {
wikiUrl: 'configWikiUrlKey',
iframeUrl: 'configIframeUrlKey',
};
let preventDuplicate = false;
GM_addStyle(`
body._blur::after { background-color: #563d7c; content: ""; display: block; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 1000; opacity: 0.5; }
#_severity_info { z-index: 9999; position: absolute; }
#_severity_info:hover { cursor: pointer }
#_severity_info::before { content: " \\1F4AC"; padding: 10px }
#_severity_info span {
display: none;
position: absolute;
width: 880px;
background: #eee;
padding: 20px;
border-radius: 4px;
right: 100px;
height: 600px;
}
#_severity_info:hover span { display: inline }
#_severity_info iframe { width: 100%; height: 100%; background-color: #eee }
._severity_wrap { justify-content: left }
#_severity_description ul { list-style-type: none; padding-left: 0 }
._severity_url { display: block; width: 400px; margin: 10px 20px; border: 1px solid #ccc; }
`);
function ensureSeverityInfo() {
if(preventDuplicate === true || document.getElementById('_severity_info')) {
return;
}
preventDuplicate = true;
// delay loading, because JIRA elements are loading async
// and elements' positions are jumping up/down
setTimeout(addSeverityInfo, 4000);
}
function addSeverityInfo() {
// find the severity field
// NOTE: the DOM is obfuscated so we can't find it by element id or classname
// but can find it via this selector
const severityButton = document.body.querySelector('button[aria-label="Edit Severity"]');
if(null == severityButton) {
preventDuplicate = false;
return;
}
const severityLabel = Array.from(document.querySelectorAll('h2')).find(el => el.textContent === 'Severity');
if(null == severityLabel) {
preventDuplicate = false;
return;
}
const container = severityLabel.parentElement;
container.classList.add('_severity_wrap');
if(null == container) {
preventDuplicate = false;
return;
}
console.log(`- adding severity info`);
const severityLevel = parseInt(severityButton.parentElement.innerText, 0);
const infoEl = document.createElement('div');
infoEl.id = '_severity_info';
const descriptionEl = document.createElement('span');
descriptionEl.id = '_severity_description';
descriptionEl.dataset.severityLevel = severityLevel;
const bodyRect = document.body.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
const offsetTop = containerRect.top - bodyRect.top;
const offsetLeft = containerRect.left - bodyRect.left;
infoEl.style.top = `${offsetTop}px`;
infoEl.style.left = `${offsetLeft + severityLabel.offsetWidth}px`;
infoEl.append(descriptionEl);
document.body.append(infoEl);
infoEl.addEventListener('click', loadWiki, false);
infoEl.addEventListener('mouseover', function() {document.body.classList.add('_blur');}, false);
infoEl.addEventListener('mouseout', function() {document.body.classList.remove('_blur');}, false);
updateSeverityDescription();
}
function loadWiki() {
const wikiUrl = GM_getValue(configKeys.wikiUrl);
if(wikiUrl) {
window.open(wikiUrl, '_severity_link');
}
}
function updateSeverityDescription() {
const descriptionEl = document.getElementById('_severity_description');
if(null == descriptionEl) {
return;
}
const iframeUrl = GM_getValue(configKeys.iframeUrl);
if(iframeUrl) {
descriptionEl.innerHTML = `<iframe src="${iframeUrl}"></iframe>`;;
} else {
descriptionEl.innerHTML = 'Please configure the severity URLs in the Settings…';
}
}
function addSeveritySettings() {
// don't add the settings popup if already present
if(document.getElementById('_severity_config')) {
return;
}
const settingsPopup = Array.from(document.querySelectorAll('h3')).find(el => el.textContent === 'Settings');
const personalSettings = Array.from(document.querySelectorAll('div')).find(el => el.textContent === 'Personal settings');
if(settingsPopup && personalSettings) {
console.log(`- adding severity info settings`);
const container = settingsPopup.parentElement;
const configHeaderEl = document.createElement('div');
configHeaderEl.id = '_severity_config';
configHeaderEl.innerText = 'Severity Info URLs'
configHeaderEl.className = personalSettings.className;
const configEl = document.createElement('input');
configEl.className = '_severity_url';
configEl.placeholder = "Please enter Severity Levels wiki URL…";
configEl.dataset.configKey = configKeys.wikiUrl;
configEl.addEventListener('change', saveConfig, false);
const configIframeEl = document.createElement('input');
configIframeEl.className = '_severity_url';
configIframeEl.placeholder = "Please enter Severity Levels iframe URL…";
configIframeEl.dataset.configKey = configKeys.iframeUrl;
configIframeEl.addEventListener('change', saveConfig, false);
const content = GM_getValue(configKeys.wikiUrl);
if(content) {
console.log(`- found severity wiki URL: ${content}`);
configEl.value = content;
}
const iframeUrl = GM_getValue(configKeys.iframeUrl);
if(iframeUrl) {
console.log(`- found severity iframe URL: ${iframeUrl}`);
configIframeEl.value = iframeUrl;
}
container.append(configHeaderEl);
container.append(configEl);
container.append(configIframeEl);
}
}
function saveConfig(event) {
const content = event.target.value;
const key = event.target.dataset.configKey;
GM_setValue(key, content);
console.log(`- saved severity ${key}: ${content}`);
updateSeverityDescription();
}
let ob = new window.MutationObserver(function() {
console.debug(`- content changed`);
addSeveritySettings();
ensureSeverityInfo();
});
ob.observe(document, {
childList: true,
subtree: true,
});
})();