|
| 1 | +import { UfsGlobal } from "./content-scripts/ufs_global.js"; |
| 2 | +import { fetchGraphQl, getFbdtsg } from "./fb_GLOBAL.js"; |
| 3 | + |
| 4 | +export default { |
| 5 | + icon: '<i class="fa-solid fa-thumbs-up fa-lg"></i>', |
| 6 | + name: { |
| 7 | + en: "Show facebook post reaction count", |
| 8 | + vi: "Hiện tổng lượt thích bài viết facebook", |
| 9 | + }, |
| 10 | + description: { |
| 11 | + en: "Show total reaction count on facebook posts when hover mouse over post's reaction section", |
| 12 | + vi: "Hiện tổng lượt thích bài viết khi đưa chuột vào xem lượt thích.", |
| 13 | + img: "/scripts/fb_getPostReactionCount.jpg", |
| 14 | + }, |
| 15 | + |
| 16 | + changeLogs: { |
| 17 | + "2024-06-25": "init", |
| 18 | + }, |
| 19 | + |
| 20 | + whiteList: ["https://*.facebook.com/*"], |
| 21 | + |
| 22 | + pageScript: { |
| 23 | + onDocumentStart: (details) => { |
| 24 | + const CACHED = {}; |
| 25 | + const ReactionId = { |
| 26 | + "👍": "1635855486666999", |
| 27 | + "💖": "1678524932434102", |
| 28 | + "🥰": "613557422527858", |
| 29 | + "😆": "115940658764963", |
| 30 | + "😲": "478547315650144", |
| 31 | + "😔": "908563459236466", |
| 32 | + "😡": "444813342392137", |
| 33 | + }; |
| 34 | + |
| 35 | + const getPostReactionsCount = async (id, reactionId) => { |
| 36 | + const res = await fetchGraphQl( |
| 37 | + { |
| 38 | + fb_api_caller_class: "RelayModern", |
| 39 | + fb_api_req_friendly_name: "CometUFIReactionIconTooltipContentQuery", |
| 40 | + variables: { |
| 41 | + feedbackTargetID: id, |
| 42 | + reactionID: reactionId, |
| 43 | + }, |
| 44 | + doc_id: "6235145276554312", |
| 45 | + }, |
| 46 | + await getFbdtsg() |
| 47 | + ); |
| 48 | + const json = JSON.parse(res || "{}"); |
| 49 | + return json?.data?.feedback?.reactors?.count || 0; |
| 50 | + }; |
| 51 | + |
| 52 | + const getTotalPostReactionCount = async (id) => { |
| 53 | + const { setText, closeAfter } = UfsGlobal.DOM.notify({ |
| 54 | + msg: "Đang đếm số lượng reaction...", |
| 55 | + duration: 10000, |
| 56 | + }); |
| 57 | + |
| 58 | + let res; |
| 59 | + if (CACHED[id]) { |
| 60 | + res = CACHED[id]; |
| 61 | + } else { |
| 62 | + res = { |
| 63 | + total: 0, |
| 64 | + each: {}, |
| 65 | + }; |
| 66 | + for (let [name, reactionId] of Object.entries(ReactionId)) { |
| 67 | + const count = await getPostReactionsCount(id, reactionId); |
| 68 | + res.total += count; |
| 69 | + res.each[name] = count; |
| 70 | + setText(`Đang đếm số lượng reaction ${name}... Tổng: ${res.total}`); |
| 71 | + } |
| 72 | + CACHED[id] = res; |
| 73 | + } |
| 74 | + |
| 75 | + setText( |
| 76 | + "<p style='color:white;font-size:20px;padding:0;margin:0'>Tổng " + |
| 77 | + res.total + |
| 78 | + " reaction.<br/>Bao gồm " + |
| 79 | + Object.entries(res.each) |
| 80 | + .filter(([key, value]) => value > 0) |
| 81 | + .map(([key, value]) => `${value}${key}`) |
| 82 | + .join(", ") + |
| 83 | + "</p>" |
| 84 | + ); |
| 85 | + closeAfter(10000); |
| 86 | + }; |
| 87 | + |
| 88 | + const originalXMLSend = XMLHttpRequest.prototype.send; |
| 89 | + XMLHttpRequest.prototype.send = function () { |
| 90 | + let s = arguments[0]?.toString() || ""; |
| 91 | + if (s.includes("CometUFIReactionsCountTooltipContentQuery")) { |
| 92 | + console.log(this); |
| 93 | + const original = this.onreadystatechange; |
| 94 | + this.onreadystatechange = function () { |
| 95 | + if (this.readyState == 4) { |
| 96 | + try { |
| 97 | + const json = JSON.parse(this.responseText); |
| 98 | + if ( |
| 99 | + json?.data?.feedback?.reaction_display_config |
| 100 | + ?.reaction_display_strategy == "HIDE_COUNTS" |
| 101 | + ) { |
| 102 | + const id = json.data.feedback.id; |
| 103 | + getTotalPostReactionCount(id); |
| 104 | + } |
| 105 | + } catch (err) { |
| 106 | + console.log(err); |
| 107 | + } |
| 108 | + } |
| 109 | + original.apply(this, arguments); |
| 110 | + }; |
| 111 | + } |
| 112 | + originalXMLSend.apply(this, arguments); |
| 113 | + }; |
| 114 | + }, |
| 115 | + }, |
| 116 | +}; |
0 commit comments