-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGML-右键0.js
173 lines (156 loc) · 6.17 KB
/
GML-右键0.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
// ==UserScript==
// @name Markdown Link Generator with Fully Styled Settings Panel
// @namespace http://tampermonkey.net/
// @version 1.7
// @description 获取当前网站的网址和标题,并在页面中央显示Markdown格式的链接,以及一个复制按钮。现在设置面板中的输入框、选择框和按钮都有了黑色边框和圆角。
// @author ZDY
// @icon https://markdown.com.cn/hero.png
// @match *://*/*
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
let buttonPosition = 'left'; // 按钮初始位置
let buttonBottom = 40; // 按钮距底部距离
let buttonFontSize = 9; // 按钮字体大小
// 创建右下角按钮样式
function createButtonStyle() {
return `
position: fixed;
bottom: ${buttonBottom}px;
${buttonPosition}: 20px;
z-index: 10000;
padding: 2px 5px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
font-size: ${buttonFontSize}px;
`;
}
// 通用样式,用于输入框、选择框和按钮
const commonStyle = `
border: 1px solid black; /* 黑色边框 */
border-radius: 4px; /* 圆角 */
padding: 2px 5px; /* 内边距 */
`;
// 输入框样式
const inputStyle = `
width: 50%; /* 设置输入框的宽度 */
border: 1px solid black; /* 加上黑色边框 */
border-radius: 4px; /* 设置圆角 */
padding: 2px 5px; /* 添加内边距 */
`;
// 创建设置面板样式,修改为在页面中央显示
const settingsPanelStyle = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 10001;
background-color: white;
border: 1px solid black;
padding: 10px;
border-radius: 5px;
display: none;
`;
// 创建复制按钮样式
const copyButtonStyle = `
padding: 5px 10px;
background-color: #007BFF;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
margin-left: 10px;
font-size: 10px;
`;
// 创建显示链接的样式
const linkStyle = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 10000;
background-color: white;
border: 1px solid black;
padding: 10px;
border-radius: 5px;
display: none;
font-size: 10px;
`;
// 创建设置面板
const settingsPanel = document.createElement('div');
settingsPanel.style.cssText = settingsPanelStyle;
settingsPanel.innerHTML = `
<div>
<label for="positionSelect">按钮位置:</label>
<select id="positionSelect" style="${commonStyle}">
<option value="left">左</option>
<option value="right">右</option>
</select>
</div>
<div>
<label for="bottomInput">底部距离(px):</label>
<input type="number" id="bottomInput" value="${buttonBottom}" style="${inputStyle}">
</div>
<div>
<label for="fontSizeInput">字体大小(px):</label>
<input type="number" id="fontSizeInput" value="${buttonFontSize}" style="${inputStyle}">
</div>
<button id="saveSettings" style="${commonStyle}">保存设置</button>
`;
// 创建复制按钮
const copyButton = document.createElement('button');
copyButton.innerHTML = 'M-Link';
copyButton.style.cssText = createButtonStyle();
// 创建显示Markdown链接的元素
const linkContainer = document.createElement('div');
linkContainer.style.cssText = linkStyle;
// 将元素添加到页面中
document.body.appendChild(copyButton);
document.body.appendChild(linkContainer);
document.body.appendChild(settingsPanel);
// 点击按钮时的行为
copyButton.addEventListener('click', function(event) {
event.preventDefault(); // 阻止默认行为
const title = document.title;
const url = window.location.href;
const markdownLink = `[${title}](${url})`;
linkContainer.innerHTML = `${markdownLink} <button style="${copyButtonStyle}">复制</button>`;
linkContainer.style.display = 'block';
// 为新的复制按钮添加事件
const newCopyButton = linkContainer.lastChild;
newCopyButton.addEventListener('click', function() {
try {
GM_setClipboard(markdownLink, 'text');
newCopyButton.innerHTML = '复制成功'; // 在按钮上显示复制成功
setTimeout(() => { newCopyButton.innerHTML = '复制'; }, 2000); // 2秒后复位按钮文字
} catch (e) {
newCopyButton.innerHTML = '复制失败'; // 在按钮上显示复制失败
setTimeout(() => { newCopyButton.innerHTML = '复制'; }, 2000); // 2秒后复位按钮文字
}
});
});
// 右键点击按钮显示设置面板
copyButton.addEventListener('contextmenu', function(event) {
event.preventDefault(); // 阻止默认的右键菜单
settingsPanel.style.display = 'block';
});
// 保存设置并应用
document.getElementById('saveSettings').addEventListener('click', function() {
buttonPosition = document.getElementById('positionSelect').value;
buttonBottom = parseInt(document.getElementById('bottomInput').value, 10);
buttonFontSize = parseInt(document.getElementById('fontSizeInput').value, 10);
copyButton.style.cssText = createButtonStyle(); // 更新按钮样式
settingsPanel.style.display = 'none'; // 关闭设置面板
});
// 点击显示框以外的地方关闭显示框
window.addEventListener('click', function(event) {
if (!linkContainer.contains(event.target) && event.target !== copyButton && !settingsPanel.contains(event.target)) {
linkContainer.style.display = 'none';
settingsPanel.style.display = 'none';
}
});
})();