ChatGPT Temporary Chat Toggle without reloading the web

Toggle temporary chat mode on chaptgpt with double shift key press without reloading

  1. // ==UserScript==
  2. // @name ChatGPT Temporary Chat Toggle without reloading the web
  3. // @namespace http://tampermonkey.net/
  4. // @version 3
  5. // @description Toggle temporary chat mode on chaptgpt with double shift key press without reloading
  6. // @author 小红书 :沉浸式学法语😈 小红书id : 590862748
  7. // @match https://chatgpt.com/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. const DOUBLE_TAP_TIMEOUT = 500;
  16. let firstShiftPressTime = null;
  17. let isShiftHeld = false;
  18. let savedChatChannel = null;
  19.  
  20. document.addEventListener('keydown', handleKeyDown);
  21. document.addEventListener('keyup', handleKeyUp);
  22.  
  23. function handleKeyDown(e) {
  24. if (e.key !== 'Shift'&& e.location===KeyboardEvent.DOM_KEY_LOCATION_LEFT) return;
  25.  
  26. if (isShiftHeld) {
  27. return;
  28. }
  29.  
  30. const currentTime = Date.now();
  31. isShiftHeld = true;
  32.  
  33. if (firstShiftPressTime && currentTime - firstShiftPressTime <= DOUBLE_TAP_TIMEOUT) {
  34. toggleTemporaryChat();
  35. firstShiftPressTime = null;
  36. } else {
  37. firstShiftPressTime = currentTime;
  38. }
  39. }
  40.  
  41. function handleKeyUp(e) {
  42. if (e.key === 'Shift' && e.location===KeyboardEvent.DOM_KEY_LOCATION_LEFT) {
  43. isShiftHeld = false;
  44. }
  45. }
  46.  
  47. function toggleTemporaryChat() {
  48. const url = new URL(window.location.href);
  49. const params = new URLSearchParams(url.search);
  50.  
  51. if (params.get('temporary-chat') === 'true') {
  52. if (savedChatChannel) {
  53. history.replaceState(null, '', savedChatChannel);
  54. savedChatChannel = null;
  55. } else {
  56. //history.replaceState(null, '', '/'); // fix it can't go to the home page with this
  57. window.location.href = 'https://chatgpt.com';
  58. }
  59. } else {
  60. if (url.pathname.startsWith('/c/')) {
  61. savedChatChannel = url.pathname + url.search + url.hash;
  62. }
  63. params.set('temporary-chat', 'true');
  64. history.replaceState(null, '', `/?${params.toString()}`);
  65. }
  66.  
  67. // trigger URL change event for SPA frameworks
  68. window.dispatchEvent(new PopStateEvent('popstate', { state: history.state }));
  69. }
  70. })();
  71.  
  72. // ==Copyright==
  73. // Copyright © 2024 by 沉浸式学法语😈 (小红书 ID: 590862748). All rights reserved.
  74. // This script is the intellectual property of the author. Unauthorized copying, modification,
  75. // or redistribution of this script, in whole or in part, is strictly prohibited without explicit
  76. // prior written permission from the author.
  77. // For inquiries, contact the author through their 小红书 ID: 590862748.
  78. // ==End of Copyright==