Keldos commited on
Commit
7795280
1 Parent(s): ab5870b

refactor: 删去原有复制代码块内容相关js

Browse files
Files changed (1) hide show
  1. assets/custom.js +0 -38
assets/custom.js CHANGED
@@ -377,41 +377,6 @@ function addChuanhuButton(botElement) {
377
  botElement.insertBefore(toggleButton, copyButton);
378
  }
379
 
380
- function addCopyCodeButton(pre) {
381
- var code = null;
382
- var firstChild = null;
383
- code = pre.querySelector('code');
384
- if (!code) return;
385
- firstChild = code.querySelector('div');
386
- if (!firstChild) return;
387
- var oldCopyButton = null;
388
- oldCopyButton = code.querySelector('button.copy-code-btn');
389
- // if (oldCopyButton) oldCopyButton.remove();
390
- if (oldCopyButton) return; // 没太有用,新生成的对话中始终会被pre覆盖,导致按钮消失,这段代码不启用……
391
- var codeButton = document.createElement('button');
392
- codeButton.classList.add('copy-code-btn');
393
- codeButton.textContent = '\uD83D\uDCCE';
394
-
395
- code.insertBefore(codeButton, firstChild);
396
- codeButton.addEventListener('click', function () {
397
- var range = document.createRange();
398
- range.selectNodeContents(code);
399
- range.setStartBefore(firstChild);
400
- navigator.clipboard
401
- .writeText(range.toString())
402
- .then(() => {
403
- codeButton.textContent = '\u2714';
404
- setTimeout(function () {
405
- codeButton.textContent = '\uD83D\uDCCE';
406
- }, 2000);
407
- })
408
- .catch(e => {
409
- console.error(e);
410
- codeButton.textContent = '\u2716';
411
- });
412
- });
413
- }
414
-
415
  function renderMarkdownText(message) {
416
  var mdDiv = message.querySelector('.md-message');
417
  if (mdDiv) mdDiv.classList.remove('hideM');
@@ -491,7 +456,6 @@ var mObserver = new MutationObserver(function (mutationsList) {
491
  }
492
  saveHistoryHtml();
493
  document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
494
- document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot pre').forEach(addCopyCodeButton);
495
  }
496
  if (node.tagName === 'INPUT' && node.getAttribute('type') === 'range') {
497
  setSlider();
@@ -505,12 +469,10 @@ var mObserver = new MutationObserver(function (mutationsList) {
505
  }
506
  saveHistoryHtml();
507
  document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
508
- document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot pre').forEach(addCopyCodeButton);
509
  }
510
  }
511
  } else if (mmutation.type === 'attributes') {
512
  if (mmutation.target.nodeType === 1 && mmutation.target.classList.contains('message') && mmutation.target.getAttribute('data-testid') === 'bot') {
513
- document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot pre').forEach(addCopyCodeButton); // 目前写的是有点问题的,会导致加button次数过多,但是bot对话内容生成时又是不断覆盖pre的……
514
  if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
515
  isThrottled = true;
516
  clearTimeout(timeoutId);
 
377
  botElement.insertBefore(toggleButton, copyButton);
378
  }
379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
  function renderMarkdownText(message) {
381
  var mdDiv = message.querySelector('.md-message');
382
  if (mdDiv) mdDiv.classList.remove('hideM');
 
456
  }
457
  saveHistoryHtml();
458
  document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
 
459
  }
460
  if (node.tagName === 'INPUT' && node.getAttribute('type') === 'range') {
461
  setSlider();
 
469
  }
470
  saveHistoryHtml();
471
  document.querySelectorAll('#chuanhu_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton);
 
472
  }
473
  }
474
  } else if (mmutation.type === 'attributes') {
475
  if (mmutation.target.nodeType === 1 && mmutation.target.classList.contains('message') && mmutation.target.getAttribute('data-testid') === 'bot') {
 
476
  if (isThrottled) break; // 为了防止重复不断疯狂渲染,加上等待_(:з」∠)_
477
  isThrottled = true;
478
  clearTimeout(timeoutId);