// ==UserScript==
// @name 幻觉(Illusion)
// @icon https://raw.githubusercontent.com/cattail-mutt/Illusion/refs/heads/main/resources/icons/illusion.png
// @namespace https://github.com/cattail-mutt
// @version 1.3
// @description 幻觉(Illusion)是一个精简的跨平台 Prompts 管理工具,支持在以下 AI 平台使用:Google AI Studio, OpenAI ChatGPT, Anthropic Claude 和 DeepSeek Chat。
// @author Mukai
// @license MIT
// @match https://aistudio.google.com/*
// @match https://chatgpt.com/*
// @match https://claude.ai/*
// @match https://chat.deepseek.com/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getResourceText
// @resource PROMPTS https://raw.githubusercontent.com/cattail-mutt/Illusion/refs/heads/main/resources/config/prompts.yaml
// @resource THEMES https://raw.githubusercontent.com/cattail-mutt/Illusion/refs/heads/main/resources/config/themes.json
// @resource CSS https://raw.githubusercontent.com/cattail-mutt/Illusion/refs/heads/main/resources/styles/illusion.css
// @require https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js
// @homepage https://greasyfork.org/zh-CN/scripts/527451-%E5%B9%BB%E8%A7%89-illusion
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
let modalRef = null;
let overlayRef = null;
let savedPrompts = {};
window.onerror = function(msg, url, line, col, error) {
console.error('[Illusion]错误:', {msg, url, line, col, error});
return false;
};
const debug = {
enabled: true,
log: (...args) => debug.enabled && console.log('[Illusion]日志:', ...args),
error: (...args) => console.error('[Illusion]错误:', ...args),
warn: (...args) => console.warn('[Illusion]警告:', ...args),
trace: (...args) => debug.enabled && console.trace('[Illusion]追踪:', ...args)
};
const initialPrompts = jsyaml.load(GM_getResourceText('PROMPTS'));
const THEMECONFIG = JSON.parse(GM_getResourceText('THEMES'));
debug.log('PROMPTS解析结果:', initialPrompts);
debug.log('THEMES解析结果:', THEMECONFIG);
function dispatchEvents(element, events) {
events.forEach(eventName => {
const event = eventName === 'input'
? new InputEvent(eventName, { bubbles: true })
: new Event(eventName, { bubbles: true });
element.dispatchEvent(event);
});
}
// ChatGPT 和 Claude 均使用了 ProseMirror 库构建富文本编辑器
function createParagraph(line) {
const p = document.createElement('p');
if (line.trim()) {
p.textContent = line;
} else {
p.innerHTML = '
';
}
return p;
}
const updateProseMirror = (editor, prompt) => {
const paragraphs = Array.from(editor.querySelectorAll('p'));
let currentContent = '';
paragraphs.forEach(p => {
const text = p.textContent.trim();
if (text) {
currentContent += text + '\n';
} else {
currentContent += '\n';
}
});
let newContent = currentContent.trim();
if (newContent) {
newContent += '\n';
}
editor.innerHTML = '';
if (newContent) {
newContent.split('\n').forEach(line => {
editor.appendChild(createParagraph(line));
});
}
const lines = prompt.split('\n');
lines.forEach((line, index) => {
editor.appendChild(createParagraph(line));
if (index < lines.length - 1 && !line.trim()) {
const brP = document.createElement('p');
brP.innerHTML = '
';
editor.appendChild(brP);
}
});
dispatchEvents(editor, ['input', 'change']);
};
// Gemini 和 DeepSeek 使用的均是纯文本输入框