-
Notifications
You must be signed in to change notification settings - Fork 0
/
JIRA_copy.user.js
406 lines (325 loc) · 11.1 KB
/
JIRA_copy.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// ==UserScript==
// @name Copy Anything for JIRA
// @namespace pbo
// @description JIRA - copy info to clipboard
// @include http://your.jira.example.com/browse/*
// @version 1.1.1
// @grant GM_registerMenuCommand
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js
// @require https://raw.githubusercontent.com/pbodnar/jquery.hotkeys/filterTextInputs-easy/jquery.hotkeys.js
// ==/UserScript==
(function($) {
var traceOn = true;
trace('starting...');
function showHelp() {
alert(`Copy Anything for JIRA - Help
Press 'D' to display the follow-up shortcuts / options to copy various parts of the page into the clipboard.
@author pbodnar
@version ${GM_info.script.version}
`);
}
try {
trace('command register');
GM_registerMenuCommand('Copy Anything for JIRA - Help', showHelp);
trace('command registered');
} catch (e) {
trace('command register error: ' + e);
}
// ==================== JIRA copy commands ====================
// TODO Refactor: Return just the extracted string instead... Plus make this easily re-usable for other pages as well.
function copyAsLinkFull() {
var issueKey = $('#key-val').text();
var summaryKey = $('#summary-val').text();
var res = '<a href="' + getPagePath() + '">' + issueKey + ' - ' + escapeHtml(summaryKey) + '</a>';
copyToClipboard(res);
}
function copyAsLinkWithKey() {
var issueKey = $('#key-val').text();
var res = '<a href="' + getPagePath() + '">' + issueKey + '</a>';
copyToClipboard(res);
}
function copyAsCommitMessage() {
var issueKey = $('#key-val').text();
var summaryKey = $('#summary-val').text();
var res = issueKey + ' - ' + escapeHtml(summaryKey) + ': ';
copyToClipboard(res);
}
// ==================== GUI ====================
var timer = null;
var commandsEnabled = false;
function showCommandsOverlay(event) {
var overlay = getCommandsDiv();
$(overlay).show()
.center();
commandsEnabled = true;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(hideCommandsOverlay, 10000);
}
function hideCommandsOverlay() {
commandsEnabled = false;
if (timer) {
clearTimeout(timer);
timer = null;
}
var overlay = getCommandsDiv();
$(overlay).hide();
}
function getCommandsDiv() {
if (typeof commandsDiv != 'undefined') {
return commandsDiv;
}
var div = commandsDiv = document.createElement('div');
div.style = 'z-index: 1000001; color: red; background-color: white; padding: 1em; border: 1px solid red;';
div.style.position = 'absolute';
div.style.opacity = 0.75;
div.innerHTML = `Follow-up shortcuts available for copying issue details to clipboard (<key> ... <what appears in clipboard>):
<ul>
<li>Shift+L ... <u>l</u>ink to issue, including issue key and summary</li>
<li>Shift+S ... <u>s</u>hort link to issue, including just issue key</li>
<li>Shift+M ... commit <u>m</u>essage template</li>
<hr />
<li>Shift+E ... <u>e</u>scape - hide this popup</li>
</ul>
<p>
NEW since v1.1: Mouse over a tag-like item to copy its text to clipboard via a dynamic 'copy' button.
</p>
`;
insertBodyElement(div);
return div;
}
// register the various keyboard shortcuts
function bindShortcut(keys, handler) {
// make the shortcuts case-insensitive (bind() takes space as alternative keys separator):
keys = keys + ' ' + keys.toUpperCase();
$(document).bind('keypress', keys, handler);
}
function bindCopyShortcut(keys, handler) {
bindShortcut(keys, function() {
trace('commandsEnabled: ' + commandsEnabled);
if (commandsEnabled) {
handler();
hideCommandsOverlay();
}
});
}
// TODO How to temporatily suppress the standard JIRA shortcuts, so there is no need to combine the letters with for example 'shift' as below?
bindShortcut('d', showCommandsOverlay);
bindCopyShortcut('shift+e', hideCommandsOverlay);
bindCopyShortcut('shift+l', copyAsLinkFull);
bindCopyShortcut('shift+s', copyAsLinkWithKey);
bindCopyShortcut('shift+m', copyAsCommitMessage);
function copyToClipboard(html) {
copyHtmlToClipboard(html);
showInfo('Copied to clipboard: ' + escapeHtml(html));
}
var infoTimer = null;
function showInfo(html) {
var div = getInfoDiv();
div.innerHTML = html;
$(div).show()
.center();
var timer = infoTimer;
if (timer) {
clearTimeout(timer);
}
infoTimer = setTimeout(function() {
infoTimer = null;
$(div).hide();
}, 3000);
}
function getInfoDiv() {
if (typeof infoDiv != 'undefined') {
return infoDiv;
}
var div = infoDiv = document.createElement('div');
div.style = 'z-index: 1000000; color: green; background-color: white; padding: 1em; border: 0px solid green;';
div.style.position = 'absolute';
div.style.opacity = 1;
insertBodyElement(div);
return div;
}
// ==================== Clipboard utils ====================
// Inspired by http://jsfiddle.net/73v73p18/
function getClipboardDiv() {
if (typeof clipboardDiv != 'undefined') {
return clipboardDiv;
}
clipboardDiv = document.createElement('div');
clipboardDiv.style.fontSize = '12pt'; // Prevent zooming on iOS
// Reset box model
clipboardDiv.style.border = '0';
clipboardDiv.style.padding = '0';
clipboardDiv.style.margin = '0';
if (true) { // change to 'false' for seeing the div for debugging
// Move element out of screen
clipboardDiv.style.position = 'fixed';
clipboardDiv.style['right'] = '-9999px';
clipboardDiv.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
// more hiding
clipboardDiv.setAttribute('readonly', '');
clipboardDiv.style.opacity = 0;
clipboardDiv.style.pointerEvents = 'none';
clipboardDiv.style.zIndex = -1;
clipboardDiv.setAttribute('tabindex', '0'); // so it can be focused
clipboardDiv.innerHTML = '';
}
insertBodyElement(clipboardDiv);
return clipboardDiv;
}
function copyHtmlToClipboard(html) {
var clipboardDiv = getClipboardDiv();
// Note: adding redundant <span> element in order not to have any trailing new line or other white space in the clipboard
clipboardDiv.innerHTML = html + '<span></span>';
var focused = document.activeElement;
clipboardDiv.focus();
window.getSelection().removeAllRanges();
var range = document.createRange();
range.setStartBefore(clipboardDiv.firstChild);
range.setEndAfter(clipboardDiv.lastChild.previousSibling);
window.getSelection().addRange(range);
try {
if (!document.execCommand('copy')) {
console.log('execCommand returned false!');
}
} catch (e) {
console.log('execCommand failed with exception: ' + e);
}
focused.focus();
}
setInterval(extendTagLikeItems, 1000);
trace('started');
// ==================== Generic functions ====================
function escapeHtml(text) {
return text.replace(/[<>&]/g, function(match) {
if (match == '<') {
return '<';
} else if (match == '>') {
return '>';
} else {
return '&';
}
});
}
function getPagePath() {
var loc = window.location.href;
// cut off any '?...' if present
var qIndex = loc.indexOf('?');
loc = qIndex > -1 ? loc.substring(0, qIndex) : loc;
// cut off any #...' if present
var hIndex = loc.indexOf('#');
loc = hIndex > -1 ? loc.substring(0, hIndex) : loc;
return loc;
}
function insertBodyElement(el) {
// insert the element as the first child - for easier debugging
document.body.insertBefore(el, document.body.firstChild);
}
function trace(text) {
if (traceOn) {
console.log(text);
}
}
// ==================== jQuery generic functions ====================
// http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
jQuery.fn.center = function () {
this.css('position', 'absolute');
this.css('top', Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + 'px');
this.css('left', Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + 'px');
return this;
}
// ==================== copy-tag-like-item extension ====================
/*
* Structure of the target element:
*
* a) readonly mode (example: "Labels" field):
*
* <li><a class="lozenge"
* href="/secure/IssueNavigator.jspa?reset=true&jqlQuery=labels+%3D+workshop"
* title="workshop"><span>workshop</span></a></li>
*
* b) edit mode (example: field in the "Link issues dialog"):
*
* <li class="item-row" title="ISSUE-50"> <button type="button" tabindex="-1"
* class="value-item"> <span><span class="value-text">ISSUE-50</span></span>
* </button> <em class="item-delete" title="Remove"></em> </li>
*/
var PROCESSED_FLAG = 'data-cpy-processed';
function extendTagLikeItems() {
// readonly targets
var targets = $('li:has(> .lozenge)').filter(':not([' + PROCESSED_FLAG + '])');
for (var i = 0; i < targets.length; i++) {
trace('Adding copy button to readonly item #' + (i + 1));
var target = targets[i];
addCopyButton(target);
}
// editable targets
targets = $('.item-row:not([' + PROCESSED_FLAG + '])');
for (var i = 0; i < targets.length; i++) {
trace('Adding copy button to editable item #' + (i + 1));
var target = targets[i];
addCopyButton(target);
}
}
function addCopyButton(target) {
$(target).attr(PROCESSED_FLAG, true);
var copyBtn = getCopyButton();
copyBtn.visBinder.addTriggerEl(target);
}
function getCopyButton() {
if (typeof copyBtn != 'undefined') {
return copyBtn;
}
copyBtn = $('<div class="item-copy" title="Copy text to clipboard"'
+ ' style="z-index: 1000000; position: absolute; display: none; color: red; border: 1px solid red; background-color: white; opacity: 0.75;'
+ ' width: 14px; height: 20px; font-size: 16px; line-height: 16px; text-align: center; font-style: italic; font-weight: bold; cursor: pointer;">'
+ '<span style="vertical-align: middle">c</span></div>')[0];
insertBodyElement(copyBtn);
copyBtn.visBinder = new VisibilityBinder(copyBtn, {
timeout : 2000
});
$(copyBtn).on('click', function() {
$(this).hide();
copyToClipboard($(this.triggerEl).text());
});
return copyBtn;
}
// @class (reusable)
function VisibilityBinder(targetEl, cfg) {
this.targetEl = targetEl;
var timeout = cfg.timeout;
var timeoutT;
$(targetEl).on('mouseover', function() {
if (timeoutT) {
clearTimeout(timeoutT);
timeoutT = null;
}
});
$(targetEl).on('mouseout', function() {
timeoutT = setTimeout(function() {
$(targetEl).hide();
}, timeout);
});
this.addTriggerEl = function(triggerEl) {
$(triggerEl).on('mouseover', function() {
$(targetEl).show();
// this could be configurable as well:
$(targetEl).position({ my: 'center top', at: 'center bottom', of: triggerEl });
targetEl.triggerEl = triggerEl;
if (timeoutT) {
clearTimeout(timeoutT);
timeoutT = null;
}
});
$(triggerEl).on('mouseout', function() {
timeoutT = setTimeout(function() {
$(targetEl).hide();
}, timeout);
});
}
}
})(jQuery);