Keldos commited on
Commit
9fb61e6
1 Parent(s): 91464d9

feat: 更新信息窗口加入交互

Browse files
Files changed (3) hide show
  1. assets/custom.css +9 -2
  2. assets/custom.js +62 -7
  3. assets/html/update.html +10 -6
assets/custom.css CHANGED
@@ -46,10 +46,11 @@ footer {
46
  #toast-update {
47
  position: absolute;
48
  display: flex;
49
- top: -20px;
50
  width: 100%;
51
  justify-content: center;
52
  z-index: var(--layer-top);
 
53
  }
54
  #check-chuanhu-update {
55
  position: absolute;
@@ -68,7 +69,7 @@ footer {
68
  overflow: hidden;
69
  pointer-events: auto;
70
  }
71
- .version-info-title {
72
  font-size: 1.2em;
73
  font-weight: bold;
74
  text-align: start;
@@ -82,6 +83,9 @@ footer {
82
  overflow: auto;
83
  padding: 0 8px;
84
  }
 
 
 
85
  .btn-update-group {
86
  display: flex;
87
  justify-content: space-between;
@@ -90,6 +94,9 @@ footer {
90
  width: 100%;
91
  padding-top: 10px;
92
  }
 
 
 
93
  /* user_info */
94
  #user_info {
95
  white-space: nowrap;
 
46
  #toast-update {
47
  position: absolute;
48
  display: flex;
49
+ top: -500px;
50
  width: 100%;
51
  justify-content: center;
52
  z-index: var(--layer-top);
53
+ transition: top 0.5s ease-in-out;
54
  }
55
  #check-chuanhu-update {
56
  position: absolute;
 
69
  overflow: hidden;
70
  pointer-events: auto;
71
  }
72
+ #version-info-title {
73
  font-size: 1.2em;
74
  font-weight: bold;
75
  text-align: start;
 
83
  overflow: auto;
84
  padding: 0 8px;
85
  }
86
+ #release-note-wrap.hideK {
87
+ display: none;
88
+ }
89
  .btn-update-group {
90
  display: flex;
91
  justify-content: space-between;
 
94
  width: 100%;
95
  padding-top: 10px;
96
  }
97
+ .btn-update-group.hideK {
98
+ display: none;
99
+ }
100
  /* user_info */
101
  #user_info {
102
  white-space: nowrap;
assets/custom.js CHANGED
@@ -18,10 +18,12 @@ var apSwitch = null;
18
  var messageBotDivs = null;
19
  var loginUserForm = null;
20
  var logginUser = null;
 
21
 
22
  var userLogged = false;
23
  var usernameGotten = false;
24
  var historyLoaded = false;
 
25
 
26
  var ga = document.getElementsByTagName("gradio-app");
27
  var targetNode = ga[0];
@@ -61,6 +63,7 @@ function gradioLoaded(mutations) {
61
  chatbot = document.querySelector('#chuanhu_chatbot');
62
  chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
63
  apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
 
64
 
65
  if (loginUserForm) {
66
  localStorage.setItem("userLogged", true);
@@ -85,10 +88,14 @@ function gradioLoaded(mutations) {
85
  if (chatbotWrap) {
86
  if (!historyLoaded) {
87
  loadHistoryHtml();
88
- updateLatestVersion();
89
  }
90
  setChatbotScroll();
91
  }
 
 
 
 
 
92
  }
93
  }
94
  }
@@ -498,23 +505,71 @@ function clearHistoryHtml() {
498
  }
499
 
500
  async function getLatestRelease() {
501
- const response = await fetch('https://api.github.com/repos/gaizhenbiao/chuanhuchatgpt/releases/latest');
502
- const data = await response.json();
503
- return data;
 
 
 
 
 
 
 
 
 
 
 
 
504
  }
505
  async function updateLatestVersion() {
 
506
  const latestVersionElement = document.getElementById('latest-version-title');
507
  const releaseNoteElement = document.getElementById('release-note-content');
 
 
 
508
  try {
509
  const data = await getLatestRelease();
510
- const latestVersion = data.tag_name;
511
- latestVersionElement.textContent = latestVersion;
512
  const releaseNote = data.body;
513
- releaseNoteElement.innerHTML = marked.parse(releaseNote);
 
 
 
 
 
 
 
 
 
 
 
514
  } catch (error) {
515
  console.error(error);
516
  }
517
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
  // 监视页面内部 DOM 变动
520
  var observer = new MutationObserver(function (mutations) {
 
18
  var messageBotDivs = null;
19
  var loginUserForm = null;
20
  var logginUser = null;
21
+ var updateToast = null;
22
 
23
  var userLogged = false;
24
  var usernameGotten = false;
25
  var historyLoaded = false;
26
+ var updateInfoGotten = false;
27
 
28
  var ga = document.getElementsByTagName("gradio-app");
29
  var targetNode = ga[0];
 
63
  chatbot = document.querySelector('#chuanhu_chatbot');
64
  chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
65
  apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
66
+ updateToast = document.querySelector("#toast-update");
67
 
68
  if (loginUserForm) {
69
  localStorage.setItem("userLogged", true);
 
88
  if (chatbotWrap) {
89
  if (!historyLoaded) {
90
  loadHistoryHtml();
 
91
  }
92
  setChatbotScroll();
93
  }
94
+ if (updateToast) {
95
+ if (!updateInfoGotten) {
96
+ updateLatestVersion();
97
+ }
98
+ }
99
  }
100
  }
101
  }
 
505
  }
506
 
507
  async function getLatestRelease() {
508
+ try {
509
+ const response = await fetch('https://api.github.com/repos/gaizhenbiao/chuanhuchatgpt/releases/latest');
510
+ if (!response.ok) {
511
+ console.log(`Error: ${response.status} - ${response.statusText}`);
512
+ updateInfoGotten = true;
513
+ return null;
514
+ }
515
+ const data = await response.json();
516
+ updateInfoGotten = true;
517
+ return data;
518
+ } catch (error) {
519
+ console.log(`Error: ${error}`);
520
+ updateInfoGotten = true;
521
+ return null;
522
+ }
523
  }
524
  async function updateLatestVersion() {
525
+ const currentVersionElement = document.getElementById('current-version');
526
  const latestVersionElement = document.getElementById('latest-version-title');
527
  const releaseNoteElement = document.getElementById('release-note-content');
528
+ const currentVersion = currentVersionElement.textContent;
529
+ // const currentVersion = '20230619'; // for debugging
530
+ updateInfoGotten = true; //无论成功与否都只执行一次,否则容易api超限...
531
  try {
532
  const data = await getLatestRelease();
 
 
533
  const releaseNote = data.body;
534
+ if (releaseNote) {
535
+ releaseNoteElement.innerHTML = marked.parse(releaseNote);
536
+ }
537
+ const latestVersion = data.tag_name;
538
+ if (latestVersion) {
539
+ latestVersionElement.textContent = latestVersion;
540
+ if (currentVersion !== latestVersion) {
541
+ if (!isInIframe) {openUpdateToast();}
542
+ } else {
543
+ noUpdate();
544
+ }
545
+ }
546
  } catch (error) {
547
  console.error(error);
548
  }
549
  }
550
+ function getUpdate() {
551
+ window.open('https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest', '_blank');
552
+ closeUpdateToast();
553
+ }
554
+ function cancelUpdate() {
555
+ closeUpdateToast();
556
+ }
557
+ function openUpdateToast() {
558
+ updateToast.style.setProperty('top', '-20px');
559
+ }
560
+ function closeUpdateToast() {
561
+ updateToast.style.setProperty('top', '-500px');
562
+ }
563
+ function noUpdate() {
564
+ const versionInfoElement = document.getElementById('version-info-title');
565
+ const releaseNoteWrap = document.getElementById('release-note-wrap');
566
+ const gotoUpdateBtn = document.getElementById('goto-update-btn');
567
+ const closeUpdateBtn = document.getElementById('close-update-btn');
568
+ versionInfoElement.textContent = 'You are using the latest version!';
569
+ releaseNoteWrap.style.setProperty('display', 'none');
570
+ gotoUpdateBtn.classList.add('hideK');
571
+ closeUpdateBtn.classList.remove('hideK');
572
+ }
573
 
574
  // 监视页面内部 DOM 变动
575
  var observer = new MutationObserver(function (mutations) {
assets/html/update.html CHANGED
@@ -1,20 +1,24 @@
1
  <div id="toast-update">
2
  <div id="check-chuanhu-update">
3
- <!-- <p style="font-size: 1em;">
4
- 当前版本: <span id="currentVersion">{current_version}</span>
5
- </p> -->
6
- <p class="version-info-title">
7
  Latest Version: <a href="https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest" target="_blank"
8
  id="latest-version-title" style="text-decoration: none;">getting latest version...</a>
9
  </p>
10
  <div id="release-note-wrap">
11
  <div class="release-note-content" id="release-note-content">
12
- Release Note...
13
  </div>
14
  </div>
15
- <div class="btn-update-group">
16
  <button class="btn-update lg secondary svelte-1ipelgc" id="cancel-button" onclick="cancelUpdate()">Cancel</button>
17
  <button class="btn-update lg primary svelte-1ipelgc" id="update-button" onclick="getUpdate()">Update</button>
18
  </div>
 
 
 
 
19
  </div>
20
  </div>
 
1
  <div id="toast-update">
2
  <div id="check-chuanhu-update">
3
+ <p style="display:none">
4
+ <span id="current-version">{current_version}</span>
5
+ </p>
6
+ <p id="version-info-title">
7
  Latest Version: <a href="https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest" target="_blank"
8
  id="latest-version-title" style="text-decoration: none;">getting latest version...</a>
9
  </p>
10
  <div id="release-note-wrap">
11
  <div class="release-note-content" id="release-note-content">
12
+ Getting Release Note...
13
  </div>
14
  </div>
15
+ <div id="goto-update-btn" class="btn-update-group">
16
  <button class="btn-update lg secondary svelte-1ipelgc" id="cancel-button" onclick="cancelUpdate()">Cancel</button>
17
  <button class="btn-update lg primary svelte-1ipelgc" id="update-button" onclick="getUpdate()">Update</button>
18
  </div>
19
+ <div id="close-update-btn" class="btn-update-group hideK">
20
+ <button class="btn-update lg secondary svelte-1ipelgc" id="update-button" onclick="getUpdate()">What's new</button>
21
+ <button class="btn-update lg primary svelte-1ipelgc" id="cancel-button" onclick="cancelUpdate()">OK</button>
22
+ </div>
23
  </div>
24
  </div>