-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpopup-logic.js
More file actions
177 lines (148 loc) · 4.2 KB
/
Copy pathpopup-logic.js
File metadata and controls
177 lines (148 loc) · 4.2 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
DEFAULT_EXPORT_FORMAT,
normalizeExportFormat,
} from './media-format.js';
export const DEFAULT_SETTINGS = {
duration: 60,
delay: 2,
format: DEFAULT_EXPORT_FORMAT,
};
export const DURATION_LIMITS = {
min: 5,
max: 300,
};
export const DELAY_LIMITS = {
min: 0,
max: 10,
};
export const STORAGE_KEY = 'scrollywood.popupSettings';
const BLOCKED_PROTOCOLS = new Set([
'about:',
'chrome:',
'chrome-extension:',
'chrome-search:',
'devtools:',
'edge:',
'view-source:',
]);
function clampNumber(value, limits, fallback) {
const number = Number.parseInt(value, 10);
if (Number.isNaN(number)) {
return fallback;
}
return Math.min(limits.max, Math.max(limits.min, number));
}
export function normalizeSettings(settings = {}) {
const normalized = {
duration: clampNumber(settings.duration, DURATION_LIMITS, DEFAULT_SETTINGS.duration),
delay: clampNumber(settings.delay, DELAY_LIMITS, DEFAULT_SETTINGS.delay),
format: normalizeExportFormat(settings.format),
};
return normalized;
}
export function readStoredSettings(storage = globalThis.localStorage) {
if (!storage) {
return { ...DEFAULT_SETTINGS };
}
try {
const raw = storage.getItem(STORAGE_KEY);
if (!raw) {
return { ...DEFAULT_SETTINGS };
}
return normalizeSettings(JSON.parse(raw));
} catch {
return { ...DEFAULT_SETTINGS };
}
}
export function writeStoredSettings(settings, storage = globalThis.localStorage) {
if (!storage) {
return normalizeSettings(settings);
}
const normalized = normalizeSettings(settings);
storage.setItem(STORAGE_KEY, JSON.stringify(normalized));
return normalized;
}
export function buildCapturePlan(settings = {}) {
const normalized = normalizeSettings(settings);
const formatLabel = normalized.format.toUpperCase();
const leadIn = normalized.delay === 0
? 'starts scrolling immediately'
: `starts after ${normalized.delay}s`;
let note = 'WebM exports fastest and keeps the cleanest motion.';
if (normalized.format === 'mp4') {
note = 'MP4 plays best in social upload flows. Availability depends on Chrome support.';
}
if (normalized.format === 'gif' && normalized.duration < 45) {
note = 'GIF adds a loopable export and encodes right after capture.';
}
if (normalized.format === 'gif' && normalized.duration >= 45) {
note = 'Long GIF takes can take a bit to encode after the pass finishes.';
}
return {
title: `${normalized.duration}s ${formatLabel} with ${normalized.delay}s lead-in`,
note,
meta: `${formatLabel} • ${leadIn}`,
};
}
export function getRecordButtonMeta(settings = {}) {
const normalized = normalizeSettings(settings);
if (normalized.format === 'mp4') {
return 'Best for platforms that reject WebM.';
}
if (normalized.format === 'gif') {
return 'Encodes after capture finishes.';
}
return 'Saves when the pass is complete.';
}
export function parseTabOverride(search = '') {
const params = new URLSearchParams(search);
const rawTabId = params.get('tab');
if (!rawTabId) {
return null;
}
const tabId = Number.parseInt(rawTabId, 10);
if (!Number.isInteger(tabId) || tabId < 0) {
return null;
}
return tabId;
}
function compactHost(hostname) {
return hostname.replace(/^www\./, '') || 'Current tab';
}
function compactTitle(title = '') {
if (!title) {
return 'Ready to record the page currently in focus.';
}
return title.length > 52 ? `${title.slice(0, 49)}...` : title;
}
export function getTabContext(tab = {}) {
const rawUrl = tab.url || tab.pendingUrl || '';
if (!rawUrl) {
return {
supported: true,
chip: 'Current tab',
detail: 'Ready to record the page currently in focus.',
};
}
try {
const parsed = new URL(rawUrl);
if (BLOCKED_PROTOCOLS.has(parsed.protocol)) {
return {
supported: false,
chip: 'Unsupported',
detail: 'Open a normal webpage before rolling camera.',
};
}
return {
supported: true,
chip: compactHost(parsed.hostname || parsed.protocol.replace(':', '')),
detail: compactTitle(tab.title),
};
} catch {
return {
supported: true,
chip: 'Current tab',
detail: compactTitle(tab.title),
};
}
}