-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfluence-jira-title-copy.user.js
More file actions
525 lines (462 loc) · 18.2 KB
/
confluence-jira-title-copy.user.js
File metadata and controls
525 lines (462 loc) · 18.2 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// ==UserScript==
// @name Confluence Jira Title Copy
// @name:en Confluence Jira Title Copy
// @name:ja Confluence Jira タイトルコピー
// @name:zh-CN Confluence Jira 复制标题和链接
// @description Add buttons to copy title and link of a Confluence/Jira page, and to copy as filename
// @description:en Add buttons to copy title and link of a Confluence/Jira page, and to copy as filename
// @description:ja ボタンをクリックして、タイトルテキスト+リンクをマークダウン形式でコピーし、ファイル名としてコピーします。
// @description:zh-CN 点击按钮以markdown格式复制标题文本+链接,以及复制为文件名
// @namespace https://github.com/CheerChen
// @version 0.8.2
// @author cheerchen37
// @license MIT
// @match *://*.atlassian.net/*
// @grant none
// @icon https://www.google.com/s2/favicons?domain=atlassian.net
// @homepage https://github.com/CheerChen/userscripts
// @supportURL https://github.com/CheerChen/userscripts/issues
// @updateURL https://raw.githubusercontent.com/CheerChen/userscripts/master/confluence-jira-title-copy.user.js
// ==/UserScript==
(function () {
'use strict';
// Initialize script
function initScript() {
// Add button styles
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
.custom-copy-button {
display: inline-flex;
align-items: center;
margin-left: 10px;
padding: 6px 12px;
background-color: #0052CC;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 12px;
vertical-align: middle;
}
.custom-copy-button:hover {
background-color: #003380;
}
#copyFeedback {
position: absolute;
margin-top: 5px;
background-color: #000;
color: #fff;
padding: 5px 10px;
border-radius: 5px;
display: none;
z-index: 1001;
}
`;
document.head.appendChild(style);
// Initial check for buttons
addButtons();
// Set up a timer to check for the elements periodically
setInterval(addButtons, 2000);
}
// Get Confluence page title
function getConfluenceTitle() {
const selectors = [
'#title-text',
'h1.css-1xrg2ua',
'h1[data-test-id="content-title"]',
'h1.PageTitle',
'.confluence-page-title',
'.aui-page-header-main h1',
'#content-header-container h1',
// Additional Confluence selectors
'.css-1mpsox7 h1', // New Confluence cloud
'#main-content h1:first-child',
'.wiki-content .confluenceTitle',
'h1.pagetitle'
];
for (const selector of selectors) {
const element = document.querySelector(selector);
if (element && element.textContent.trim()) {
return element.textContent.trim();
}
}
return document.title.split(' - ')[0].trim();
}
// Get Jira page title
function getJiraTitle() {
const mainSelector = 'h1[data-testid="issue.views.issue-base.foundation.summary.heading"]';
const mainElement = document.querySelector(mainSelector);
if (mainElement && mainElement.textContent.trim()) {
return mainElement.textContent.trim();
}
const backupSelectors = [
'h1[data-test-id="issue-title"]',
'h1.issue-title',
'h1.ghx-summary',
'.issue-header h1',
'.jira-issue-header h1',
// Additional selectors for backlog view
'.ghx-issue-title',
'[data-testid="rapid-board-issue.ui.issue-card.title-container"]',
'[role="heading"][aria-level="3"]' // Often used in backlog
];
for (const selector of backupSelectors) {
const elements = document.querySelectorAll(selector);
if (elements && elements.length > 0) {
// Return the one that's visible or the first one
for (const element of elements) {
if (element && element.textContent.trim() && isElementVisible(element)) {
return element.textContent.trim();
}
}
// If no visible element found, return the first one
if (elements[0].textContent.trim()) {
return elements[0].textContent.trim();
}
}
}
return document.title.split(' - ')[0].trim();
}
// Check if element is visible
function isElementVisible(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}
// Get Jira ticket ID from URL or page
function getJiraTicketId() {
// 1. Try to extract ticket ID from URL parameters (backlog, modal, etc.)
const urlParams = new URLSearchParams(window.location.search);
for (const param of ['selectedIssue', 'modal', 'issueKey']) {
const value = urlParams.get(param);
if (value && /^[A-Z][A-Z0-9]+-\d+$/i.test(value)) {
return value;
}
}
// 2. Try to extract ticket ID from /browse/ URL
const urlMatch = window.location.href.match(/\/browse\/([A-Z][A-Z0-9]+-\d+)/i);
if (urlMatch && urlMatch[1]) {
return urlMatch[1];
}
// 3. Try to extract from DOM elements (list view, board, etc.)
const selectors = [
'a[data-testid*="issue-key"]',
'a[data-testid*="issuekey"]',
'[data-testid*="issue-key"] a[href*="/browse/"]'
];
for (const selector of selectors) {
const element = document.querySelector(selector);
if (element) {
// Try text content first
const text = element.textContent.trim();
if (/^[A-Z][A-Z0-9]+-\d+$/i.test(text)) {
return text;
}
// Try href attribute
const href = element.getAttribute('href');
if (href) {
const hrefMatch = href.match(/\/browse\/([A-Z][A-Z0-9]+-\d+)/i);
if (hrefMatch && hrefMatch[1]) {
return hrefMatch[1];
}
}
}
}
return '';
}
// Sanitize string for use as filename
function sanitizeFilename(input) {
return input.replace(/[\\/:*?"<>|[\]{}#%&+,;=@^`~]/g, '-')
.replace(/\s+/g, ' ')
.trim();
}
// Add copy buttons to page
function addButtons() {
// Check if we're in Jira
if (document.location.href.includes("/browse/") ||
document.location.href.includes("/jira/") ||
document.location.href.includes("/backlog")) {
addJiraButtons();
}
// Check if we're in Confluence
else if (document.location.href.includes("/wiki/") ||
document.location.href.includes("/confluence/") ||
document.location.href.includes("/display/")) {
addConfluenceButtons();
}
}
// Add buttons to Jira pages
function addJiraButtons() {
if (document.getElementById('customCopyButton')) {
return; // Buttons already exist
}
let titleElement = null;
// Try to find the title element using various selectors
const titleSelectors = [
'h1[data-testid="issue.views.issue-base.foundation.summary.heading"]',
'.issue-header h1',
'.jira-issue-header h1',
'.ghx-detail-title h1',
// For backlog view
'[data-testid="rapid-board-issue.ui.issue-card.title-container"]',
'.ghx-selected .ghx-summary'
];
for (const selector of titleSelectors) {
const element = document.querySelector(selector);
if (element && isElementVisible(element)) {
titleElement = element;
break;
}
}
// If we found a title element, add buttons next to it
if (titleElement) {
insertButtonsNextToElement(titleElement);
} else {
// For backlog, try to add to a visible container
const backlogContainers = [
'[data-test-id="platform-board.ui.board.board-container"]',
'.ghx-detail-view',
'.ghx-detail-contents',
'[data-testid="software-board.board.board.container"]'
];
for (const selector of backlogContainers) {
const container = document.querySelector(selector);
if (container && isElementVisible(container)) {
// Create floating buttons for backlog
insertFloatingButtons(container);
break;
}
}
}
}
// Add buttons to Confluence pages
function addConfluenceButtons() {
if (document.getElementById('customCopyButton')) {
return; // Buttons already exist
}
let titleElement = null;
// Try to find the confluence title using various selectors
const titleSelectors = [
'#title-text',
'h1.css-1xrg2ua',
'h1[data-test-id="content-title"]',
'h1.PageTitle',
'.confluence-page-title',
'.aui-page-header-main h1',
'#content-header-container h1',
// Additional Confluence selectors
'.css-1mpsox7 h1',
'#main-content h1:first-child',
'.wiki-content .confluenceTitle',
'h1.pagetitle',
// Generic h1 as last resort
'#main-content h1'
];
for (const selector of titleSelectors) {
const element = document.querySelector(selector);
if (element && isElementVisible(element)) {
titleElement = element;
break;
}
}
// If we found a title element, add buttons next to it
if (titleElement) {
insertButtonsNextToElement(titleElement);
} else {
// Try adding to a container
const containers = [
'#main-content',
'.confluence-information-macro-body',
'.wiki-content',
'.content-body',
'#content'
];
for (const selector of containers) {
const container = document.querySelector(selector);
if (container && isElementVisible(container)) {
insertFloatingButtons(container);
break;
}
}
}
}
// Insert buttons next to an element
function insertButtonsNextToElement(element) {
// Create button container
const buttonContainer = document.createElement('div');
buttonContainer.style.display = 'inline-flex';
buttonContainer.style.alignItems = 'center';
buttonContainer.style.marginLeft = '10px';
// Create markdown button
const button = document.createElement('button');
button.id = 'customCopyButton';
button.className = 'custom-copy-button';
button.textContent = 'Copy Title & Link';
buttonContainer.appendChild(button);
// Create filename button
const filenameButton = document.createElement('button');
filenameButton.id = 'customCopyFilenameButton';
filenameButton.className = 'custom-copy-button';
filenameButton.style.marginLeft = '5px';
filenameButton.textContent = 'Copy as Filename';
buttonContainer.appendChild(filenameButton);
// Create feedback element
const feedback = document.createElement('div');
feedback.id = 'copyFeedback';
feedback.textContent = 'Copied!';
buttonContainer.appendChild(feedback);
// Insert container after the element
if (element.nextSibling) {
element.parentNode.insertBefore(buttonContainer, element.nextSibling);
} else {
element.parentNode.appendChild(buttonContainer);
}
// Add event listeners
button.addEventListener('click', copyAsMarkdown);
filenameButton.addEventListener('click', copyAsFilename);
}
// Insert floating buttons in a container
function insertFloatingButtons(container) {
// Create floating button container
const buttonContainer = document.createElement('div');
buttonContainer.style.position = 'absolute';
buttonContainer.style.top = '10px';
buttonContainer.style.right = '10px';
buttonContainer.style.zIndex = '1000';
buttonContainer.style.display = 'flex';
// Create markdown button
const button = document.createElement('button');
button.id = 'customCopyButton';
button.className = 'custom-copy-button';
button.textContent = 'Copy Title & Link';
buttonContainer.appendChild(button);
// Create filename button
const filenameButton = document.createElement('button');
filenameButton.id = 'customCopyFilenameButton';
filenameButton.className = 'custom-copy-button';
filenameButton.style.marginLeft = '5px';
filenameButton.textContent = 'Copy as Filename';
buttonContainer.appendChild(filenameButton);
// Create feedback element
const feedback = document.createElement('div');
feedback.id = 'copyFeedback';
feedback.textContent = 'Copied!';
feedback.style.position = 'absolute';
feedback.style.top = '40px';
feedback.style.right = '0';
buttonContainer.appendChild(feedback);
// Make sure container has position relative
const currentPosition = window.getComputedStyle(container).position;
if (currentPosition === 'static') {
container.style.position = 'relative';
}
// Add to container
container.appendChild(buttonContainer);
// Add event listeners
button.addEventListener('click', copyAsMarkdown);
filenameButton.addEventListener('click', copyAsFilename);
}
// Copy title and link as Markdown
function copyAsMarkdown() {
let titleText = '';
if (document.location.href.includes("wiki") || document.location.href.includes("confluence")) {
titleText = getConfluenceTitle();
console.log("Got Confluence title:", titleText);
} else {
titleText = getJiraTitle();
console.log("Got Jira title:", titleText);
}
if (!titleText) {
titleText = document.title;
console.log("Using document title:", titleText);
}
const pageLink = window.location.href;
const copyText = `[${titleText}](${pageLink})`;
console.log("Copying as Markdown:", copyText);
copyToClipboard(copyText);
}
// Copy as filename format
function copyAsFilename() {
let titleText = '';
let ticketId = '';
if (document.location.href.includes("wiki") || document.location.href.includes("confluence")) {
titleText = getConfluenceTitle();
} else {
titleText = getJiraTitle();
ticketId = getJiraTicketId();
console.log("Extracted ticket ID:", ticketId);
}
if (!titleText) {
titleText = document.title;
}
// Sanitize title to be safe for filenames
titleText = sanitizeFilename(titleText);
// Create filename format: {ticket-id} {title}
let filename = titleText;
if (ticketId) {
filename = `${ticketId} ${titleText}`;
}
console.log("Copying as filename:", filename);
copyToClipboard(filename);
}
// Copy text to clipboard
function copyToClipboard(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => {
showFeedback();
}).catch(err => {
console.error("Clipboard API failed:", err);
copyWithFallback(text);
});
} else {
copyWithFallback(text);
}
}
// Fallback copy method
function copyWithFallback(text) {
const tempTextarea = document.createElement('textarea');
tempTextarea.style.position = 'fixed';
tempTextarea.style.top = '0';
tempTextarea.style.left = '0';
tempTextarea.style.width = '2em';
tempTextarea.style.height = '2em';
tempTextarea.style.opacity = '0';
document.body.appendChild(tempTextarea);
tempTextarea.value = text;
tempTextarea.select();
try {
const success = document.execCommand('copy');
if (success) {
showFeedback();
} else {
console.error("execCommand copy failed");
}
} catch (err) {
console.error('Copy error:', err);
}
document.body.removeChild(tempTextarea);
}
// Show copy success feedback
function showFeedback() {
const feedback = document.getElementById('copyFeedback');
if (feedback) {
feedback.style.display = 'block';
setTimeout(() => { feedback.style.display = 'none'; }, 2000);
}
}
// Initialize on page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => setTimeout(initScript, 500));
} else {
setTimeout(initScript, 500);
}
// Watch for DOM changes
const observer = new MutationObserver(function (mutations) {
if (!document.getElementById('customCopyButton')) {
addButtons();
}
});
// Start observing after initialization
setTimeout(() => {
observer.observe(document.body, { childList: true, subtree: true });
}, 1000);
})();