@dongdev/fca-unofficial 1.0.11 → 1.0.12

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/index.js +70 -104
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -2,3 +2,6 @@
2
2
  Too lazy to write changelog, sorry! (will write changelog in the next release, through.)
3
3
  ## v1.0.10 - 2025-04-24
4
4
  - Hotfix / auto bump
5
+
6
+ ## v1.0.11 - 2025-04-24
7
+ - Hotfix / auto bump
package/index.js CHANGED
@@ -62,7 +62,7 @@ if (global.fca.config.autoUpdate) {
62
62
  }
63
63
  const Boolean_Option = [
64
64
  "online",
65
- "selfListen",
65
+ "selfListen",
66
66
  "listenEvents",
67
67
  "updatePresence",
68
68
  "forceLogin",
@@ -73,7 +73,7 @@ const Boolean_Option = [
73
73
  "emitReady",
74
74
  ];
75
75
  function setOptions(globalOptions, options) {
76
- Object.keys(options).map(function(key) {
76
+ Object.keys(options).map(function (key) {
77
77
  switch (Boolean_Option.includes(key)) {
78
78
  case true: {
79
79
  globalOptions[key] = Boolean(options[key]);
@@ -133,6 +133,7 @@ function buildAPI(globalOptions, html, jar) {
133
133
  const cookies = jar.getCookies("https://www.facebook.com");
134
134
  const userCookie = cookies.find(c => c.cookieString().startsWith("c_user="));
135
135
  const tiktikCookie = cookies.find(c => c.cookieString().startsWith("i_user="));
136
+ console.log(cookies)
136
137
  if (userCookie.length === 0 && tiktikCookie.length === 0) {
137
138
  return log.error('login', "Không tìm thấy cookie cho người dùng, vui lòng kiểm tra lại thông tin đăng nhập")
138
139
  } else if (!userCookie && !tiktikCookie) {
@@ -145,7 +146,7 @@ function buildAPI(globalOptions, html, jar) {
145
146
  logger(`Logged in as ${userID}`, 'info');
146
147
  try {
147
148
  clearInterval(checkVerified);
148
- } catch (_) {}
149
+ } catch (_) { }
149
150
  const clientID = ((Math.random() * 2147483648) | 0).toString(16);
150
151
  let mqttEndpoint, region, fb_dtsg, irisSeqID;
151
152
  try {
@@ -208,7 +209,7 @@ function buildAPI(globalOptions, html, jar) {
208
209
  require("fs")
209
210
  .readdirSync(__dirname + "/src/")
210
211
  .filter((v) => v.endsWith(".js"))
211
- .map(function(v) {
212
+ .map(function (v) {
212
213
  api[v.replace(".js", "")] = require("./src/" + v)(defaultFuncs, api, ctx);
213
214
  });
214
215
  api.listen = api.listenMqtt;
@@ -223,130 +224,95 @@ function buildAPI(globalOptions, html, jar) {
223
224
  console.error("An error occurred while refreshing fb_dtsg", err);
224
225
  });
225
226
  }, 1000 * 60 * 60 * 24);
226
- return [ctx, defaultFuncs, api];
227
+ return {
228
+ ctx,
229
+ defaultFuncs,
230
+ api
231
+ };
227
232
  }
228
- function loginHelper(
229
- appState,
230
- email,
231
- password,
232
- globalOptions,
233
- callback,
234
- prCallback
235
- ) {
233
+
234
+ function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
236
235
  let mainPromise = null;
237
236
  const jar = utils.getJar();
238
237
  if (appState) {
239
- if (utils.getType(appState) === "Array" && appState.some((c) => c.name)) {
240
- appState = appState.map((c) => {
241
- c.key = c.name;
242
- delete c.name;
243
- return c;
244
- });
245
- } else if (utils.getType(appState) === "String") {
246
- const arrayAppState = [];
247
- appState.split(";").forEach((c) => {
248
- const [key, value] = c.split("=");
249
- arrayAppState.push({
250
- key: (key || "").trim(),
251
- value: (value || "").trim(),
252
- domain: "facebook.com",
253
- path: "/",
254
- expires: new Date().getTime() + 1000 * 60 * 60 * 24 * 365,
255
- });
238
+ try {
239
+ appState = JSON.parse(appState);
240
+ } catch (e) {
241
+ try {
242
+ appState = appState;
243
+ } catch (e) {
244
+ return callback(new Error("Failed to parse appState"));
245
+ }
246
+ }
247
+
248
+ try {
249
+ appState.forEach(c => {
250
+ const str = `${c.key}=${c.value}; expires=${c.expires}; domain=${c.domain}; path=${c.path};`;
251
+ jar.setCookie(str, "http://" + c.domain);
256
252
  });
257
- appState = arrayAppState;
253
+
254
+ mainPromise = utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
255
+ .then(utils.saveCookies(jar));
256
+ } catch (e) {
257
+ process.exit(0);
258
258
  }
259
- appState.map(function(c) {
260
- const str =
261
- c.key +
262
- "=" +
263
- c.value +
264
- "; expires=" +
265
- c.expires +
266
- "; domain=" +
267
- c.domain +
268
- "; path=" +
269
- c.path +
270
- ";";
271
- jar.setCookie(str, "http://" + c.domain);
272
- });
273
- mainPromise = utils
274
- .get("https://www.facebook.com/", jar, null, globalOptions, {
275
- noRef: true,
276
- })
277
- .then(utils.saveCookies(jar));
278
259
  } else {
279
- if (email) {
280
- throw {
281
- error:
282
- "Currently, the login method by email and password is no longer supported, please use the login method by appState",
283
- };
284
- } else {
285
- throw { error: "No appState given." };
260
+ mainPromise = utils
261
+ .get("https://www.facebook.com/", null, null, globalOptions, { noRef: true })
262
+ .then(utils.saveCookies(jar))
263
+ .then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
264
+ .then(() => utils.get('https://www.facebook.com/', jar, null, globalOptions).then(utils.saveCookies(jar)));
265
+ }
266
+
267
+ function handleRedirect(res) {
268
+ const reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
269
+ const redirect = reg.exec(res.body);
270
+ if (redirect && redirect[1]) {
271
+ return utils.get(redirect[1], jar, null, globalOptions).then(utils.saveCookies(jar));
286
272
  }
273
+ return res;
287
274
  }
288
- let ctx = null;
289
- let _defaultFuncs = null;
290
- let api = null;
275
+
276
+ let ctx, api;
291
277
  mainPromise = mainPromise
292
- .then(function(res) {
293
- const reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
294
- const redirect = reg.exec(res.body);
295
- if (redirect && redirect[1]) {
296
- return utils
297
- .get(redirect[1], jar, null, globalOptions)
298
- .then(utils.saveCookies(jar));
278
+ .then(handleRedirect)
279
+ .then(res => {
280
+ const mobileAgentRegex = /MPageLoadClientMetrics/gs;
281
+ if (!mobileAgentRegex.test(res.body)) {
282
+ globalOptions.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36";
283
+ return utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
299
284
  }
300
285
  return res;
301
286
  })
302
- .then(function(res) {
287
+ .then(handleRedirect)
288
+ .then(res => {
303
289
  const html = res.body;
304
- const stuff = buildAPI(globalOptions, html, jar);
305
- ctx = stuff[0];
306
- _defaultFuncs = stuff[1];
307
- api = stuff[2];
290
+ const Obj = buildAPI(globalOptions, html, jar);
291
+ ctx = Obj.ctx;
292
+ api = Obj.api;
308
293
  return res;
309
294
  });
295
+
310
296
  if (globalOptions.pageID) {
311
297
  mainPromise = mainPromise
312
- .then(function() {
313
- return utils.get(
314
- "https://www.facebook.com/" +
315
- ctx.globalOptions.pageID +
316
- "/messages/?section=messages&subsection=inbox",
317
- ctx.jar,
318
- null,
319
- globalOptions
320
- );
321
- })
322
- .then(function(resData) {
323
- let url = utils
324
- .getFrom(
325
- resData.body,
326
- 'window.location.replace("https:\\/\\/www.facebook.com\\',
327
- '");'
328
- )
329
- .split("\\")
330
- .join("");
298
+ .then(() => utils.get(`https://www.facebook.com/${globalOptions.pageID}/messages/?section=messages&subsection=inbox`, jar, null, globalOptions))
299
+ .then(resData => {
300
+ let url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
331
301
  url = url.substring(0, url.length - 1);
332
- return utils.get(
333
- "https://www.facebook.com" + url,
334
- ctx.jar,
335
- null,
336
- globalOptions
337
- );
302
+ return utils.get('https://www.facebook.com' + url, jar, null, globalOptions);
338
303
  });
339
304
  }
305
+
340
306
  mainPromise
341
- .then(function() {
342
- logger("Done logging in", 'info');
343
- return callback(null, api);
307
+ .then(async () => {
308
+ log.info('Đăng nhập thành công');
309
+ callback(null, api);
344
310
  })
345
- .catch(function(e) {
346
- log.error("login", e.error || e);
311
+ .catch(e => {
347
312
  callback(e);
348
313
  });
349
314
  }
315
+
350
316
  function login(loginData, options, callback) {
351
317
  if (
352
318
  utils.getType(options) === "Function" ||
@@ -379,11 +345,11 @@ function login(loginData, options, callback) {
379
345
  ) {
380
346
  let rejectFunc = null;
381
347
  let resolveFunc = null;
382
- var returnPromise = new Promise(function(resolve, reject) {
348
+ var returnPromise = new Promise(function (resolve, reject) {
383
349
  resolveFunc = resolve;
384
350
  rejectFunc = reject;
385
351
  });
386
- prCallback = function(error, api) {
352
+ prCallback = function (error, api) {
387
353
  if (error) {
388
354
  return rejectFunc(error);
389
355
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongdev/fca-unofficial",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "A Facebook chat API without XMPP, will not be deprecated after April 30th, 2015.",
5
5
  "main": "index.js",
6
6
  "repository": {