0
# Facebook Upload
1
2
Browser automation system for uploading videos to Facebook Reels using Puppeteer. Handles session management through exported cookies, video upload workflow, caption input, and video duration validation.
3
4
## Capabilities
5
6
### Reels Upload Function
7
8
Main function for uploading videos to Facebook Reels with automated browser workflow.
9
10
```javascript { .api }
11
/**
12
* Upload video to Facebook Reels via browser automation
13
* @param namafile - Filename without extension (video should exist in ./download/ directory)
14
* @param caption - Video caption/description to add
15
* @returns Promise resolving to upload result status
16
*/
17
function ReelsUpload(namafile: string, caption: string): Promise<UploadResult>;
18
19
interface UploadResult {
20
/** Upload status indicator */
21
status: 'success' | 'error';
22
/** Descriptive message about upload result */
23
message: string;
24
}
25
```
26
27
**Usage Examples:**
28
29
```javascript
30
import { ReelsUpload } from './lib/browserHandler.js';
31
32
// Basic upload (requires cookies.json and video file)
33
const result = await ReelsUpload('video123', 'Check out this amazing video! #viral');
34
if (result.status === 'success') {
35
console.log('Upload successful:', result.message);
36
} else {
37
console.log('Upload failed:', result.message);
38
}
39
40
// Handle different outcomes
41
const uploadResult = await ReelsUpload('longvideo', 'My caption');
42
switch (uploadResult.status) {
43
case 'success':
44
console.log('Video published to Reels');
45
break;
46
case 'error':
47
console.log('Failed:', uploadResult.message);
48
break;
49
}
50
```
51
52
### Session Management
53
54
Functions for managing Facebook authentication through exported cookies.
55
56
```javascript { .api }
57
/**
58
* Check if valid Facebook session cookies exist
59
* @returns Promise resolving to boolean indicating session validity
60
*/
61
function checkSession(): Promise<boolean>;
62
63
/**
64
* Generate timestamped console log message
65
* @param str - Message to log with timestamp
66
*/
67
function printLog(str: string): void;
68
```
69
70
**Usage Examples:**
71
72
```javascript
73
// Check session before attempting upload
74
const hasValidSession = await checkSession();
75
if (!hasValidSession) {
76
console.log('Please export Facebook cookies to cookies.json');
77
return;
78
}
79
80
// Logging with timestamps
81
printLog('Starting upload process...');
82
// Output: [14:30:25] Starting upload process...
83
```
84
85
## Browser Configuration
86
87
### Browser Options
88
89
The module uses specific Puppeteer configuration for Facebook compatibility:
90
91
```javascript { .api }
92
interface BrowserConfig {
93
/** Chrome executable path from puppeteer */
94
executablePath: string;
95
/** Headless mode (set to false for debugging) */
96
headless: boolean;
97
/** Chrome launch arguments */
98
args: string[];
99
}
100
101
const browserOptions: BrowserConfig = {
102
executablePath: executablePath("chrome"),
103
headless: false,
104
args: [
105
'--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"',
106
'--no-sandbox',
107
'--mute-audio'
108
]
109
};
110
```
111
112
### Page Navigation Options
113
114
```javascript { .api }
115
interface PageOptions {
116
/** Wait condition for page loads */
117
waitUntil: 'networkidle0' | 'load' | 'domcontentloaded';
118
}
119
120
const browserPageOpt: PageOptions = { waitUntil: 'networkidle0' };
121
```
122
123
### Browser Automation Selectors
124
125
XPath selectors used for Facebook Reels UI automation. These are critical for the upload workflow and may need updates if Facebook changes their interface.
126
127
```javascript { .api }
128
/** Facebook UI element selectors */
129
interface FacebookSelectors {
130
/** Upload button selector */
131
uploadButtonSelector: string;
132
/** Next button selectors for different steps */
133
nextButtonSelector: string;
134
nextButtonSelector2: string;
135
nextButtonSelector3: string;
136
/** Caption text area selector */
137
textAreaSelector: string;
138
/** Publish button selector */
139
publishButtonSelector: string;
140
/** Upload completion indicator */
141
ended: string;
142
/** 90-second duration limit detector */
143
s90detector: string;
144
/** Video cutting tool selector */
145
cutvideoSelector: string;
146
}
147
148
// Current XPath selectors (may change with Facebook UI updates)
149
const selectors: FacebookSelectors = {
150
uploadButtonSelector: '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[2]/div[1]/div[2]/div/div/div[1]/div/div/div/div/div/div[1]',
151
nextButtonSelector: '//*[starts-with(@id, "mount")]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[3]/div[2]/div/div/div',
152
nextButtonSelector2: '//*[starts-with(@id, "mount")]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[3]/div[2]/div[2]/div[1]/div',
153
nextButtonSelector3: '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[3]/div[2]/div[2]/div[1]',
154
textAreaSelector: '//*[starts-with(@id, "mount")]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[2]/div[1]/div[2]/div/div/div/div/div[1]/div[1]/div[1]',
155
publishButtonSelector: '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[3]/div[2]/div[2]/div[1]/div/div[1]/div',
156
ended: '/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div[2]/div/div/div[1]/div/div/div/div/div[2]/div[1]/div/div/div[2]/div[2]',
157
s90detector: '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[3]/div[1]/div[1]/div/div/div/div[2]/div/div/div/div/span/span',
158
cutvideoSelector: '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/form/div/div/div[1]/div/div[2]/div[1]/div[2]/div/div/div/div/div/div/div[1]/div/div'
159
};
160
```
161
162
## Upload Workflow
163
164
The upload process follows these automated steps:
165
166
1. **Session Validation**: Check cookies.json for valid Facebook session
167
2. **Page Navigation**: Open Facebook Reels creation page
168
3. **File Selection**: Click upload button and select video file
169
4. **Duration Check**: Validate video is under 90 seconds
170
5. **Caption Input**: Add provided caption text
171
6. **Publishing**: Wait for upload processing and publish the Reel
172
173
### Video Duration Handling
174
175
Facebook Reels have a 90-second limit. The module automatically detects videos over this limit:
176
177
```javascript
178
// If video exceeds 90 seconds, the upload will fail with:
179
{
180
status: "error",
181
message: "Video Gagal di publish!"
182
}
183
```
184
185
## Required Files
186
187
### Cookies Configuration
188
189
The module requires a `cookies.json` file containing exported Facebook session cookies:
190
191
```javascript { .api }
192
// cookies.json format (array of cookie objects)
193
[
194
{
195
"name": "cookie_name",
196
"value": "cookie_value",
197
"domain": ".facebook.com",
198
"path": "/",
199
"expires": 1234567890,
200
"httpOnly": true,
201
"secure": true
202
}
203
// ... more cookies
204
]
205
```
206
207
### Video File Location
208
209
Videos must be placed in the `./download/` directory with `.mp4` extension:
210
- Expected path: `./download/{namafile}.mp4`
211
- File must exist before calling ReelsUpload
212
- Function uses Indonesian variable name `namafile` (meaning "filename")
213
214
### Session Configuration
215
216
Additional session and browser configuration options:
217
218
```javascript { .api }
219
/** Browser visibility control */
220
const browserHide: boolean = false;
221
222
/**
223
* Print timestamped log messages
224
* @param str - Message to log
225
*/
226
function printLog(str: string): void;
227
228
/**
229
* Check if valid Facebook session exists
230
* @returns Promise resolving to session validity
231
*/
232
function checkSession(): Promise<boolean>;
233
```
234
235
## Error Handling
236
237
Common error scenarios and their handling:
238
239
**Session Errors**:
240
```javascript
241
{
242
status: "error",
243
message: "INFO: Session tidak ditemukan..."
244
}
245
```
246
247
**Upload Errors**:
248
```javascript
249
{
250
status: "error",
251
message: "Video Gagal di publish!"
252
}
253
```
254
255
**Duration Errors**:
256
- Videos over 90 seconds are automatically rejected
257
- Module detects duration limit and prevents upload
258
- Indonesian error message: `"Durasi video tidak boleh lebih dari 90 detik."`
259
260
**Specific Error Messages:**
261
```javascript
262
// Indonesian language error messages used in implementation
263
{
264
status: "error",
265
message: "INFO: Session tidak ditemukan..." // "Session not found"
266
}
267
268
{
269
status: "error",
270
message: "Video Gagal di publish!" // "Video failed to publish"
271
}
272
273
// Console messages during processing
274
"Durasi video tidak boleh lebih dari 90 detik." // "Video duration cannot exceed 90 seconds"
275
"Video lebih dari 90 detik tidak terdeteksi" // "Video over 90 seconds not detected"
276
"Berhasil membuka fb" // "Successfully opened Facebook"
277
"Menginput Caption..." // "Inputting caption..."
278
"Post ke Reels" // "Post to Reels"
279
"Berhasil" // "Success"
280
```
281
282
## Browser Automation Details
283
284
The module uses XPath selectors to interact with Facebook's interface. Key interaction points:
285
286
- Upload button selection
287
- File chooser handling
288
- Caption text area input
289
- Publish button activation
290
- Upload completion detection
291
292
All browser interactions include appropriate delays and wait conditions to ensure reliable automation.