@@ -343,10 +343,10 @@ For complex plugins that need full control over settings UI, use `customSettings
343343 const result = await verifyApiKey (input .value .trim ());
344344
345345 if (result .success ) {
346- status .innerHTML = ` <span style =" color :var (--success-color );" >✓ ${ t (' verifySuccess' )} </span >` ;
346+ status .innerHTML = ` <span style =" color :var (--success-color );" >${ t (' verifySuccess' )} </span >` ;
347347 input .value = ' ' ;
348348 } else {
349- status .innerHTML = ` <span style =" color :var (--error-color );" >✗ ${ t (' verifyFailed' )} </span >` ;
349+ status .innerHTML = ` <span style =" color :var (--error-color );" >${ t (' verifyFailed' )} </span >` ;
350350 }
351351 btn .textContent = t (' verify' );
352352 },
@@ -651,10 +651,10 @@ To distribute your plugin:
651651 - **Direct Download**: Host on GitHub releases or your website
652652
6536535. **Common issues**:
654- - ❌ Zip contains nested folders: ` your- plugin .zip / your- plugin/ your- plugin/ manifest .json `
655- - ✅ Correct structure: ` your- plugin .zip / your- plugin/ manifest .json `
656- - ❌ Files outside plugin folder
657- - ✅ All files inside single plugin folder
654+ - Wrong: Zip contains nested folders: ` your- plugin .zip / your- plugin/ your- plugin/ manifest .json `
655+ - Correct structure: ` your- plugin .zip / your- plugin/ manifest .json `
656+ - Wrong: Files outside plugin folder
657+ - Correct: All files inside single plugin folder
658658
659659### Advanced: Local Dependencies and CSS Loading
660660
@@ -747,13 +747,13 @@ ChatRawPlugin.hooks.register('after_receive', {
74774711. **Always ` await ` async operations**: When calling async functions (like ` saveSettings ()` ) in event handlers, always use ` await ` to ensure operations complete before proceeding:
748748
749749` ` ` javascript
750- // ❌ Wrong - data may not be saved
750+ // Wrong - data may not be saved
751751button .onclick = () => {
752752 saveSettings (); // Missing await!
753753 renderUI ();
754754};
755755
756- // ✅ Correct - wait for save to complete
756+ // Correct - wait for save to complete
757757button .onclick = async () => {
758758 await saveSettings ();
759759 renderUI ();
@@ -811,12 +811,12 @@ Watch out for these common mistakes:
8118111. **Wrong IIFE pattern**: Always use the parameter-passing pattern for cleaner code:
812812
813813```javascript
814- // ❌ Wrong - direct global access
814+ // Wrong - direct global access
815815(function() {
816816 window.ChatRawPlugin.hooks.register(...);
817817})();
818818
819- // ✅ Correct - pass as parameter
819+ // Correct - pass as parameter
820820(function(ChatRaw) {
821821 if (!ChatRaw || !ChatRaw.hooks) {
822822 console.error('[YourPlugin] ChatRawPlugin not available');
@@ -829,20 +829,20 @@ Watch out for these common mistakes:
8298292. **Wrong API method names**: Use the correct method names:
830830
831831```javascript
832- // ❌ Wrong - getLang doesn't exist
832+ // Wrong - getLang doesn't exist
833833const lang = ChatRaw.utils?.getLang?.() || 'en';
834834
835- // ✅ Correct - use getLanguage
835+ // Correct - use getLanguage
836836const lang = ChatRaw.utils?.getLanguage?.() || 'en';
837837```
838838
8398393. **Missing optional chaining**: Always use `?.` for potentially undefined methods:
840840
841841```javascript
842- // ❌ Risky - may throw error if utils is undefined
842+ // Risky - may throw error if utils is undefined
843843ChatRaw.utils.showToast('Message', 'info');
844844
845- // ✅ Safe - handles undefined gracefully
845+ // Safe - handles undefined gracefully
846846ChatRaw.utils?.showToast?.('Message', 'info');
847847```
848848
@@ -851,25 +851,25 @@ ChatRaw.utils?.showToast?.('Message', 'info');
8518515. **Icon format for toolbar buttons**: Must use RemixIcon format (`ri-xxx-line` or `ri-xxx-fill`):
852852
853853```javascript
854- // ❌ Wrong - will be rejected
854+ // Wrong - will be rejected
855855icon: 'fa-home' // FontAwesome
856856icon: 'mdi-home' // Material Design Icons
857857icon: 'icon-home' // Custom class
858858
859- // ✅ Correct - RemixIcon format
859+ // Correct - RemixIcon format
860860icon: 'ri-home-line' // Line style
861861icon: 'ri-home-fill' // Fill style
862862```
863863
8648646. **Registering toolbar buttons in hook callbacks**: Buttons must be registered immediately when the script loads, not inside hooks:
865865
866866```javascript
867- // ❌ Wrong - hook callback runs after _currentLoadingPlugin is cleared
867+ // Wrong - hook callback runs after _currentLoadingPlugin is cleared
868868ChatRaw.hooks.register('before_send', () => {
869869 ChatRaw.ui.registerToolbarButton({ ... }); // Will fail!
870870});
871871
872- // ✅ Correct - register immediately in IIFE
872+ // Correct - register immediately in IIFE
873873(function(ChatRaw) {
874874 // Register buttons here, during script load
875875 ChatRaw.ui.registerToolbarButton({ ... }); // Works!
@@ -1219,10 +1219,10 @@ your-plugin/
12191219 const result = await verifyApiKey(input.value.trim());
12201220
12211221 if (result.success) {
1222- status.innerHTML = `<span style="color:var(--success-color);">✓ ${t('verifySuccess')}</span>`;
1222+ status.innerHTML = `<span style="color:var(--success-color);">${t('verifySuccess')}</span>`;
12231223 input.value = '';
12241224 } else {
1225- status.innerHTML = `<span style="color:var(--error-color);">✗ ${t('verifyFailed')}</span>`;
1225+ status.innerHTML = `<span style="color:var(--error-color);">${t('verifyFailed')}</span>`;
12261226 }
12271227 btn.textContent = t('verify');
12281228 },
@@ -1527,10 +1527,10 @@ const allData = ChatRaw.storage.getAll(PLUGIN_ID);
15271527 - **直接下载**:托管在 GitHub releases 或你的网站
15281528
152915295. **常见问题**:
1530- - ❌ zip 包含嵌套文件夹:`your-plugin.zip/your-plugin/your-plugin/manifest.json`
1531- - ✅ 正确结构:`your-plugin.zip/your-plugin/manifest.json`
1532- - ❌ 文件在插件文件夹外
1533- - ✅ 所有文件在单个插件文件夹内
1530+ - 错误: zip 包含嵌套文件夹:`your-plugin.zip/your-plugin/your-plugin/manifest.json`
1531+ - 正确结构:`your-plugin.zip/your-plugin/manifest.json`
1532+ - 错误: 文件在插件文件夹外
1533+ - 正确: 所有文件在单个插件文件夹内
15341534
15351535### 进阶:本地依赖和 CSS 加载
15361536
@@ -1623,13 +1623,13 @@ ChatRawPlugin.hooks.register('after_receive', {
1623162311. **异步操作必须 `await`**:在事件处理函数中调用异步函数(如 `saveSettings()`)时,必须使用 `await` 确保操作完成后再继续:
16241624
16251625```javascript
1626- // ❌ 错误 - 数据可能未保存
1626+ // 错误 - 数据可能未保存
16271627button.onclick = () => {
16281628 saveSettings(); // 缺少 await!
16291629 renderUI();
16301630};
16311631
1632- // ✅ 正确 - 等待保存完成
1632+ // 正确 - 等待保存完成
16331633button.onclick = async () => {
16341634 await saveSettings();
16351635 renderUI();
@@ -1687,12 +1687,12 @@ button.onclick = async () => {
168716871. **错误的 IIFE 模式**:始终使用参数传递模式以获得更清晰的代码:
16881688
16891689```javascript
1690- // ❌ 错误 - 直接访问全局变量
1690+ // 错误 - 直接访问全局变量
16911691(function() {
16921692 window.ChatRawPlugin.hooks.register(...);
16931693})();
16941694
1695- // ✅ 正确 - 作为参数传递
1695+ // 正确 - 作为参数传递
16961696(function(ChatRaw) {
16971697 if (!ChatRaw || !ChatRaw.hooks) {
16981698 console.error('[YourPlugin] ChatRawPlugin 不可用');
@@ -1705,20 +1705,20 @@ button.onclick = async () => {
170517052. **错误的 API 方法名**:请使用正确的方法名:
17061706
17071707```javascript
1708- // ❌ 错误 - getLang 方法不存在
1708+ // 错误 - getLang 方法不存在
17091709const lang = ChatRaw.utils?.getLang?.() || 'en';
17101710
1711- // ✅ 正确 - 使用 getLanguage
1711+ // 正确 - 使用 getLanguage
17121712const lang = ChatRaw.utils?.getLanguage?.() || 'en';
17131713```
17141714
171517153. **缺少可选链操作符**:对于可能未定义的方法,始终使用 `?.`:
17161716
17171717```javascript
1718- // ❌ 有风险 - 如果 utils 未定义会报错
1718+ // 有风险 - 如果 utils 未定义会报错
17191719ChatRaw.utils.showToast('消息', 'info');
17201720
1721- // ✅ 安全 - 优雅处理未定义情况
1721+ // 安全 - 优雅处理未定义情况
17221722ChatRaw.utils?.showToast?.('消息', 'info');
17231723```
17241724
@@ -1727,25 +1727,25 @@ ChatRaw.utils?.showToast?.('消息', 'info');
172717275. **工具栏按钮图标格式**:必须使用 RemixIcon 格式(`ri-xxx-line` 或 `ri-xxx-fill`):
17281728
17291729```javascript
1730- // ❌ 错误 - 会被拒绝
1730+ // 错误 - 会被拒绝
17311731icon: 'fa-home' // FontAwesome
17321732icon: 'mdi-home' // Material Design Icons
17331733icon: 'icon-home' // 自定义类名
17341734
1735- // ✅ 正确 - RemixIcon 格式
1735+ // 正确 - RemixIcon 格式
17361736icon: 'ri-home-line' // 线条样式
17371737icon: 'ri-home-fill' // 填充样式
17381738```
17391739
174017406. **在 hook 回调中注册工具栏按钮**:按钮必须在脚本加载时立即注册,不能在 hook 中注册:
17411741
17421742```javascript
1743- // ❌ 错误 - hook 回调执行时 _currentLoadingPlugin 已被清除
1743+ // 错误 - hook 回调执行时 _currentLoadingPlugin 已被清除
17441744ChatRaw.hooks.register('before_send', () => {
17451745 ChatRaw.ui.registerToolbarButton({ ... }); // 会失败!
17461746});
17471747
1748- // ✅ 正确 - 在 IIFE 中立即注册
1748+ // 正确 - 在 IIFE 中立即注册
17491749(function(ChatRaw) {
17501750 // 在这里注册按钮,脚本加载期间
17511751 ChatRaw.ui.registerToolbarButton({ ... }); // 正常工作!
0 commit comments