Claude Predefined Prompt

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Claude Predefined Prompt
// @author       FishHawk
// @description  Use predefined prompts in Claude by custom command
// @license      GPL-3.0
// @namespace    https://github.com/FishHawk
// @homepageURL  https://github.com/FishHawk/claude-predefined-prompt
// @version      2024-03-14
// @match        https://claude.ai/chat*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @description:zh-CN 使用自定义命令在Claude中触发预定义的提示语
// ==/UserScript==

(async function () {
  'use strict';

  const prompts = {
    commit: '请帮我写一条Git commit message,内容是如下:\n',
  };

  const delay = (ms) => new Promise((r) => setTimeout(r, ms));

  let content = '';
  while (true) {
    await delay(100);
    const input = document.querySelector("div[enterkeyhint='enter']");
    if (input) {
      const newContent = input.textContent;
      if (newContent !== content) {
        content = newContent;

        for (const key in prompts) {
          if (content.trimStart() === `/${key} `) {
            // Replace content
            input.innerHTML = prompts[key]
              .split('\n')
              .map((it) => `<p>${it}</p>`)
              .join('\n');

            // Set cursor position
            const range = document.createRange();
            range.selectNodeContents(input);
            range.collapse(false);
            const sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
          }
        }
      }
    }
  }
})();