Claude Predefined Prompt

使用自定义命令在Claude中触发预定义的提示语

目前为 2024-03-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Claude Predefined Prompt
  3. // @author FishHawk
  4. // @description Use predefined prompts in Claude by custom command
  5. // @license GPL-3.0
  6. // @namespace https://github.com/FishHawk
  7. // @homepageURL https://github.com/FishHawk/claude-predefined-prompt
  8. // @version 2024-03-14
  9. // @match https://claude.ai/chat*
  10. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  11. // @description:zh-CN 使用自定义命令在Claude中触发预定义的提示语
  12. // ==/UserScript==
  13.  
  14. (async function () {
  15. 'use strict';
  16.  
  17. const prompts = {
  18. commit: '请帮我写一条Git commit message,内容是如下:\n',
  19. };
  20.  
  21. const delay = (ms) => new Promise((r) => setTimeout(r, ms));
  22.  
  23. let content = '';
  24. while (true) {
  25. await delay(100);
  26. const input = document.querySelector("div[enterkeyhint='enter']");
  27. if (input) {
  28. const newContent = input.textContent;
  29. if (newContent !== content) {
  30. content = newContent;
  31.  
  32. for (const key in prompts) {
  33. if (content.trimStart() === `/${key} `) {
  34. // Replace content
  35. input.innerHTML = prompts[key]
  36. .split('\n')
  37. .map((it) => `<p>${it}</p>`)
  38. .join('\n');
  39.  
  40. // Set cursor position
  41. const range = document.createRange();
  42. range.selectNodeContents(input);
  43. range.collapse(false);
  44. const sel = window.getSelection();
  45. sel.removeAllRanges();
  46. sel.addRange(range);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. })();