|
| 1 | +window.onload = async () => { |
| 2 | + const Global = { |
| 3 | + endpoint: "https://www.facebook.com/api/graphqlbatch/", |
| 4 | + mid: null, |
| 5 | + right: Date.now(), |
| 6 | + left: new Date("1/1/2004").getTime(), |
| 7 | + fb_dtsg: await UsefulScriptGlobalPageContext.Facebook.getFbdtsg(), |
| 8 | + }; |
| 9 | + |
| 10 | + console.log(Global); |
| 11 | + |
| 12 | + // query elements |
| 13 | + let friend_url_inp = document.querySelector("#inp-friend-url"); |
| 14 | + let access_token_inp = document.querySelector("#inp-access-token"); |
| 15 | + let btnScan = document.querySelector("#btn-scan"); |
| 16 | + let loadingOverlay = document.querySelector(".overlay"); |
| 17 | + let loadingText = document.querySelector("#loading-text"); |
| 18 | + |
| 19 | + // init |
| 20 | + access_token_inp.value = localStorage.ufs_fb_messenger_history || ""; |
| 21 | + access_token_inp.addEventListener("input", function (e) { |
| 22 | + localStorage.ufs_fb_messenger_history = access_token_inp.value; |
| 23 | + }); |
| 24 | + btnScan.onclick = scan; |
| 25 | + |
| 26 | + // functions |
| 27 | + async function scan() { |
| 28 | + try { |
| 29 | + let friend_url = friend_url_inp.value; |
| 30 | + let access_token = access_token_inp.value; |
| 31 | + if (!friend_url) throw new Error("Vui lòng nhập link profile của bạn bè"); |
| 32 | + if (!access_token) throw new Error("Vui lòng nhập access token của bạn"); |
| 33 | + |
| 34 | + showLoading("Đang lấy thông tin bạn bè..."); |
| 35 | + let friendUid = |
| 36 | + await UsefulScriptGlobalPageContext.Facebook.getUidFromUrl(friend_url); |
| 37 | + let friendInfo = await UsefulScriptGlobalPageContext.Facebook.getUserInfo( |
| 38 | + friendUid, |
| 39 | + access_token |
| 40 | + ); |
| 41 | + console.log(friendInfo); |
| 42 | + } catch (e) { |
| 43 | + alert("ERROR: " + e); |
| 44 | + } finally { |
| 45 | + showLoading(false); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + function showLoading(text) { |
| 50 | + loadingOverlay.style.display = text ? "flex" : "none"; |
| 51 | + if (typeof text === "string") loadingText.innerText = text; |
| 52 | + } |
| 53 | + |
| 54 | + return; |
| 55 | + |
| 56 | + function startScan() {} |
| 57 | + |
| 58 | + function search() { |
| 59 | + return new Promise(function (t) { |
| 60 | + Math.abs(n.l - n.r) <= 1e3 |
| 61 | + ? ((n.loading = !1), |
| 62 | + null === n.result ? (n.error = !0) : ((n.error = !1), t(n.result))) |
| 63 | + : ((n.mid = 1e3 * Math.round((n.l + n.r) / 2 / 1e3)), |
| 64 | + n.isExist(n.mid, e).then(function (r) { |
| 65 | + r.length === e |
| 66 | + ? ((n.r = n.mid - 1), (n.result = r)) |
| 67 | + : (n.l = n.mid + 1), |
| 68 | + n.search(e).then(t); |
| 69 | + })); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + function getMessageAfter(msgId, limit) { |
| 74 | + return new Promise(function (r, i) { |
| 75 | + fetch("https://www.facebook.com/ajax/mercury/search_context.php", { |
| 76 | + headers: { |
| 77 | + "Content-Type": "application/x-www-form-urlencoded", |
| 78 | + }, |
| 79 | + method: "POST", |
| 80 | + body: |
| 81 | + "message_id=" + |
| 82 | + msgId + |
| 83 | + "&limit=" + |
| 84 | + limit + |
| 85 | + "&direction=down&other_user_fbid=" + |
| 86 | + t.friend_info.id + |
| 87 | + "&__a=1&fb_dtsg=" + |
| 88 | + t.csrf_token, |
| 89 | + credentials: "include", |
| 90 | + }) |
| 91 | + .then(function (e) { |
| 92 | + return e.text(); |
| 93 | + }) |
| 94 | + .then(function (e) { |
| 95 | + var n = JSON.parse(e.substr(9)), |
| 96 | + t = n.payload.graphql_payload; |
| 97 | + if (t) r(t); |
| 98 | + else { |
| 99 | + var o = new Error("There is no results."); |
| 100 | + i(o); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + function makeQuery(cursor, limit) { |
| 107 | + return { |
| 108 | + o0: { |
| 109 | + doc_id: "1526314457427586", |
| 110 | + query_params: { |
| 111 | + id: this.friend_info.id, |
| 112 | + message_limit: limit, |
| 113 | + load_messages: 1, |
| 114 | + load_read_receipts: !0, |
| 115 | + before: cursor, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }; |
| 119 | + } |
| 120 | + function isExist(cursor, limit) { |
| 121 | + return new Promise(function (resolve, reject) { |
| 122 | + fetch(Global.endpoint, { |
| 123 | + headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 124 | + method: "POST", |
| 125 | + body: |
| 126 | + "fb_dtsg=" + |
| 127 | + t.csrf_token + |
| 128 | + "&queries=" + |
| 129 | + JSON.stringify(makeQuery(cursor, limit)), |
| 130 | + credentials: "include", |
| 131 | + }) |
| 132 | + .then(function (e) { |
| 133 | + return e.text(); |
| 134 | + }) |
| 135 | + .then(function (e) { |
| 136 | + try { |
| 137 | + var n = JSON.parse(e.split("\n")[0]); |
| 138 | + if (n.o0.data.message_thread) |
| 139 | + resolve(n.o0.data.message_thread.messages.nodes); |
| 140 | + } catch (i) { |
| 141 | + console.error(i.message); |
| 142 | + } |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | +}; |
0 commit comments