Skip to content

Commit bac46a8

Browse files
committed
fix fb_dtsg
1 parent 18e8476 commit bac46a8

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

scripts/backup/ext/fb-auto-invite-share/popup/helpers/facebook.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,44 @@ export function wrapGraphQlParams(params = {}) {
8383

8484
export async function getFbDtsg() {
8585
const uid = await getMyUid();
86-
8786
if (CACHED.fb_dtsg[uid]) return CACHED.fb_dtsg[uid];
88-
let res = await fetch("https://mbasic.facebook.com/photos/upload/");
89-
let text = await res.text();
90-
let dtsg = RegExp(/name="fb_dtsg" value="(.*?)"/).exec(text)?.[1];
91-
if (!dtsg) {
92-
res = await fetch("https://m.facebook.com/home.php", {
93-
headers: {
94-
Accept: "text/html",
95-
},
96-
});
97-
text = await res.text();
98-
dtsg =
99-
RegExp(/"dtsg":{"token":"([^"]+)"/).exec(text)?.[1] ||
100-
RegExp(/"name":"fb_dtsg","value":"([^"]+)/).exec(text)?.[1];
87+
88+
for (let fn of [
89+
async () => {
90+
let text = await fetch("https://www.facebook.com/policies_center/");
91+
let token = text.match(/DTSGInitData",\[\],\{"token":"(.*?)"/)[1];
92+
return token;
93+
},
94+
async () => {
95+
let res = await fetch("https://mbasic.facebook.com/photos/upload/");
96+
let text = await res.text();
97+
let dtsg = RegExp(/name="fb_dtsg" value="(.*?)"/).exec(text)?.[1];
98+
return dtsg;
99+
},
100+
async () => {
101+
const res = await fetch("https://m.facebook.com/home.php", {
102+
headers: {
103+
Accept: "text/html",
104+
},
105+
});
106+
const text = await res.text();
107+
const dtsg =
108+
RegExp(/"dtsg":{"token":"([^"]+)"/).exec(text)?.[1] ||
109+
RegExp(/"name":"fb_dtsg","value":"([^"]+)/).exec(text)?.[1];
110+
return dtsg;
111+
},
112+
]) {
113+
try {
114+
const res = await fn();
115+
if (res) {
116+
CACHED.fb_dtsg[uid] = res;
117+
return CACHED.fb_dtsg[uid];
118+
}
119+
} catch (e) {
120+
console.error(e);
121+
}
101122
}
102-
CACHED.fb_dtsg[uid] = dtsg || null;
103-
return CACHED.fb_dtsg[uid];
123+
return null;
104124
}
105125

106126
export function findDataObject(object) {

scripts/fb_GLOBAL.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ export async function getFbdtsg() {
625625
let methods = [
626626
() => require("DTSGInitData").token,
627627
() => require("DTSG").getToken(),
628+
async () => {
629+
let text = await fetch("https://www.facebook.com/policies_center/");
630+
let token = text.match(/DTSGInitData",\[\],\{"token":"(.*?)"/)[1];
631+
return token;
632+
},
628633
() => {
629634
return RegExp(/"DTSGInitialData",\[],{"token":"(.+?)"/).exec(
630635
document.documentElement.innerHTML

scripts/ufs_statistic.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,17 @@ async function initCache() {
477477

478478
if (!CACHED.fb_dtsg) {
479479
let res = await UfsGlobal.Extension.runInBackground("fetch", [
480-
"https://mbasic.facebook.com/photos/upload/",
480+
"https://www.facebook.com/policies_center/",
481481
]);
482-
CACHED.fb_dtsg = RegExp(/name="fb_dtsg" value="(.*?)"/).exec(res.body)?.[1];
482+
CACHED.fb_dtsg = res?.body?.match?.(
483+
/DTSGInitData",\[\],\{"token":"(.*?)"/
484+
)?.[1];
483485
}
484486
}
485487

486488
async function getFbProfile(uid, force = false) {
487-
if (CACHED.fbProfile.has(uid) && !force) return CACHED.fbProfile.get(uid);
489+
const cached = CACHED.fbProfile.get(uid);
490+
if (cached?.name && !force) return cached;
488491

489492
let res = await UfsGlobal.Extension.runInBackground("fetch", [
490493
"https://www.facebook.com/api/graphql/",

0 commit comments

Comments
 (0)