-
Notifications
You must be signed in to change notification settings - Fork 32
Closed
Description
## Bug Report: `InvalidCharacterError` when using Unicode characters (e.g., Chinese)
### Description
The plugin fails to load when the configuration (Server URL, Vault path, or other settings) contains non-Latin1 characters (such as Chinese characters). This is caused by a direct call to the `btoa()` function, which does not support Unicode strings.
### Error Logs
```text
Plugin failure: opencode-obsidian InvalidCharacterError: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
at ProcessManager.getUrl (plugin:opencode-obsidian:486:25)
at OpenCodePlugin.getServerUrl (plugin:opencode-obsidian:1079:32)
at OpenCodePlugin.onload (plugin:opencode-obsidian:943:73)
Steps to Reproduce
- Set a "Server URL" or have a Vault path that contains Chinese characters (e.g.,
C:/用户/Notes). - Enable the plugin.
- The plugin fails to load and throws the error in the console.
Root Cause Analysis
The error occurs in ProcessManager.getUrl at line 486. The code likely uses window.btoa() to encode a string into Base64. However, btoa() only supports characters in the U+0000 to U+00FF range. Any character outside this (like UTF-8 characters) will trigger this exception.
Suggested Fix
To support Unicode strings, you can use the following pattern to encode the string properly before passing it to btoa:
// Replace direct btoa() calls with a Unicode-safe version
const safeBtoa = (str: string) => {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => {
return String.fromCharCode(parseInt(p1, 16));
}));
};Alternatively, since Obsidian runs in an environment with Node.js support, you can use Buffer:
const encoded = Buffer.from(inputString).toString('base64');Environment:
- Obsidian Version: [1.11.7]
- Plugin Version: [0.1.0]
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels