Auto Refresh ChatGPT session

Auto refresh chatGPT session

  1. // ==UserScript==
  2. // @name Auto Refresh ChatGPT session
  3. // @namespace http://github.com/zhaohongxuan
  4. // @version 0.5
  5. // @description Auto refresh chatGPT session
  6. // @author hank zhao
  7. // @match https://chat.openai.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  9. // @grant GM_notification
  10. // @license AGPL-3.0-or-later
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. function showNotification() {
  16. const notification = new Notification("chatGPT session auto refresh Failed",{
  17. "body":"Click to reload manully",
  18. "icon":"https://www.google.com/s2/favicons?sz=64&domain=openai.com"
  19. })
  20.  
  21. notification.onclick = (event) => {
  22. event.preventDefault(); // prevent the browser from focusing the Notification's tab
  23. location.reload()
  24. };
  25. }
  26.  
  27. function checkPermissionAndNotify() {
  28. if (!("Notification" in window)) {
  29. alert("chatGPT session auto refresh Failed");
  30. } else if (Notification.permission === "granted") {
  31. showNotification()
  32. } else if (Notification.permission !== "denied") {
  33. Notification.requestPermission().then((permission) => {
  34. if (permission === "granted") {
  35. showNotification()
  36. }
  37. });
  38. }
  39. }
  40.  
  41. const intervalId = setInterval(function() {
  42. var xhr = new XMLHttpRequest()
  43. xhr.open('GET', "https://chat.openai.com/api/auth/session")
  44. xhr.onload = function() {
  45. if (xhr.status === 200) {
  46. console.debug('refresh chatGPT session Success')
  47. } else {
  48. console.warn('refresh chatGPT session Failed: ', xhr.status)
  49. clearInterval(intervalId)
  50. checkPermissionAndNotify()
  51. }
  52. }
  53. xhr.onerror = function() {
  54. console.warn('refresh chatGPT session Error: ', xhr.status)
  55. clearInterval(intervalId)
  56. checkPermissionAndNotify()
  57. }
  58. xhr.send()
  59. }, 10000);
  60. })();

QingJ © 2025

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