ChatGPT: Fix Enter Key on Small Sizes

Userscript changes the mobile interface by restoring Ctrl+Enter or Enter as Submit, even when opening small, thin windows in the desktop environment.

< 脚本ChatGPT: Fix Enter Key on Small Sizes的反馈

评价:差评 - 脚本失效或无法使用

§
发表于:2023-11-26

Installed today (2023/11/26) and it did not work.

My guess is that the 'required' header is asking for a JS file that doesn't exist anymore, and looking at ChatGPT JS files, I don't see one named chatgpt.js, so they probably moved things around.

I don't know my way around JS much, so I just set a 5s wait instead of the await isLoaded method, and I get the inputArea via the old document.getElementById. I also had to change the selSubmit and add an extra div there. My focus is just the ENTER working on small screens so I didn't pay attention to the newChat functionality, but at least this change makes the ENTER feature work:

const selSubmit = 'form > div > div > div > button';
const selNewChat = 'nav > div > a, div.sticky > button:last-of-type';
const enableDefaultModeOnMobile = true;

// I need to ensure that ChatGPT is fully loaded to check selector validity

(async () => {
setTimeout(
function() {
btnSubmit = document.querySelector(selSubmit);
btnNewChat = document.querySelector(selNewChat);
inputArea = document.getElementById("prompt-textarea");
inputArea.addEventListener('keydown', handleKeydown, true);
}, 5000);

})();

All I have to do is wait 5s before typing anything, 5s was enough for me but can be changed for slower connections.

§
发表于:2023-11-26
编辑于:2023-11-26

Just a fix, code above just works once, because I don't know why the rect is on 0,0 the second time.
So I have to get the button object via query selector every time:

//removed querySelectors from the initial function
(async () => {
setTimeout(
function() {
inputArea = document.getElementById("prompt-textarea");
inputArea.addEventListener('keydown', handleKeydown, true);
}, 5000);

})();

//then, inside handleKeyDown, I get the button just when needed

if (enableDefaultModeOnMobile) {

if (isEnter && modKey === 0) { // force enter, even on mobile
btnSubmit = document.querySelector(selSubmit);
clickMouseOn(btnSubmit);
skipActions(event);
return false;
}
(...)
//also removed the buttons from the if condition
if (isEnter && modKey === 3) { // console.log('New Chat');
btnNewChat = document.querySelector(selNewChat);
clickMouseOn(btnNewChat);
ignoreOtherListeners = true;
}

if (isEnter && modKey === 1) { // console.log('Send');
btnSubmit = document.querySelector(selSubmit);
clickMouseOn(btnSubmit);
ignoreOtherListeners = true;
}

Now it works more than once =)

发表回复

登录(不可用)以发表回复。

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址