Skip to content

Instantly share code, notes, and snippets.

@thinhbuzz
Last active January 8, 2024 19:15
Show Gist options
  • Select an option

  • Save thinhbuzz/8c2f0abb1bb32a651d47fde6ed160652 to your computer and use it in GitHub Desktop.

Select an option

Save thinhbuzz/8c2f0abb1bb32a651d47fde6ed160652 to your computer and use it in GitHub Desktop.
lấy hết comment trong bài viết của nhóm.
function getDTSG() {
return JSON.parse(document.querySelector("#__eqmc").textContent).f;
}
async function getComments(id, dtsg, reactions, after = "null") {
console.log(new Date().toLocaleString(), "Get comments....");
const urlencoded = new URLSearchParams();
urlencoded.append("__a", "1");
urlencoded.append("__comet_req", "15");
urlencoded.append("fb_dtsg", dtsg);
urlencoded.append(
"variables",
'{"commentsAfterCount":-1,"commentsAfterCursor":' +
after +
',"commentsBeforeCount":null,"commentsBeforeCursor":null,"commentsIntentToken":null,"feedLocation":"GROUP_PERMALINK","focusCommentID":null,"scale":1,"useDefaultActor":false,"id":"' +
id +
'"}'
);
urlencoded.append(
"doc_id",
require("CommentsListComponentsPaginationQuery_facebookRelayOperation")
);
const requestOptions = {
method: "POST",
body: urlencoded,
redirect: "follow",
};
const response = await fetch(
"https://www.facebook.com/api/graphql/",
requestOptions
);
const json = await response.json();
const { data } = json;
const items =
data.node.comment_rendering_instance_for_feed_location.comments.edges
.map((item) => {
const node = item.node;
const { author, body_renderer } = node;
if (!body_renderer) {
return null;
}
return {
id: node.legacy_fbid,
author: pick(author, ["id", "name", "url"]),
message: body_renderer.text,
tagged: body_renderer.ranges.map((range) =>
pick(range.entity, ["id", "url"])
),
reaction: reactions.find((r) => r.id === author.id)?.reaction || {
id: null,
name: null,
label: null,
},
};
})
.filter((i) => !!i);
console.log(
new Date().toLocaleString(),
`Get more ${items.length} comments...`
);
if (
data.node.comment_rendering_instance_for_feed_location.comments.page_info
.has_next_page
) {
return items.concat(
await getComments(
id,
dtsg,
reactions,
`"${data.node.comment_rendering_instance_for_feed_location.comments.page_info.end_cursor}"`
)
);
}
return items;
}
async function getReactions(id, dtsg, reactionTypes, after = "null") {
console.log(new Date().toLocaleString(), "Get reaction....");
const urlencoded = new URLSearchParams();
urlencoded.append("__a", "1");
urlencoded.append("fb_dtsg", dtsg);
urlencoded.append(
"variables",
'{"count":10,"cursor":' +
after +
',"feedbackTargetID":"' +
id +
'","reactionID":null,"scale":1,"id":"' +
id +
'"}'
);
urlencoded.append(
"doc_id",
require("CometUFIReactionsDialogTabContentRefetchQuery_facebookRelayOperation")
);
const requestOptions = {
method: "POST",
body: urlencoded,
redirect: "follow",
};
const response = await fetch(
"https://www.facebook.com/api/graphql/",
requestOptions
);
const json = await response.json();
const { data } = json;
const items = data.node.reactors.edges
.map((item) => {
const node = item.node;
const reactionId = item.feedback_reaction_info.id;
const reactionType = reactionTypes[reactionId];
return {
...pick(node, ["id", "name", "url"]),
reaction: {
id: reactionId,
name: reactionType.name,
label: reactionType.display_name,
},
};
})
.filter((i) => !!i);
console.log(
new Date().toLocaleString(),
`Get more ${items.length} reactions...`
);
if (data.node.reactors.page_info.has_next_page) {
return items.concat(
await getReactions(
id,
dtsg,
reactionTypes,
`"${data.node.reactors.page_info.end_cursor}"`
)
);
}
return items;
}
function pick(obj, keys) {
return keys.reduce((acc, key) => {
return { ...acc, [key]: obj[key] };
}, {});
}
function last(arr) {
return arr[arr.length - 1];
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
(async () => {
console.log("Open reaction dialog");
document
.querySelector('[role="toolbar"]')
.firstElementChild.querySelector("div")
.click();
await sleep(3000);
const id = btoa(
"feedback:" + last(location.pathname.split("/").filter((i) => !!i.trim()))
);
const dtsg = getDTSG();
const reactionTypes = require("DynamicUFIReactionTypes").reactions;
const reactions = await getReactions(id, dtsg, reactionTypes);
console.log(reactions);
const comments = await getComments(id, dtsg, reactions);
console.log(comments);
console.log("Done");
// open('https://www.convertcsv.com/json-to-csv.htm', '_blank');
})().catch(console.error);
@thinhbuzz
Copy link
Copy Markdown
Author

mở dialog này trước khi chạy
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment