Perplexity 代码框限制高度

限制Perplexity网站代码框高度为400px并添加按钮

  1. // ==UserScript==
  2. // @name Perplexity 代码框限制高度
  3. // @name:en Perplexity Code Height Limiter
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.2
  6. // @description 限制Perplexity网站代码框高度为400px并添加按钮
  7. // @description:en Limit code block height to 400px and add copy button on Perplexity
  8. // @author Dost
  9. // @match https://www.perplexity.ai/*
  10. // @grant GM_addStyle
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. GM_addStyle(`
  18. pre {
  19. max-height: 400px !important;
  20. overflow-y: auto !important;
  21. position: relative !important;
  22. }
  23.  
  24. .copy-button {
  25. position: sticky !important;
  26. bottom: 10px !important;
  27. right: 10px !important;
  28. float: right !important;
  29. background-color: #4a5568 !important;
  30. color: white !important;
  31. border: none !important;
  32. border-radius: 4px !important;
  33. padding: 5px 10px !important;
  34. cursor: pointer !important;
  35. opacity: 0 !important;
  36. transition: opacity 0.3s ease !important;
  37. z-index: 1000 !important;
  38. }
  39.  
  40. pre:hover .copy-button {
  41. opacity: 1 !important;
  42. }
  43.  
  44. .copy-button:hover {
  45. background-color: #2d3748 !important;
  46. }
  47. `);
  48.  
  49. function addCopyButtonToCodeBlocks() {
  50. const codeBlocks = document.querySelectorAll('pre');
  51.  
  52. codeBlocks.forEach(pre => {
  53. if (!pre.querySelector('.copy-button')) {
  54. const copyButton = document.createElement('button');
  55. copyButton.textContent = '复制';
  56. copyButton.className = 'copy-button';
  57.  
  58. copyButton.addEventListener('click', async () => {
  59. // 获取code元素中的文本内容
  60. const codeElement = pre.querySelector('code');
  61. const code = codeElement ? codeElement.textContent : pre.textContent;
  62. const cleanCode = code.replace('复制', '').trim();
  63. try {
  64. await navigator.clipboard.writeText(cleanCode);
  65. copyButton.textContent = '已复制!';
  66. setTimeout(() => {
  67. copyButton.textContent = '复制';
  68. }, 2000);
  69. } catch (err) {
  70. copyButton.textContent = '复制失败';
  71. setTimeout(() => {
  72. copyButton.textContent = '复制';
  73. }, 2000);
  74. }
  75. });
  76.  
  77. pre.appendChild(copyButton);
  78. }
  79. });
  80. }
  81.  
  82. const observer = new MutationObserver(() => {
  83. addCopyButtonToCodeBlocks();
  84. });
  85.  
  86. observer.observe(document.body, {
  87. childList: true,
  88. subtree: true
  89. });
  90.  
  91. addCopyButtonToCodeBlocks();
  92. })();

QingJ © 2025

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