Guest User

gpt_response_alert

a guest
Mar 6th, 2025
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.48 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         ChatGPT回答通知
  3. // @namespace    http://tampermonkey.net/
  4. // @version      5.0
  5. // @description  监听按钮变化,回答完成发通知,并带可拖动+折叠开关+位置记忆
  6. // @match        https://chat.openai.com/*
  7. // @match        https://chatgpt.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12.     'use strict';
  13.  
  14.     const STORAGE_KEY = 'chatgptAnswerNotificationEnabled';
  15.     const POSITION_KEY = 'chatgptAnswerNotificationPosition';
  16.  
  17.     function showNotification() {
  18.         if (Notification.permission === 'granted') {
  19.             new Notification('ChatGPT回答完成', {
  20.                 body: '回答已生成完成!',
  21.                 icon: 'https://www.svgrepo.com/show/24550/robot.svg'
  22.             });
  23.         } else if (Notification.permission !== 'denied') {
  24.             Notification.requestPermission().then(permission => {
  25.                 if (permission === 'granted') {
  26.                     showNotification();
  27.                 }
  28.             });
  29.         }
  30.     }
  31.  
  32.     let isGenerating = false;
  33.  
  34.     function checkButtonState() {
  35.         const sendButton = document.querySelector('button[data-testid="send-button"]');
  36.         const stopButton = document.querySelector('button[data-testid="stop-button"]');
  37.  
  38.         if (stopButton) {
  39.             if (!isGenerating) {
  40.                 console.log('⚡️ 发现停止按钮,回答生成中...');
  41.                 isGenerating = true;
  42.             }
  43.         } else if (sendButton) {
  44.             if (isGenerating) {
  45.                 console.log('✅ 发现发送按钮恢复,回答生成完成!');
  46.                 isGenerating = false;
  47.                 if (getNotificationSetting()) {
  48.                     showNotification();
  49.                 } else {
  50.                     console.log('🔕 通知已关闭,跳过');
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     function createToggleButton() {
  57.         if (document.getElementById('notificationToggleButton')) {
  58.             return; // 防止重复创建
  59.         }
  60.  
  61.         const container = document.createElement('div');
  62.         container.id = 'notificationToggleButton';
  63.         container.style.position = 'fixed';
  64.         container.style.zIndex = '9999';
  65.         container.style.padding = '5px 10px';
  66.         container.style.background = '#000';
  67.         container.style.color = '#fff';
  68.         container.style.borderRadius = '5px';
  69.         container.style.cursor = 'grab';
  70.         container.style.fontSize = '14px';
  71.         container.style.userSelect = 'none';
  72.         container.style.transition = 'width 0.3s, padding 0.3s';
  73.         container.style.overflow = 'hidden';
  74.         container.style.width = '30px';
  75.         container.style.textAlign = 'center';
  76.         container.innerText = '⚙️';
  77.  
  78.         const savedPosition = getSavedPosition();
  79.         container.style.top = savedPosition.top;
  80.         container.style.left = savedPosition.left;
  81.  
  82.         const fullText = () => getNotificationSetting() ? '🔔 On' : '🔕 Off';
  83.  
  84.         function expand() {
  85.             container.innerText = fullText();
  86.             container.style.width = 'auto';
  87.             container.style.padding = '5px 10px';
  88.         }
  89.  
  90.         function collapse() {
  91.             container.innerText = '⚙️';
  92.             container.style.width = '30px';
  93.             container.style.padding = '5px';
  94.         }
  95.  
  96.         container.addEventListener('mouseenter', expand);
  97.         container.addEventListener('mouseleave', collapse);
  98.  
  99.         container.addEventListener('click', () => {
  100.             setNotificationSetting(!getNotificationSetting());
  101.             container.innerText = fullText();
  102.         });
  103.  
  104.         // 拖动逻辑
  105.         let offsetX = 0, offsetY = 0, dragging = false;
  106.  
  107.         container.addEventListener('mousedown', (e) => {
  108.             dragging = true;
  109.             offsetX = e.clientX - container.getBoundingClientRect().left;
  110.             offsetY = e.clientY - container.getBoundingClientRect().top;
  111.             container.style.cursor = 'grabbing';
  112.         });
  113.  
  114.         document.addEventListener('mousemove', (e) => {
  115.             if (!dragging) return;
  116.             container.style.top = `${e.clientY - offsetY}px`;
  117.             container.style.left = `${e.clientX - offsetX}px`;
  118.         });
  119.  
  120.         document.addEventListener('mouseup', () => {
  121.             if (dragging) {
  122.                 dragging = false;
  123.                 container.style.cursor = 'grab';
  124.                 savePosition(container.style.top, container.style.left);
  125.             }
  126.         });
  127.  
  128.         document.body.appendChild(container);
  129.     }
  130.  
  131.     function getNotificationSetting() {
  132.         return localStorage.getItem(STORAGE_KEY) === 'true';
  133.     }
  134.  
  135.     function setNotificationSetting(value) {
  136.         localStorage.setItem(STORAGE_KEY, value);
  137.     }
  138.  
  139.     function getSavedPosition() {
  140.         const pos = JSON.parse(localStorage.getItem(POSITION_KEY) || '{"top":"10px","left":"10px"}');
  141.         return pos;
  142.     }
  143.  
  144.     function savePosition(top, left) {
  145.         localStorage.setItem(POSITION_KEY, JSON.stringify({ top, left }));
  146.     }
  147.  
  148.     const observer = new MutationObserver(() => {
  149.         checkButtonState();
  150.         if (document.querySelector('div.min-w-9')) {
  151.             createToggleButton();
  152.         }
  153.     });
  154.  
  155.     observer.observe(document.body, { childList: true, subtree: true });
  156.  
  157.     checkButtonState();
  158.     if (document.querySelector('div.min-w-9')) {
  159.         createToggleButton();
  160.     }
  161. })();
  162.  
Advertisement
Add Comment
Please, Sign In to add comment