trustcaptcha-svelte 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -0
- package/dist/captcha.svelte +92 -0
- package/dist/captcha.svelte.d.ts +7 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.js +1 -0
- package/package.json +64 -0
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# TrustCaptcha for Svelte
|
|
2
|
+
|
|
3
|
+
A Svelte 5 component for integrating [TrustCaptcha](https://www.trustcomponent.com/) CAPTCHA widget into your Svelte applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Full Svelte 5 support with runes
|
|
8
|
+
- ✅ TypeScript support with complete type definitions
|
|
9
|
+
- ✅ All TrustCaptcha widget properties, methods, and events
|
|
10
|
+
- ✅ SSR compatible
|
|
11
|
+
- ✅ Customizable themes (light, dark, media)
|
|
12
|
+
- ✅ Custom translations and design
|
|
13
|
+
- ✅ Invisible CAPTCHA mode
|
|
14
|
+
- ✅ Autostart configuration
|
|
15
|
+
- ✅ License-based premium features
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install trustcaptcha-svelte
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
or
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add trustcaptcha-svelte
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
or
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
yarn add trustcaptcha-svelte
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Basic Usage
|
|
36
|
+
|
|
37
|
+
```svelte
|
|
38
|
+
<script lang="ts">
|
|
39
|
+
import { TrustCaptcha } from 'trustcaptcha-svelte';
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<form>
|
|
43
|
+
<TrustCaptcha sitekey="your_site_key_here" />
|
|
44
|
+
</form>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Properties
|
|
48
|
+
|
|
49
|
+
| Property | Type | Default | Description |
|
|
50
|
+
| -------------------- | ---------------------------------------------------------- | ------------------------- | ------------------------------------------------------- |
|
|
51
|
+
| `sitekey` | `string` | **required** | Your CAPTCHA site key from the TrustCaptcha admin panel |
|
|
52
|
+
| `width` | `'fixed' \| 'full'` | `'fixed'` | Width of the component |
|
|
53
|
+
| `language` | `string` | `'auto'` | Display language (auto, en, de, fr, es, etc.) |
|
|
54
|
+
| `theme` | `'light' \| 'dark' \| 'media'` | `'light'` | Visual theme |
|
|
55
|
+
| `autostart` | `boolean` | `true` | Start verification automatically on form interaction |
|
|
56
|
+
| `license` | `string` | - | License key for premium features |
|
|
57
|
+
| `hideBranding` | `boolean` | `false` | Hide TrustCaptcha logo (requires license) |
|
|
58
|
+
| `invisible` | `boolean` | `false` | Enable invisible mode (requires license) |
|
|
59
|
+
| `invisibleHint` | `'inline' \| 'right-border' \| 'right-bottom' \| 'hidden'` | `'right-border'` | Position of invisible mode hint |
|
|
60
|
+
| `bypassToken` | `string` | - | Bypass token for automated testing |
|
|
61
|
+
| `mode` | `'standard' \| 'minimal'` | `'standard'` | Data mode of the CAPTCHA |
|
|
62
|
+
| `tokenFieldName` | `string` | `'tc-verification-token'` | Name of hidden input field for token |
|
|
63
|
+
| `customTranslations` | `string \| CustomTranslation[]` | - | Custom text translations |
|
|
64
|
+
| `customDesign` | `string \| CustomDesign` | - | Custom design configuration (requires license) |
|
|
65
|
+
| `privacyUrl` | `string` | - | Link to your privacy policy (requires license) |
|
|
66
|
+
|
|
67
|
+
## Events
|
|
68
|
+
|
|
69
|
+
| Event | Description | Detail Type |
|
|
70
|
+
| ------------------ | ----------------------------------------- | ------------------------------------------ |
|
|
71
|
+
| `oncaptchaStarted` | Fired when CAPTCHA verification starts | `CustomEvent` |
|
|
72
|
+
| `oncaptchaSolved` | Fired when CAPTCHA is successfully solved | `CustomEvent<string>` (verification token) |
|
|
73
|
+
| `oncaptchaFailed` | Fired when CAPTCHA verification fails | `CustomEvent<CaptchaError>` |
|
|
74
|
+
| `oncaptchaReset` | Fired when CAPTCHA is reset | `CustomEvent` |
|
|
75
|
+
|
|
76
|
+
## Methods
|
|
77
|
+
|
|
78
|
+
You can call these methods using a component reference:
|
|
79
|
+
|
|
80
|
+
```svelte
|
|
81
|
+
<script lang="ts">
|
|
82
|
+
import { TrustCaptcha } from 'trustcaptcha-svelte';
|
|
83
|
+
|
|
84
|
+
let captchaRef: { startVerification: () => void; reset: () => void } | undefined;
|
|
85
|
+
|
|
86
|
+
function manualStart() {
|
|
87
|
+
captchaRef?.startVerification();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function resetCaptcha() {
|
|
91
|
+
captchaRef?.reset();
|
|
92
|
+
}
|
|
93
|
+
</script>
|
|
94
|
+
|
|
95
|
+
<TrustCaptcha bind:this={captchaRef} sitekey="your_site_key_here" />
|
|
96
|
+
|
|
97
|
+
<button onclick={manualStart}>Start CAPTCHA</button>
|
|
98
|
+
<button onclick={resetCaptcha}>Reset CAPTCHA</button>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Available Methods
|
|
102
|
+
|
|
103
|
+
- `startVerification()` - Manually start the CAPTCHA verification
|
|
104
|
+
- `reset()` - Reset the CAPTCHA to initial state
|
|
105
|
+
|
|
106
|
+
## TypeScript Support
|
|
107
|
+
|
|
108
|
+
Full TypeScript definitions are included:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import type {
|
|
112
|
+
TrustCaptchaProps,
|
|
113
|
+
CaptchaError,
|
|
114
|
+
ErrorCode,
|
|
115
|
+
CustomTranslation,
|
|
116
|
+
CustomDesign,
|
|
117
|
+
ThemeColors
|
|
118
|
+
} from 'trustcaptcha-svelte';
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Links
|
|
122
|
+
|
|
123
|
+
- [TrustCaptcha Official Documentation](https://docs.trustcomponent.com/captcha/widget/overview/)
|
|
124
|
+
- [TrustCaptcha Website](https://www.trustcomponent.com/)
|
|
125
|
+
- [Get your Site Key](https://admin.trustcomponent.com/)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { TrustCaptchaProps } from './index.js';
|
|
3
|
+
|
|
4
|
+
let {
|
|
5
|
+
autostart,
|
|
6
|
+
hideBranding,
|
|
7
|
+
invisible,
|
|
8
|
+
invisibleHint,
|
|
9
|
+
bypassToken,
|
|
10
|
+
tokenFieldName,
|
|
11
|
+
customTranslations,
|
|
12
|
+
customDesign,
|
|
13
|
+
privacyUrl,
|
|
14
|
+
oncaptchaStarted,
|
|
15
|
+
oncaptchaSolved,
|
|
16
|
+
oncaptchaFailed,
|
|
17
|
+
oncaptchaReset,
|
|
18
|
+
...rest
|
|
19
|
+
}: TrustCaptchaProps = $props();
|
|
20
|
+
|
|
21
|
+
let captchaElement: HTMLElement | null = $state(null);
|
|
22
|
+
let scriptLoaded = $state(
|
|
23
|
+
typeof customElements !== 'undefined' && !!customElements.get('trustcaptcha-component')
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const customTranslationsStr = $derived(
|
|
27
|
+
typeof customTranslations === 'object' ? JSON.stringify(customTranslations) : customTranslations
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const customDesignStr = $derived(
|
|
31
|
+
typeof customDesign === 'object' ? JSON.stringify(customDesign) : customDesign
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
function onScriptLoad() {
|
|
35
|
+
scriptLoaded = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
$effect(() => {
|
|
39
|
+
if (!captchaElement || !scriptLoaded) return;
|
|
40
|
+
|
|
41
|
+
const handlers: Record<string, EventListener | undefined> = {
|
|
42
|
+
captchaStarted: oncaptchaStarted as EventListener | undefined,
|
|
43
|
+
captchaSolved: oncaptchaSolved as EventListener | undefined,
|
|
44
|
+
captchaFailed: oncaptchaFailed as EventListener | undefined,
|
|
45
|
+
captchaReset: oncaptchaReset as EventListener | undefined
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
for (const [event, handler] of Object.entries(handlers)) {
|
|
49
|
+
if (handler) {
|
|
50
|
+
captchaElement.addEventListener(event, handler);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
for (const [event, handler] of Object.entries(handlers)) {
|
|
56
|
+
if (handler) {
|
|
57
|
+
captchaElement!.removeEventListener(event, handler);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export function startVerification(): void {
|
|
64
|
+
(captchaElement as HTMLElement & { startVerification: () => void })?.startVerification();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function reset(): void {
|
|
68
|
+
(captchaElement as HTMLElement & { reset: () => void })?.reset();
|
|
69
|
+
}
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
<svelte:head>
|
|
73
|
+
<script
|
|
74
|
+
onload={onScriptLoad}
|
|
75
|
+
|
|
76
|
+
src="https://cdn.trustcomponent.com/trustcaptcha/2.1.x/trustcaptcha.esm.min.js"
|
|
77
|
+
></script>
|
|
78
|
+
</svelte:head>
|
|
79
|
+
|
|
80
|
+
<trustcaptcha-component
|
|
81
|
+
bind:this={captchaElement}
|
|
82
|
+
autostart={autostart ? undefined : 'false'}
|
|
83
|
+
hide-branding={hideBranding ? 'true' : undefined}
|
|
84
|
+
invisible={invisible ? 'true' : undefined}
|
|
85
|
+
invisible-hint={invisibleHint}
|
|
86
|
+
bypass-token={bypassToken}
|
|
87
|
+
token-field-name={tokenFieldName}
|
|
88
|
+
custom-translations={customTranslationsStr}
|
|
89
|
+
custom-design={customDesignStr}
|
|
90
|
+
privacy-url={privacyUrl}
|
|
91
|
+
{...rest}
|
|
92
|
+
></trustcaptcha-component>
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export { default as TrustCaptcha } from './captcha.svelte';
|
|
2
|
+
export interface TrustCaptchaProps {
|
|
3
|
+
/** Sitekey of the TrustCaptcha. You can find this on the administration page of your TrustCaptcha. */
|
|
4
|
+
sitekey: string;
|
|
5
|
+
/** Width of the TrustCaptcha component. @default 'fixed' */
|
|
6
|
+
width?: 'fixed' | 'full';
|
|
7
|
+
/** Display language of the TrustCaptcha component. @default 'auto' */
|
|
8
|
+
language?: 'auto' | 'ar' | 'be' | 'bg' | 'bs' | 'ca' | 'cs' | 'da' | 'de' | 'el' | 'en' | 'es' | 'et' | 'fi' | 'fr' | 'hi' | 'hr' | 'hu' | 'it' | 'ko' | 'lb' | 'lt' | 'lv' | 'mk' | 'nl' | 'no' | 'pl' | 'pt' | 'ro' | 'ru' | 'sk' | 'sl' | 'sq' | 'sr' | 'sv' | 'tr' | 'uk' | 'zh';
|
|
9
|
+
/** Display mode of the TrustCaptcha component. @default 'light' */
|
|
10
|
+
theme?: 'light' | 'dark' | 'media';
|
|
11
|
+
/** Setting whether the CAPTCHA should start automatically. @default true */
|
|
12
|
+
autostart?: boolean;
|
|
13
|
+
/** License key for special features such as privacyUrl, hideBranding, customDesign or invisible. */
|
|
14
|
+
license?: string;
|
|
15
|
+
/** Setting to hide the TrustCaptcha logo. License key required. @default false */
|
|
16
|
+
hideBranding?: boolean;
|
|
17
|
+
/** Setting to make the CAPTCHA invisible. License key required. @default false */
|
|
18
|
+
invisible?: boolean;
|
|
19
|
+
/** Shows a small message when the CAPTCHA runs invisibly. License key required. @default 'right-border' */
|
|
20
|
+
invisibleHint?: 'inline' | 'right-border' | 'right-bottom' | 'hidden';
|
|
21
|
+
/** For automated tests, bypass keys can be created in the settings to always pass the CAPTCHA. */
|
|
22
|
+
bypassToken?: string;
|
|
23
|
+
/** Set the data mode of the captcha. Must match the settings of the captcha. @default 'standard' */
|
|
24
|
+
mode?: 'standard' | 'minimal';
|
|
25
|
+
/** Name of the hidden field within the form in which the verification token is provided after a successful verification. @default 'tc-verification-token' */
|
|
26
|
+
tokenFieldName?: string;
|
|
27
|
+
/** Overwrite existing translations and/or add additional languages. JSON array as a string or object. */
|
|
28
|
+
customTranslations?: string | CustomTranslation[];
|
|
29
|
+
/** Custom design adjustments for the TrustCaptcha component. License key required. JSON as a string or object. */
|
|
30
|
+
customDesign?: string | CustomDesign;
|
|
31
|
+
/** Link to your privacy policy. License key required. */
|
|
32
|
+
privacyUrl?: string;
|
|
33
|
+
/** Triggered when the CAPTCHA is started. */
|
|
34
|
+
oncaptchaStarted?: (event: CustomEvent) => void;
|
|
35
|
+
/** Triggered when the CAPTCHA has been successfully solved. */
|
|
36
|
+
oncaptchaSolved?: (event: CustomEvent<string>) => void;
|
|
37
|
+
/** Triggered when solving the CAPTCHA has failed. */
|
|
38
|
+
oncaptchaFailed?: (event: CustomEvent<CaptchaError>) => void;
|
|
39
|
+
/** Triggered when the CAPTCHA is reset to the start setting. */
|
|
40
|
+
oncaptchaReset?: (event: CustomEvent) => void;
|
|
41
|
+
}
|
|
42
|
+
export interface CaptchaError {
|
|
43
|
+
errorCode: ErrorCode;
|
|
44
|
+
message: string;
|
|
45
|
+
}
|
|
46
|
+
export type ErrorCode = 'UNKNOWN_ERROR' | 'NO_FORM_FOUND' | 'COMMUNICATION_FAILURE' | 'NO_SITE_KEY_SPECIFIED' | 'UNAUTHORIZED' | 'SITE_KEY_NOT_VALID' | 'MODES_NOT_MATCHING' | 'CAPTCHA_NOT_ACCESSIBLE' | 'POW_FAILURE' | 'PAYMENT_REQUIRED' | 'LOCKED' | 'LICENSE_INVALID' | 'OPTION_NOT_AVAILABLE';
|
|
47
|
+
export interface CustomTranslation {
|
|
48
|
+
language: string;
|
|
49
|
+
boxStart?: string;
|
|
50
|
+
boxInProgress?: string;
|
|
51
|
+
boxCompleted?: string;
|
|
52
|
+
endPrivacyPolicy?: string;
|
|
53
|
+
ariaLabelStart?: string;
|
|
54
|
+
ariaLabelRunning?: string;
|
|
55
|
+
ariaLabelDone?: string;
|
|
56
|
+
srRunning?: string;
|
|
57
|
+
srDone?: string;
|
|
58
|
+
srFailed?: string;
|
|
59
|
+
srTrustcaptcha?: string;
|
|
60
|
+
srPrivacy?: string;
|
|
61
|
+
}
|
|
62
|
+
export interface CustomDesign {
|
|
63
|
+
rounding?: {
|
|
64
|
+
box?: string;
|
|
65
|
+
checkbox?: string;
|
|
66
|
+
invisibleHint?: string;
|
|
67
|
+
};
|
|
68
|
+
theme?: {
|
|
69
|
+
light?: ThemeColors;
|
|
70
|
+
dark?: ThemeColors;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export interface ThemeColors {
|
|
74
|
+
boxDefaultBackground?: string;
|
|
75
|
+
boxDefaultText?: string;
|
|
76
|
+
boxDefaultBorder?: string;
|
|
77
|
+
boxCheckboxBackground?: string;
|
|
78
|
+
boxCheckboxBorder?: string;
|
|
79
|
+
boxSuccessBackground?: string;
|
|
80
|
+
boxSuccessBorder?: string;
|
|
81
|
+
invisibleHintBackground?: string;
|
|
82
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as TrustCaptcha } from './captcha.svelte';
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "trustcaptcha-svelte",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"!dist/**/*.test.*",
|
|
8
|
+
"!dist/**/*.spec.*"
|
|
9
|
+
],
|
|
10
|
+
"sideEffects": [
|
|
11
|
+
"**/*.css"
|
|
12
|
+
],
|
|
13
|
+
"svelte": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"svelte": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"svelte": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eslint/compat": "^2.0.1",
|
|
27
|
+
"@eslint/js": "^9.39.2",
|
|
28
|
+
"@sveltejs/adapter-auto": "^7.0.0",
|
|
29
|
+
"@sveltejs/kit": "^2.50.0",
|
|
30
|
+
"@sveltejs/package": "^2.5.7",
|
|
31
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
32
|
+
"@types/node": "^25.0.9",
|
|
33
|
+
"@vitest/browser-playwright": "^4.0.17",
|
|
34
|
+
"eslint": "^9.39.2",
|
|
35
|
+
"eslint-config-prettier": "^10.1.8",
|
|
36
|
+
"eslint-plugin-svelte": "^3.14.0",
|
|
37
|
+
"globals": "^17.0.0",
|
|
38
|
+
"playwright": "^1.57.0",
|
|
39
|
+
"prettier": "^3.8.1",
|
|
40
|
+
"prettier-plugin-svelte": "^3.4.1",
|
|
41
|
+
"publint": "^0.3.17",
|
|
42
|
+
"svelte": "^5.47.1",
|
|
43
|
+
"svelte-check": "^4.3.5",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"typescript-eslint": "^8.53.1",
|
|
46
|
+
"vite": "^7.3.1",
|
|
47
|
+
"vitest": "^4.0.17",
|
|
48
|
+
"vitest-browser-svelte": "^2.0.1"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"svelte"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "vite dev",
|
|
55
|
+
"build": "vite build && npm run prepack",
|
|
56
|
+
"preview": "vite preview",
|
|
57
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
58
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
59
|
+
"format": "prettier --write .",
|
|
60
|
+
"lint": "prettier --check . && eslint .",
|
|
61
|
+
"test:unit": "vitest",
|
|
62
|
+
"test": "npm run test:unit -- --run"
|
|
63
|
+
}
|
|
64
|
+
}
|