Skip to content

Commit db82101

Browse files
committed
update shortcut
1 parent 4e779a0 commit db82101

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

scripts/content-scripts/ufs_global.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const UfsGlobal = {
1212
waitForTabToLoad,
1313
},
1414
DOM: {
15+
getSelectionText,
1516
keyDown,
1617
keyUp,
1718
closest,
@@ -142,6 +143,16 @@ function download(options) {
142143

143144
// #region DOM
144145

146+
function getSelectionText() {
147+
var text = "";
148+
if (window.getSelection) {
149+
text = window.getSelection().toString();
150+
} else if (document.selection && document.selection.type != "Control") {
151+
text = document.selection.createRange().text;
152+
}
153+
return text;
154+
}
155+
145156
//prettier-ignore
146157
function keyDown(e){let n=document.createEvent("KeyboardEvent");Object.defineProperty(n,"keyCode",{get:function(){return this.keyCodeVal}}),n.initKeyboardEvent?n.initKeyboardEvent("keydown",!0,!0,document.defaultView,e,e,"","",!1,""):n.initKeyEvent("keydown",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),n.keyCodeVal=e,document.body.dispatchEvent(n)}
147158
//prettier-ignore

scripts/ufs_statistic.js

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,26 @@ async function onDocumentEnd() {
607607
document.body.prepend(container);
608608

609609
// #endregion
610+
611+
// #region auto get fb info of selected text on press A
612+
613+
document.addEventListener("keydown", (event) => {
614+
const selectedText = UfsGlobal.DOM.getSelectionText();
615+
if (selectedText) {
616+
if (event.key === "a") {
617+
getEntityAbout(selectedText)
618+
.then((data) =>
619+
alert(data.type + ":\n" + data.name + "\n\n" + data.url)
620+
)
621+
.catch((e) => alert("ERROR: " + e.message));
622+
}
623+
if (event.key === "d") {
624+
window.open("https://fb.com/" + selectedText, "_blank");
625+
}
626+
}
627+
});
628+
629+
// #endregion
610630
}
611631

612632
function getCurrentLogDate() {
@@ -677,24 +697,22 @@ async function initCache() {
677697
async function getFbProfile(uid, force = false) {
678698
if (CACHED.fbProfile.has(uid) && !force) return CACHED.fbProfile.get(uid);
679699

680-
const variables = {
681-
userID: uid,
682-
shouldDeferProfilePic: false,
683-
useVNextHeader: false,
684-
scale: 1.5,
685-
};
686-
let f = new URLSearchParams();
687-
f.append("fb_dtsg", CACHED.fb_dtsg);
688-
f.append("fb_api_req_friendly_name", "ProfileCometHeaderQuery");
689-
f.append("variables", JSON.stringify(variables));
690-
f.append("doc_id", "4159355184147969");
691-
692700
let res = await UfsGlobal.Extension.runInBackground("fetch", [
693701
"https://www.facebook.com/api/graphql/",
694702
{
695703
method: "POST",
696704
headers: { "content-type": "application/x-www-form-urlencoded" },
697-
body: f.toString(),
705+
body: new URLSearchParams({
706+
fb_api_req_friendly_name: "ProfileCometHeaderQuery",
707+
fb_dtsg: CACHED.fb_dtsg,
708+
variables: JSON.stringify({
709+
userID: uid,
710+
shouldDeferProfilePic: false,
711+
useVNextHeader: false,
712+
scale: 1.5,
713+
}),
714+
doc_id: "4159355184147969",
715+
}).toString(),
698716
},
699717
]);
700718

@@ -725,6 +743,58 @@ async function getFbProfile(uid, force = false) {
725743
return info;
726744
}
727745

746+
export const TargetType = {
747+
User: "user",
748+
Page: "page",
749+
Group: "group",
750+
IGUser: "ig_user",
751+
};
752+
753+
export async function getEntityAbout(entityID, context = "DEFAULT") {
754+
let res = await UfsGlobal.Extension.runInBackground("fetch", [
755+
"https://www.facebook.com/api/graphql/",
756+
{
757+
method: "POST",
758+
headers: { "content-type": "application/x-www-form-urlencoded" },
759+
body: new URLSearchParams({
760+
fb_api_req_friendly_name: "CometHovercardQueryRendererQuery",
761+
fb_dtsg: CACHED.fb_dtsg,
762+
variables: JSON.stringify({
763+
actionBarRenderLocation: "WWW_COMET_HOVERCARD",
764+
context: context,
765+
entityID: entityID,
766+
includeTdaInfo: true,
767+
scale: 1,
768+
}),
769+
doc_id: "7257793420991802",
770+
}).toString(),
771+
},
772+
]);
773+
console.log(res);
774+
const text = await res.body;
775+
const json = JSON.parse(text);
776+
const node = json.data.node;
777+
if (!node) throw new Error("Wrong ID / Entity not found");
778+
const typeText = node.__typename.toLowerCase();
779+
if (!Object.values(TargetType).includes(typeText))
780+
throw new Error("Not supported type: " + typeText);
781+
const card = node.comet_hovercard_renderer[typeText];
782+
const type =
783+
typeText === "user"
784+
? card.profile_plus_transition_path?.startsWith("PAGE")
785+
? TargetType.Page
786+
: TargetType.User
787+
: TargetType.Group;
788+
return {
789+
type,
790+
id: node.id || card.id,
791+
name: card.name,
792+
avatar: card.profile_picture.uri,
793+
url: card.profile_url || card.url,
794+
raw: json,
795+
};
796+
}
797+
728798
// log example: 5/31/2024, 9:13:41 AM: OPEN-TAB-unlock (1.67-1717121281787) -> 43
729799
function extractUid(log) {
730800
return /-(\d+)\)/.exec(log)?.[1] || "?";

0 commit comments

Comments
 (0)