-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirebaseFeedback.js
More file actions
86 lines (72 loc) · 2.7 KB
/
firebaseFeedback.js
File metadata and controls
86 lines (72 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function () {
'use strict';
if (window.__SMARTCAPTCHA_FEEDBACK_DISABLED__ === true) return;
const FIREBASE_SDK_VERSION = '10.7.1';
const DEFAULT_FIREBASE_CONFIG = {
apiKey: 'AIzaSyAKwG9QU4wad82zMYBpIkT-T7-y52ZQ8h0',
authDomain: 'smartcaptcha-feedback.firebaseapp.com',
projectId: 'smartcaptcha-feedback',
storageBucket: 'smartcaptcha-feedback.firebasestorage.app',
messagingSenderId: '838993919490',
appId: '1:838993919490:web:3e1efefdf73a677d8a311b',
measurementId: 'G-S5HHWGEDTE'
};
const firebaseConfig = (window.__SMARTCAPTCHA_FIREBASE_CONFIG__ && typeof window.__SMARTCAPTCHA_FIREBASE_CONFIG__ === 'object')
? window.__SMARTCAPTCHA_FIREBASE_CONFIG__
: DEFAULT_FIREBASE_CONFIG;
function loadScript(src) {
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.async = true;
s.src = src;
s.onload = () => resolve();
s.onerror = () => reject(new Error(`Failed to load script: ${src}`));
document.head.appendChild(s);
});
}
async function ensureFirebaseCompatLoaded() {
if (window.firebase && typeof window.firebase.initializeApp === 'function') return;
const base = `https://www.gstatic.com/firebasejs/${FIREBASE_SDK_VERSION}`;
await loadScript(`${base}/firebase-app-compat.js`);
await loadScript(`${base}/firebase-firestore-compat.js`);
if (!window.firebase || typeof window.firebase.initializeApp !== 'function') {
throw new Error('Firebase compat SDK not available after load');
}
}
let initPromise = null;
async function getFirestore() {
await ensureFirebaseCompatLoaded();
const existingApps = (window.firebase && typeof window.firebase.apps === 'object') ? window.firebase.apps : [];
const app = (existingApps && existingApps.length)
? existingApps[0]
: window.firebase.initializeApp(firebaseConfig);
return window.firebase.firestore(app);
}
async function submitFeedback(payload) {
try {
const db = await getFirestore();
await db.collection('feedback').add({
email: String(payload.email || ''),
message: payload.message ? String(payload.message) : '',
createdAt: window.firebase.firestore.FieldValue.serverTimestamp(),
source: 'smartcaptcha-feedback'
});
return { ok: true };
} catch (err) {
return { ok: false, error: err };
}
}
window.SmartCaptchaFeedback = {
init: function init() {
if (!initPromise) {
initPromise = Promise.resolve()
.then(() => true)
.catch(() => false);
}
return initPromise;
},
submit: function submit(email, message) {
return submitFeedback({ email, message });
}
};
})();