SDK Properties
This document describes the properties available in GM Web SDK. The GM host app injects these properties automatically, giving your app the context and configuration data it needs.
Usage
Every property is available on the global $gm object, for example $gm.token and $gm.data.
Authentication
token
User authentication token, used for secure communication with backend services.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| token | Authentication token of the signed-in user | string | - | Yes |
Example:
// Get the user authentication token
const userToken = $gm.token;
console.log('用户令牌:', userToken);
// Use the token in an API request
fetch('/api/user/profile', {
headers: {
'Authorization': `Bearer ${$gm.token}`
}
});App data
data
Custom data the GM host app passes to the current app. It can be any data structure.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| data | Custom data passed by the host app | any | null | No |
Example:
// Get the data passed by the host app
const appData = $gm.data;
console.log('应用数据:', appData);
// Handle different kinds of data
if (appData) {
if (typeof appData === 'object') {
// Handle object data
console.log('配置信息:', appData.config);
} else if (typeof appData === 'string') {
// Handle string data
console.log('文本数据:', appData);
}
}fileId
Unique file identifier generated automatically when the app starts, used for file operations and app instance management.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| fileId | Unique file identifier of the app instance | string | - | Yes |
Example:
// Get the app file ID
const appFileId = $gm.fileId;
console.log('应用文件ID:', appFileId);
// Use it in a file operation
$gm.file.save({
fileId: $gm.fileId,
content: 'Hello World'
});name
Name identifier of the current app.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| name | Name of the current app | string | - | Yes |
Example:
// Get the app name
const appName = $gm.name;
console.log('当前应用:', appName);
// Show the app name in the page title
document.title = `${appName} - GM 应用`;Environment information
webURL
Current URL of the host app.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| webURL | Current URL of the host app | string | - | Yes |
Example:
// Get the host app URL
const mainAppURL = $gm.webURL;
console.log('主应用URL:', mainAppURL);
// Parse the URL
const url = new URL(mainAppURL);
console.log('域名:', url.hostname);
console.log('路径:', url.pathname);host
Host address of the currently connected server.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| host | Server host address | string | - | Yes |
Example:
// Get the server host
const serverHost = $gm.host;
console.log('服务器主机:', serverHost);
// Build an API request URL
const apiURL = `https://${$gm.host}/api/data`;lang
Current language, kept in sync with the host app; supports internationalization.
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| lang | Current language | string | 'zh-CN' | Yes |
Supported language codes:
zh-CN: Simplified Chineseen-US: Englishja-JP: Japanese- More language codes are being added...
Example:
// Get the current language
const currentLang = $gm.lang;
console.log('当前语言:', currentLang);
// Show different content for each language
const messages = {
'zh-CN': '欢迎使用 GM 应用',
'en-US': 'Welcome to GM App',
'ja-JP': 'GM アプリへようこそ'
};
const welcomeMessage = messages[currentLang] || messages['zh-CN'];
console.log(welcomeMessage);
// Load a language pack dynamically
if (currentLang === 'en-US') {
import('./i18n/en.js').then(langPack => {
// Use the English language pack
});
}Internationalization support
The language property helps you internationalize your app. Based on the value of $gm.lang you can:
- load the matching language pack dynamically
- switch the text shown in the interface
- adjust date and number formats
- adapt to user habits in different regions
Notes
- All properties are read-only; do not try to modify them.
- Some properties (such as token) may contain sensitive information; handle them carefully.
- Check that a property exists before you use it.
