-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform-config.js
More file actions
87 lines (76 loc) · 2.46 KB
/
platform-config.js
File metadata and controls
87 lines (76 loc) · 2.46 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
87
// Shared platform registry and URL helpers used across extension entrypoints.
(function initCbvPlatformConfig(global) {
'use strict';
const PLATFORM_REGISTRY = Object.freeze([
{
id: 'chatgpt',
label: 'ChatGPT',
hosts: ['chatgpt.com', 'chat.openai.com'],
assistantBadge: 'chatgpt',
transientDomIdPrefixes: ['g_'],
},
{
id: 'claude',
label: 'Claude',
hosts: ['claude.ai'],
assistantBadge: 'claude',
transientDomIdPrefixes: ['claude_'],
},
]);
function normalizeHost(input) {
if (!input) return '';
try {
return new URL(input).hostname.toLowerCase();
} catch (_) {
return String(input).toLowerCase();
}
}
function matchPlatform(input) {
const host = normalizeHost(input);
return PLATFORM_REGISTRY.find(platform =>
platform.hosts.some(candidate => host === candidate || host.endsWith(`.${candidate}`))
) || null;
}
function getPlatform(platformId) {
return PLATFORM_REGISTRY.find(platform => platform.id === platformId) || null;
}
function detectPlatform(input) {
return matchPlatform(input)?.id || 'unknown';
}
function formatPlatformName(platformId) {
return getPlatform(platformId)?.label || platformId || 'Unknown';
}
function getAssistantBadgeKind(platformId) {
return getPlatform(platformId)?.assistantBadge || 'assistant';
}
function isSupportedUrl(input) {
return Boolean(matchPlatform(input));
}
function makeStorageKey(input) {
try {
const url = new URL(input);
if (!isSupportedUrl(url.href)) return null;
return 'cbv_tree_' + (url.pathname + url.hash).replace(/[^a-zA-Z0-9]/g, '_').slice(0, 120);
} catch (_) {
return null;
}
}
function makeNodeId(turnIndex, branchIndex, domId) {
if (domId) {
const isTransient = PLATFORM_REGISTRY.some(platform =>
(platform.transientDomIdPrefixes || []).some(prefix => domId.startsWith(prefix))
);
if (!isTransient) return domId;
}
return `t${turnIndex}_b${branchIndex}`;
}
global.CBV_PLATFORM_REGISTRY = PLATFORM_REGISTRY;
global.cbvMatchPlatform = matchPlatform;
global.cbvGetPlatform = getPlatform;
global.cbvDetectPlatform = detectPlatform;
global.cbvFormatPlatformName = formatPlatformName;
global.cbvGetAssistantBadgeKind = getAssistantBadgeKind;
global.cbvIsSupportedUrl = isSupportedUrl;
global.cbvMakeStorageKey = makeStorageKey;
global.cbvMakeNodeId = makeNodeId;
})(globalThis);