@prosopo/procaptcha-bundle 0.2.16 → 0.2.19

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/src/index.html DELETED
@@ -1,11 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8"/>
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
- <title>Prosopo CAPTCHA</title>
7
- </head>
8
- <body>
9
- <div id="root"></div>
10
- </body>
11
- </html>
package/src/index.tsx DELETED
@@ -1,219 +0,0 @@
1
- // Copyright 2021-2023 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- import {
15
- ApiParams,
16
- EnvironmentTypesSchema,
17
- NetworkNamesSchema,
18
- ProcaptchaConfigSchema,
19
- ProcaptchaOutput,
20
- } from '@prosopo/types'
21
- import { Procaptcha } from '@prosopo/procaptcha-react'
22
- import { ProcaptchaConfigOptional } from '@prosopo/procaptcha'
23
- import { at } from '@prosopo/util'
24
- import { createRoot } from 'react-dom/client'
25
- interface ProcaptchaRenderOptions {
26
- siteKey: string
27
- theme?: 'light' | 'dark'
28
- callback?: string
29
- 'challenge-valid-length'?: string // seconds for successful challenge to be valid
30
- 'chalexpired-callback'?: string
31
- 'expired-callback'?: string
32
- 'open-callback'?: string
33
- 'close-callback'?: string
34
- 'error-callback'?: string
35
- }
36
-
37
- type ProcaptchaUrlParams = {
38
- onloadUrlCallback: string | undefined
39
- renderExplicit: string | undefined
40
- }
41
-
42
- const BUNDLE_NAME = 'procaptcha.bundle.js'
43
-
44
- const getCurrentScript = () =>
45
- document && document.currentScript && 'src' in document.currentScript && document.currentScript.src !== undefined
46
- ? document.currentScript
47
- : undefined
48
-
49
- const extractParams = (name: string): ProcaptchaUrlParams => {
50
- const script = getCurrentScript()
51
- if (script && script.src.indexOf(`${name}`) !== -1) {
52
- const params = new URLSearchParams(script.src.split('?')[1])
53
- return {
54
- onloadUrlCallback: params.get('onload') || undefined,
55
- renderExplicit: params.get('render') || undefined,
56
- }
57
- }
58
- return { onloadUrlCallback: undefined, renderExplicit: undefined }
59
- }
60
-
61
- const getConfig = (siteKey?: string): ProcaptchaConfigOptional => {
62
- if (!siteKey) {
63
- siteKey = process.env.PROSOPO_SITE_KEY || ''
64
- }
65
- return ProcaptchaConfigSchema.parse({
66
- defaultEnvironment: process.env.PROSOPO_DEFAULT_ENVIRONMENT
67
- ? EnvironmentTypesSchema.parse(process.env.PROSOPO_DEFAULT_ENVIRONMENT)
68
- : EnvironmentTypesSchema.enum.development,
69
- defaultNetwork: process.env.PROSOPO_DEFAULT_NETWORK
70
- ? NetworkNamesSchema.parse(process.env.PROSOPO_DEFAULT_NETWORK)
71
- : NetworkNamesSchema.enum.development,
72
- userAccountAddress: '',
73
- account: {
74
- address: siteKey,
75
- },
76
- serverUrl: process.env.PROSOPO_SERVER_URL || '',
77
- })
78
- }
79
-
80
- const getParentForm = (element: Element): HTMLFormElement | null => element.closest('form') as HTMLFormElement
81
-
82
- const getWindowCallback = (callbackName: string) => {
83
- const fn = (window as any)[callbackName.replace('window.', '')]
84
- if (typeof fn !== 'function') {
85
- throw new Error(`Callback ${callbackName} is not defined on the window object`)
86
- }
87
- return fn
88
- }
89
-
90
- const handleOnHuman = (element: Element, payload: ProcaptchaOutput) => {
91
- const form = getParentForm(element)
92
-
93
- if (!form) {
94
- console.error('Parent form not found for the element:', element)
95
- return
96
- }
97
-
98
- const input = document.createElement('input')
99
- input.type = 'hidden'
100
- input.name = ApiParams.procaptchaResponse
101
- input.value = JSON.stringify(payload)
102
- form.appendChild(input)
103
- }
104
-
105
- const customThemeSet = new Set(['light', 'dark'])
106
- const validateTheme = (themeAttribute: string): 'light' | 'dark' =>
107
- customThemeSet.has(themeAttribute) ? (themeAttribute as 'light' | 'dark') : 'light'
108
-
109
- const renderLogic = (
110
- elements: Element[],
111
- config: ProcaptchaConfigOptional,
112
- renderOptions?: ProcaptchaRenderOptions
113
- ) => {
114
- elements.forEach((element) => {
115
- const callbackName = renderOptions?.callback || element.getAttribute('data-callback')
116
- const chalExpiredCallbackName =
117
- renderOptions?.['chalexpired-callback'] || element.getAttribute('data-chalexpired-callback')
118
- const errorCallback = renderOptions?.['error-callback'] || element.getAttribute('data-error-callback')
119
- const onCloseCallbackName = renderOptions?.['close-callback'] || element.getAttribute('data-close-callback')
120
- const onOpenCallbackName = renderOptions?.['open-callback'] || element.getAttribute('data-open-callback')
121
- const onExpiredCallbackName =
122
- renderOptions?.['expired-callback'] || element.getAttribute('data-expired-callback')
123
-
124
- // Setting up default callbacks object
125
- const callbacks = {
126
- onHuman: (payload: ProcaptchaOutput) => handleOnHuman(element, payload),
127
- onChallengeExpired: () => {
128
- console.log('Challenge expired')
129
- },
130
- onExpired: () => {
131
- alert('Completed challenge has expired, please try again')
132
- },
133
- onError: (error: Error) => {
134
- console.error(error)
135
- },
136
- onClose: () => {
137
- console.log('Challenge closed')
138
- },
139
- onOpen: () => {
140
- console.log('Challenge opened')
141
- },
142
- }
143
-
144
- if (callbackName) callbacks.onHuman = getWindowCallback(callbackName)
145
- if (chalExpiredCallbackName) callbacks.onChallengeExpired = getWindowCallback(chalExpiredCallbackName)
146
- if (onExpiredCallbackName) callbacks.onExpired = getWindowCallback(onExpiredCallbackName)
147
- if (errorCallback) callbacks.onError = getWindowCallback(errorCallback)
148
- if (onCloseCallbackName) callbacks.onClose = getWindowCallback(onCloseCallbackName)
149
- if (onOpenCallbackName) callbacks.onOpen = getWindowCallback(onOpenCallbackName)
150
-
151
- // Getting and setting the theme
152
- const themeAttribute = renderOptions?.theme || element.getAttribute('data-theme') || 'light'
153
- config.theme = validateTheme(themeAttribute)
154
-
155
- // Getting and setting the challenge valid length
156
- const challengeValidLengthAttribute =
157
- renderOptions?.['challenge-valid-length'] || element.getAttribute('data-challenge-valid-length')
158
- if (challengeValidLengthAttribute) {
159
- config.challengeValidLength = parseInt(challengeValidLengthAttribute)
160
- }
161
-
162
- createRoot(element).render(<Procaptcha config={config} callbacks={callbacks} />)
163
- })
164
- }
165
-
166
- // Implicit render for targeting all elements with class 'procaptcha'
167
- const implicitRender = () => {
168
- // Get elements with class 'procaptcha'
169
- const elements: Element[] = Array.from(document.getElementsByClassName('procaptcha'))
170
-
171
- // Set siteKey from renderOptions or from the first element's data-sitekey attribute
172
- if (elements.length) {
173
- const siteKey = at(elements, 0).getAttribute('data-sitekey') || undefined
174
- const config = getConfig(siteKey)
175
-
176
- renderLogic(elements, config)
177
- }
178
- }
179
-
180
- // Explicit render for targeting specific elements
181
- export const render = (elementId: string, renderOptions: ProcaptchaRenderOptions) => {
182
- const siteKey = renderOptions.siteKey
183
- const config = getConfig(siteKey)
184
- const element = document.getElementById(elementId)
185
-
186
- if (!element) {
187
- console.error('Element not found:', elementId)
188
- return
189
- }
190
-
191
- renderLogic([element], config, renderOptions)
192
- }
193
-
194
- export default function ready(fn: () => void) {
195
- if (document && document.readyState !== 'loading') {
196
- console.log('document.readyState ready!')
197
- fn()
198
- } else {
199
- console.log('DOMContentLoaded listener!')
200
- document.addEventListener('DOMContentLoaded', fn)
201
- }
202
- }
203
-
204
- // onLoadUrlCallback defines the name of the callback function to be called when the script is loaded
205
- // onRenderExplicit takes values of either explicit or implicit
206
- const { onloadUrlCallback, renderExplicit } = extractParams(BUNDLE_NAME)
207
-
208
- // Render the Procaptcha component implicitly if renderExplicit is not set to explicit
209
- if (renderExplicit !== 'explicit') {
210
- ready(implicitRender)
211
- }
212
-
213
- if (onloadUrlCallback) {
214
- const onloadCallback = getWindowCallback(onloadUrlCallback)
215
- // Add event listener to the script tag to call the callback function when the script is loaded
216
- getCurrentScript()?.addEventListener('load', () => {
217
- ready(onloadCallback)
218
- })
219
- }
package/tsconfig.cjs.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.cjs.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist/cjs",
6
- "lib": ["es6", "dom"]
7
- },
8
- "include": ["src/index.tsx", "src/index.html"],
9
- "references": [
10
- {
11
- "path": "../../dev/config"
12
- },
13
- {
14
- "path": "../procaptcha/tsconfig.cjs.json"
15
- },
16
- {
17
- "path": "../procaptcha-react/tsconfig.cjs.json"
18
- },
19
- {
20
- "path": "../util/tsconfig.cjs.json"
21
- }
22
- ]
23
- }
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.esm.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist",
6
- "lib": ["es6", "dom"]
7
- },
8
- "include": ["src", "src/**/*.json", "src/index.html"],
9
- "references": [
10
- {
11
- "path": "../../dev/config"
12
- },
13
- {
14
- "path": "../procaptcha"
15
- },
16
- {
17
- "path": "../procaptcha-react"
18
- },
19
- {
20
- "path": "../util"
21
- }
22
- ]
23
- }