-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjira_quick_copy.js
More file actions
160 lines (139 loc) · 6.05 KB
/
jira_quick_copy.js
File metadata and controls
160 lines (139 loc) · 6.05 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
// ==UserScript==
// @name Jira标题拷贝按钮
// @namespace https://izmj.net/
// @version 0.7
// @description 在JIRA页面,快速拷贝ID,标题,链接
// @author GiraKoo
// @license MIT
// @match *://*/browse/*
// @run-at document-end
// @icon64 https://gitee.com/izmj/web_js/raw/main/img/zmj.png
// ==/UserScript==
// 尝试获取元素,如果不存在则返回null
function getElementByIdSafe(id) {
return document.getElementById(id) || null;
}
// 尝试添加元素到指定位置,如果父元素不存在则不执行任何操作
function appendElementAfterSafe(newElement, referenceElement) {
if (referenceElement) {
referenceElement.after(newElement);
}
}
// 创建拷贝按钮的ul元素和li元素
var transitions_div = getElementByIdSafe('opsbar-opsbar-transitions');
var jira_copy_link_div = document.createElement('div');
jira_copy_link_div.id = 'jira_copy_link_div';
jira_copy_link_div.className = 'aui-buttons pluggable-ops';
// 如果transitions_div下面有ul元素
if (transitions_div) {
if (transitions_div.querySelector('ul')) {
var jira_copy_link_ul = document.createElement('ul');
jira_copy_link_ul.className = 'toolbar-group pluggable-ops';
jira_copy_link_ul.id = 'jira_copy_link_ul';
var li_copy_id = document.createElement('li');
li_copy_id.className = 'toolbar-item';
li_copy_id.innerHTML = '<a class="aui-button toolbar-trigger" href="#" id="jira_copy_id">ID</a>';
jira_copy_link_ul.appendChild(li_copy_id);
var li_copy_title = document.createElement('li');
li_copy_title.className = 'toolbar-item';
li_copy_title.innerHTML = '<a class="aui-button toolbar-trigger" href="#" id="jira_copy_title">标题</a>';
jira_copy_link_ul.appendChild(li_copy_title);
var li_copy_link = document.createElement('li');
li_copy_link.className = 'toolbar-item';
li_copy_link.innerHTML = '<a class="aui-button toolbar-trigger" href="#" id="jira_copy_link">链接</a>';
jira_copy_link_ul.appendChild(li_copy_link);
jira_copy_link_div.appendChild(jira_copy_link_ul);
}
else {
// 如果transitions_div下面没有ul元素,则直接使用a元素
var jira_copy_id_element = document.createElement('a');
jira_copy_id_element.className = 'aui-button toolbar-trigger';
jira_copy_id_element.href = '#';
jira_copy_id_element.id = 'jira_copy_id';
jira_copy_id_element.innerText = 'ID';
var jira_copy_title_element = document.createElement('a');
jira_copy_title_element.className = 'aui-button toolbar-trigger';
jira_copy_title_element.href = '#';
jira_copy_title_element.id = 'jira_copy_title';
jira_copy_title_element.innerText = '标题';
var jira_copy_link_element = document.createElement('a');
jira_copy_link_element.className = 'aui-button toolbar-trigger';
jira_copy_link_element.href = '#';
jira_copy_link_element.id = 'jira_copy_link';
jira_copy_link_element.innerText = '链接';
jira_copy_link_div.appendChild(jira_copy_id_element);
jira_copy_link_div.appendChild(jira_copy_title_element);
jira_copy_link_div.appendChild(jira_copy_link_element);
}
}
// 安全地添加按钮ul到transitions_div之后
appendElementAfterSafe(jira_copy_link_div, transitions_div);
// 显示提示信息的函数
function showToast(message) {
var toastId = 'toast_jira_quick_copy';
var div = document.createElement('div');
div.id = toastId;
div.className = 'toast';
div.style.position = 'fixed';
div.style.top = '10px';
div.style.left = '50%';
div.style.transform = 'translateX(-50%)';
div.style.padding = '10px';
div.style.backgroundColor = '#4CAF50';
div.style.color = 'white';
div.style.borderRadius = '5px';
div.style.zIndex = '9999';
div.innerText = message;
// 清除可能存在的旧toast
var oldToast = getElementByIdSafe(toastId);
if (oldToast) {
oldToast.parentNode.removeChild(oldToast);
}
document.body.appendChild(div);
// 设置定时器移除toast,并确保在移除前清除事件监听器
setTimeout(function () {
var toast = getElementByIdSafe(toastId);
if (toast) {
toast.parentNode.removeChild(toast);
}
}, 1000);
}
// 添加事件监听器,将JIRA的ID拷贝到剪贴板
var jira_copy_id_btn = getElementByIdSafe('jira_copy_id');
if (jira_copy_id_btn) {
jira_copy_id_btn.addEventListener('click', function () {
var jira_id_element = getElementByIdSafe('key-val');
if (jira_id_element) {
navigator.clipboard.writeText(jira_id_element.innerText);
showToast('已拷贝到剪贴板');
} else {
console.error('未能找到JIRA ID或标题元素');
}
});
}
// 添加事件监听器,将JIRA的ID和标题拷贝到剪贴板
var jira_copy_title_btn = getElementByIdSafe('jira_copy_title');
if (jira_copy_title_btn) {
jira_copy_title_btn.addEventListener('click', function () {
var jira_id_element = getElementByIdSafe('key-val');
var jira_title_element = getElementByIdSafe('summary-val');
if (jira_id_element && jira_title_element) {
var copy_text = jira_id_element.innerText + ' ' + jira_title_element.innerText;
navigator.clipboard.writeText(copy_text);
showToast('已拷贝到剪贴板');
} else {
console.error('未能找到JIRA ID或标题元素');
}
});
}
// 添加事件监听器,将JIRA链接拷贝到剪贴板
var jira_copy_link_btn = getElementByIdSafe('jira_copy_link');
if (jira_copy_link_btn) {
jira_copy_link_btn.addEventListener('click', function () {
// 获取key-val元素的href
var jira_id_element = getElementByIdSafe('key-val');
var jira_link = jira_id_element ? jira_id_element.href : window.location.href;
navigator.clipboard.writeText(jira_link);
showToast('已拷贝到剪贴板');
});
}