Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Created January 11, 2023 09:33
Show Gist options
  • Save dvygolov/a480da9bfda8e1e01aeec65bf08487d7 to your computer and use it in GitHub Desktop.
Save dvygolov/a480da9bfda8e1e01aeec65bf08487d7 to your computer and use it in GitHub Desktop.
This script shows Ad Account creation limit for all Business Managers
var bms = await getAllBms();
for (let i = 0; i < bms.data.length; i++) {
let bm = bms.data[i];
console.log(
`%cProcessing BM: ${bm.name}`,
"font-size:15px;font-weight:bold;"
);
console.log(`Ad Account creation limit is: ${await getBMAdAccountCreationLimit(bm.id)}`);
}
async function getAllBms() {
return await publicApiRequest(
"me/businesses",
"fields=id,name,is_disabled_for_integrity_reasons"
);
}
async function getBMAdAccountCreationLimit(bmId){
let uid = require("CurrentUserInitialData").USER_ID;
let dtsg = require("DTSGInitialData").token;
let lsd = require("LSD").token;
let body = `__user=${uid}&__a=1&fb_dtsg=${dtsg}&lsd=${lsd}`;
let curUrl = window.location.href;
let subDomain = curUrl.includes("/business.") ? "business" : "www";
let js = await privateApiRequest2(body,null,`https://${subDomain}.facebook.com/business/adaccount/limits/?business_id=${bmId}`);
return js.payload.adAccountLimit;
}
async function privateApiRequest2(body, headers = null, url = null) {
let graphUrl = "";
if (url != null) graphUrl = url;
else {
let curUrl = window.location.href;
let subDomain = curUrl.includes("business") ? "business" : "www";
graphUrl = `https://${subDomain}.facebook.com/api/graphql/`;
}
headers = headers ?? {
accept: "*/*",
"accept-language": "ca-ES,ca;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/x-www-form-urlencoded",
"sec-ch-ua":
'"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-fb-lsd": require("LSD").token,
};
let f = await fetch(graphUrl, {
headers: headers,
body: body,
method: "POST",
mode: "cors",
credentials: "include",
});
let t = await f.text();
if (t.startsWith("for (;;);"))
t = t.substring(9);
return JSON.parse(t);
}
async function publicApiRequest(path, qs, token = null) {
token = token ?? __accessToken;
let f = await fetch(
`https://graph.facebook.com/v14.0/${path}?${qs}&access_token=${token}`,
{
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "ca-ES,ca;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "max-age=0",
"sec-ch-ua":
'"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
},
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
credentials: "include",
}
);
let js = await f.json();
return js;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment