-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPT_Copy_Text_on_Enter.js
52 lines (45 loc) · 1.66 KB
/
ChatGPT_Copy_Text_on_Enter.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
// ==UserScript==
// @name ChatGPT Copy Text on Enter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Send messages in ChatGPT using Command+Enter instead of Enter alone.
// @author [email protected]
// @match https://*.chatgpt.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const chatInputSelector = '#prompt-textarea'; // CSS selector for the ChatGPT input boxes
function handleKeydown(e) {
// 检查按下的是否是回车键
if (e.key === 'Enter') {
// 使用 e.target 获取当前触发事件的元素
const chatInput = e.target;
// 复制文本到剪贴板
navigator.clipboard.writeText(chatInput.value)
.then(function() {
console.log('Text copied to clipboard');
})
.catch(function(err) {
console.error('Could not copy text: ', err);
});
}
}
// 设置MutationObserver来监听DOM变化
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
document.querySelectorAll(chatInputSelector).forEach(chatInput => {
chatInput.removeEventListener('keydown', handleKeydown);
chatInput.addEventListener('keydown', handleKeydown);
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// 初始绑定
document.querySelectorAll(chatInputSelector).forEach(chatInput => {
chatInput.addEventListener('keydown', handleKeydown);
});
})();