Keldos commited on
Commit
ee42d46
·
1 Parent(s): 6b4e8ea

feat: 点击按钮后在后台更新程序(git)

Browse files
assets/custom.css CHANGED
@@ -112,6 +112,9 @@ footer {
112
  .btn-update-group.hideK {
113
  display: none;
114
  }
 
 
 
115
  /* user_info */
116
  #user_info.block {
117
  white-space: nowrap;
 
112
  .btn-update-group.hideK {
113
  display: none;
114
  }
115
+ span.hideK {
116
+ display: none;
117
+ }
118
  /* user_info */
119
  #user_info.block {
120
  white-space: nowrap;
assets/custom.js CHANGED
@@ -23,6 +23,7 @@ var sendBtn = null;
23
  var cancelBtn = null;
24
  var sliders = null;
25
  var updateChuanhuBtn = null;
 
26
 
27
  var userLogged = false;
28
  var usernameGotten = false;
@@ -37,7 +38,7 @@ var language = navigator.language.slice(0,2);
37
  var currentTime = new Date().getTime();
38
 
39
  // i18n
40
- var forView_i18n = {
41
  'zh': "仅供查看",
42
  'en': "For viewing only",
43
  'ja': "閲覧専用",
@@ -47,14 +48,14 @@ var forView_i18n = {
47
  'sv': "Endast för visning",
48
  };
49
 
50
- var deleteConfirm_i18n_pref = {
51
  'zh': "你真的要删除 ",
52
  'en': "Are you sure you want to delete ",
53
  'ja': "本当に ",
54
  'ko': "정말로 ",
55
  'sv': "Är du säker på att du vill ta bort "
56
  };
57
- var deleteConfirm_i18n_suff = {
58
  'zh': " 吗?",
59
  'en': " ?",
60
  'ja': " を削除してもよろしいですか?",
@@ -64,7 +65,7 @@ var deleteConfirm_i18n_suff = {
64
  var deleteConfirm_msg_pref = "Are you sure you want to delete ";
65
  var deleteConfirm_msg_suff = " ?";
66
 
67
- var usingLatest_i18n = {
68
  'zh': "您使用的就是最新版!",
69
  'en': "You are using the latest version!",
70
  'ja': "最新バージョンを使用しています!",
@@ -72,6 +73,21 @@ var usingLatest_i18n = {
72
  'sv': "Du använder den senaste versionen!"
73
  };
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  // gradio 页面加载好了么??? 我能动你的元素了么??
76
  function gradioLoaded(mutations) {
77
  for (var i = 0; i < mutations.length; i++) {
@@ -89,6 +105,7 @@ function gradioLoaded(mutations) {
89
  cancelBtn = document.getElementById("cancel_btn");
90
  sliders = document.querySelectorAll('input[type="range"]');
91
  updateChuanhuBtn = document.getElementById("update_chuanhu_btn");
 
92
 
93
  if (loginUserForm) {
94
  localStorage.setItem("userLogged", true);
@@ -117,6 +134,9 @@ function gradioLoaded(mutations) {
117
  setChatbotScroll();
118
  mObserver.observe(chatbotWrap, { attributes: true, childList: true, subtree: true, characterData: true});
119
  }
 
 
 
120
  if (sliders) {
121
  setSlider();
122
  }
@@ -507,6 +527,22 @@ var submitObserver = new MutationObserver(function (mutationsList) {
507
  saveHistoryHtml();
508
  });
509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
  var loadhistorytime = 0; // for debugging
511
  function saveHistoryHtml() {
512
  var historyHtml = document.querySelector('#chuanhu_chatbot>.wrapper>.wrap');
@@ -570,7 +606,7 @@ async function getLatestRelease() {
570
  console.log(`Error: ${response.status} - ${response.statusText}`);
571
  updateInfoGotten = true;
572
  return null;
573
- }
574
  const data = await response.json();
575
  updateInfoGotten = true;
576
  return data;
@@ -580,10 +616,12 @@ async function getLatestRelease() {
580
  return null;
581
  }
582
  }
 
 
583
  async function updateLatestVersion() {
584
  const currentVersionElement = document.getElementById('current-version');
585
  const latestVersionElement = document.getElementById('latest-version-title');
586
- const releaseNoteElement = document.getElementById('release-note-content');
587
  const currentVersion = currentVersionElement.textContent;
588
  const versionTime = document.getElementById('version-time').innerText;
589
  const localVersionTime = versionTime !== "unknown" ? (new Date(versionTime)).getTime() : 0;
@@ -611,10 +649,16 @@ async function updateLatestVersion() {
611
  console.error(error);
612
  }
613
  }
614
- function getUpdate() {
615
  window.open('https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest', '_blank');
616
  closeUpdateToast();
617
  }
 
 
 
 
 
 
618
  function cancelUpdate() {
619
  closeUpdateToast();
620
  }
@@ -645,6 +689,15 @@ function noUpdate() {
645
  gotoUpdateBtn.classList.add('hideK');
646
  closeUpdateBtn.classList.remove('hideK');
647
  }
 
 
 
 
 
 
 
 
 
648
  function setUpdateWindowHeight() {
649
  if (!showingUpdateInfo) {return;}
650
  const scrollPosition = window.scrollY;
 
23
  var cancelBtn = null;
24
  var sliders = null;
25
  var updateChuanhuBtn = null;
26
+ var statusDisplay = null;
27
 
28
  var userLogged = false;
29
  var usernameGotten = false;
 
38
  var currentTime = new Date().getTime();
39
 
40
  // i18n
41
+ const forView_i18n = {
42
  'zh': "仅供查看",
43
  'en': "For viewing only",
44
  'ja': "閲覧専用",
 
48
  'sv': "Endast för visning",
49
  };
50
 
51
+ const deleteConfirm_i18n_pref = {
52
  'zh': "你真的要删除 ",
53
  'en': "Are you sure you want to delete ",
54
  'ja': "本当に ",
55
  'ko': "정말로 ",
56
  'sv': "Är du säker på att du vill ta bort "
57
  };
58
+ const deleteConfirm_i18n_suff = {
59
  'zh': " 吗?",
60
  'en': " ?",
61
  'ja': " を削除してもよろしいですか?",
 
65
  var deleteConfirm_msg_pref = "Are you sure you want to delete ";
66
  var deleteConfirm_msg_suff = " ?";
67
 
68
+ const usingLatest_i18n = {
69
  'zh': "您使用的就是最新版!",
70
  'en': "You are using the latest version!",
71
  'ja': "最新バージョンを使用しています!",
 
73
  'sv': "Du använder den senaste versionen!"
74
  };
75
 
76
+ const updateSuccess_i18n = {
77
+ 'zh': "更新成功,请重启本程序。",
78
+ 'en': "Updated successfully, please restart this program.",
79
+ 'ja': "更新が成功しました、このプログラムを再起動してください。",
80
+ 'ko': "업데이트 성공, 이 프로그램을 재시작 해주세요.",
81
+ 'sv': "Uppdaterat framgångsrikt, starta om programmet."
82
+ }
83
+ const updateFailure_i18n = {
84
+ 'zh': '更新失败,请尝试<a href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新" target="_blank">手动更新</a>。',
85
+ 'en': 'Update failed, please try <a href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新" target="_blank">manually updating</a>.',
86
+ 'ja': '更新に失敗しました、<a href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新" target="_blank">手動での更新</a>をお試しください。',
87
+ 'ko': '업데이트 실패, <a href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新" target="_blank">수동 업데이트</a>를 시도하십시오.',
88
+ 'sv': 'Uppdateringen misslyckades, prova att <a href="https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新" target="_blank">uppdatera manuellt</a>.'
89
+ }
90
+
91
  // gradio 页面加载好了么??? 我能动你的元素了么??
92
  function gradioLoaded(mutations) {
93
  for (var i = 0; i < mutations.length; i++) {
 
105
  cancelBtn = document.getElementById("cancel_btn");
106
  sliders = document.querySelectorAll('input[type="range"]');
107
  updateChuanhuBtn = document.getElementById("update_chuanhu_btn");
108
+ statusDisplay = document.querySelector('#status_display');
109
 
110
  if (loginUserForm) {
111
  localStorage.setItem("userLogged", true);
 
134
  setChatbotScroll();
135
  mObserver.observe(chatbotWrap, { attributes: true, childList: true, subtree: true, characterData: true});
136
  }
137
+ if (statusDisplay) {
138
+ // statusObserver.observe(statusDisplay, { childList: true, subtree: true, characterData: true});
139
+ }
140
  if (sliders) {
141
  setSlider();
142
  }
 
527
  saveHistoryHtml();
528
  });
529
 
530
+ var statusObserver = new MutationObserver(function (mutationsList) {
531
+ for (const mutation of mutationsList) {
532
+ if (mutation.type === 'attributes' || mutation.type === 'childList') {
533
+ if (statusDisplay.innerHTML.includes('<span id="update-status"')) {
534
+ if (getUpdateStatus() === "success") {
535
+ releaseNoteElement.innerHTML = updateSuccess_i18n.hasOwnProperty(language) ? updateSuccess_i18n[language] : updateSuccess_i18n['en'];
536
+ } else if (getUpdateStatus() === "failure") {
537
+ releaseNoteElement.innerHTML = updateFailure_i18n.hasOwnProperty(language) ? updateFailure_i18n[language] : updateFailure_i18n['en'];
538
+ } else {
539
+ releaseNoteElement.innerHTML = getUpdateStatus();
540
+ }
541
+ }
542
+ }
543
+ }
544
+ });
545
+
546
  var loadhistorytime = 0; // for debugging
547
  function saveHistoryHtml() {
548
  var historyHtml = document.querySelector('#chuanhu_chatbot>.wrapper>.wrap');
 
606
  console.log(`Error: ${response.status} - ${response.statusText}`);
607
  updateInfoGotten = true;
608
  return null;
609
+ }
610
  const data = await response.json();
611
  updateInfoGotten = true;
612
  return data;
 
616
  return null;
617
  }
618
  }
619
+
620
+ var releaseNoteElement = document.getElementById('release-note-content');
621
  async function updateLatestVersion() {
622
  const currentVersionElement = document.getElementById('current-version');
623
  const latestVersionElement = document.getElementById('latest-version-title');
624
+ releaseNoteElement = document.getElementById('release-note-content');
625
  const currentVersion = currentVersionElement.textContent;
626
  const versionTime = document.getElementById('version-time').innerText;
627
  const localVersionTime = versionTime !== "unknown" ? (new Date(versionTime)).getTime() : 0;
 
649
  console.error(error);
650
  }
651
  }
652
+ function getUpdateInfo() {
653
  window.open('https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest', '_blank');
654
  closeUpdateToast();
655
  }
656
+ function bgUpdateChuanhu() {
657
+ updateChuanhuBtn.click();
658
+ releaseNoteElement = document.getElementById('release-note-content');
659
+ releaseNoteElement.innerHTML = '<p>正在尝试更新...</p>';
660
+ statusObserver.observe(statusDisplay, { childList: true, subtree: true, characterData: true});
661
+ }
662
  function cancelUpdate() {
663
  closeUpdateToast();
664
  }
 
689
  gotoUpdateBtn.classList.add('hideK');
690
  closeUpdateBtn.classList.remove('hideK');
691
  }
692
+ function getUpdateStatus() {
693
+ const updateStatus = statusDisplay.querySelector("#update-status")
694
+ if (updateStatus) {
695
+ return updateStatus.innerText
696
+ } else {
697
+ return "unknown"
698
+ }
699
+ }
700
+
701
  function setUpdateWindowHeight() {
702
  if (!showingUpdateInfo) {return;}
703
  const scrollPosition = window.scrollY;
assets/html/update.html CHANGED
@@ -14,11 +14,11 @@
14
  </div>
15
  </div>
16
  <div id="goto-update-btn" class="btn-update-group">
17
- <button class="btn-update lg secondary svelte-1ipelgc" id="cancel-button" onclick="cancelUpdate()">{cancel_btn}</button>
18
- <button class="btn-update lg primary svelte-1ipelgc" id="update-button" onclick="getUpdate(); updateChuanhuBtn.click();">{update_btn}</button>
19
  </div>
20
  <div id="close-update-btn" class="btn-update-group hideK">
21
- <button class="btn-update lg secondary svelte-1jrzxu" id="update-button" onclick="getUpdate()">{seenew_btn}</button>
22
  <button class="btn-update lg primary svelte-1jrzxu" id="cancel-button" onclick="cancelUpdate()">{ok_btn}</button>
23
  </div>
24
  </div>
 
14
  </div>
15
  </div>
16
  <div id="goto-update-btn" class="btn-update-group">
17
+ <button class="btn-update lg secondary svelte-1jrzxu" id="cancel-button" onclick="cancelUpdate()">{cancel_btn}</button>
18
+ <button class="btn-update lg primary svelte-1jrzxu" id="update-button" onclick="bgUpdateChuanhu()">{update_btn}</button>
19
  </div>
20
  <div id="close-update-btn" class="btn-update-group hideK">
21
+ <button class="btn-update lg secondary svelte-1jrzxu" id="update-button" onclick="getUpdateInfo()">{seenew_btn}</button>
22
  <button class="btn-update lg primary svelte-1jrzxu" id="cancel-button" onclick="cancelUpdate()">{ok_btn}</button>
23
  </div>
24
  </div>
modules/utils.py CHANGED
@@ -594,11 +594,13 @@ def update_chuanhu():
594
  git = os.environ.get('GIT', "git")
595
  try:
596
  run(f"{git} fetch --all && {git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop")
597
- logging.info("更新成功,重启中……")
598
- return gr.Markdown.update(value="更新成功,重启中……")
 
599
  except Exception:
600
- logging.info("更新失败")
601
- return gr.Markdown.update(value="更新失败")
 
602
 
603
  def get_html(filename):
604
  path = os.path.join(shared.chuanhu_path, "assets", "html", filename)
 
594
  git = os.environ.get('GIT', "git")
595
  try:
596
  run(f"{git} fetch --all && {git} stash && {git} pull https://github.com/GaiZhenbiao/ChuanhuChatGPT.git main -f && {git} stash pop")
597
+ logging.info("Successfully updated")
598
+ status = '<span id="update-status" class="hideK">success</span>'
599
+ return gr.Markdown.update(value=i18n("更新成功,请重启本程序")+status)
600
  except Exception:
601
+ logging.info("Failed to update")
602
+ status = '<span id="update-status" class="hideK">failure</span>'
603
+ return gr.Markdown.update(value=i18n("更新失败,请尝试[手动更新](https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新)")+status)
604
 
605
  def get_html(filename):
606
  path = os.path.join(shared.chuanhu_path, "assets", "html", filename)