Skip to content

Commit

Permalink
feat(Instagram): rewrite presence (PreMiD#7829)
Browse files Browse the repository at this point in the history
* chore(Instagram): rewrite presence

chore(Instagram): support /reel/ & update metadata

chore(Instagram): better privacy checks

chore(Instagram): change for loop to array.filter

* chore(Instagram): change window => document

* fix(Instagram): add backup profilename for /reel/

* chore(Instagram): fix CI & improve check

* chore(Instagram): fix CI & improve check"

* chore(Instagram): remove variable only used once
  • Loading branch information
Dark_Ville authored Dec 25, 2023
1 parent 1882560 commit c4635bc
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 100 deletions.
6 changes: 5 additions & 1 deletion websites/I/Instagram/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
{
"name": "N0chteil ^^",
"id": "495901098926669825"
},
{
"name": "Dark_Ville",
"id": "638080361179512853"
}
],
"service": "Instagram",
Expand All @@ -24,7 +28,7 @@
"vi_VN": "Một cách đơn giản, vui và sáng tạo để chụp, chỉnh sửa và chia sẻ các bức ảnh, video và tin nhắn với gia đình và bạn bè."
},
"url": "www.instagram.com",
"version": "2.2.12",
"version": "2.3.0",
"logo": "https://cdn.rcd.gg/PreMiD/websites/I/Instagram/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/I/Instagram/assets/thumbnail.png",
"color": "#E1306C",
Expand Down
271 changes: 172 additions & 99 deletions websites/I/Instagram/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const presence = new Presence({
}),
timestamp = Math.floor(Date.now() / 1000);

let cached: {
href: string;
video: HTMLVideoElement;
user: string;
};

presence.on("UpdateData", async () => {
const presenceData: PresenceData = {
largeImageKey:
Expand All @@ -13,116 +19,183 @@ presence.on("UpdateData", async () => {
presence.getSetting<boolean>("elapsedTime"),
presence.getSetting<boolean>("postImage"),
]),
{ pathname } = window.location,
{ href, pathname } = document.location,
path = pathname.split("/"),
[, profileName] = document.title.split("(");

if (elapsedTimeSetting) presenceData.startTimestamp = timestamp;

if (!path[1]) presenceData.details = "Viewing the Homepage";
else if (pathname.startsWith("/stories")) {
const time = document.querySelector("time.BPyeS.Nzb55"),
video = document.querySelector("video");

presenceData.details = privacySetting
? "Viewing a Story"
: `Viewing ${path[2]}'s Story`;

if (time && time.getAttribute("datetime")) {
presenceData.state = getDateString(
new Date(time.getAttribute("datetime"))
);
switch (true) {
case !!document.querySelector("div.QY4Ed.P0xOK input.focus-visible"): {
presenceData.details = privacySetting ? "Searching" : "Searching for:";
presenceData.state = document.querySelector<HTMLInputElement>(
"div.QY4Ed.P0xOK input.focus-visible"
)?.value;
break;
}

if (!privacySetting && video && video.duration) {
const timestamps = presence.getTimestampsfromMedia(video);

presenceData.startTimestamp = timestamps[0];
presenceData.endTimestamp = timestamps[1];
case !path[1]: {
presenceData.details = "Viewing the Homepage";
break;
}

presenceData.buttons = [
{
label: "View Story",
url: `https://www.instagram.com/stories/${path[2]}/${path[3]}`,
},
];
} else if (pathname.startsWith("/accounts")) {
presenceData.details = "Settings";
presenceData.state = "Changing their Settings";
} else if (pathname.startsWith("/p")) {
const time = document.querySelector("time._1o9PC.Nzb55"),
profileName = document.querySelector(
"a.sqdOP.yWX7d._8A5w5.ZIAjV"
)?.textContent,
image = document.querySelector<HTMLImageElement>(
"div.eLAPa.RzuR0 div.KL4Bh img"
);

presenceData.details =
privacySetting || !profileName
? "Viewing a Post"
: `Viewing ${profileName}'s Post`;

if (time && time.getAttribute("datetime")) {
presenceData.state = getDateString(
new Date(time.getAttribute("datetime"))
);
case pathname.startsWith("/stories"): {
const time = document.querySelector("time.BPyeS.Nzb55"),
video = document.querySelector("video");

presenceData.details = privacySetting
? "Viewing a Story"
: `Viewing ${path[2]}'s Story`;

if (time && time.getAttribute("datetime")) {
presenceData.state = getDateString(
new Date(time.getAttribute("datetime"))
);
}

if (!privacySetting && video && video.duration) {
[presenceData.startTimestamp, presenceData.endTimestamp] =
presence.getTimestampsfromMedia(video);
}

presenceData.buttons = [
{
label: "View Story",
url: `https://www.instagram.com/stories/${path[2]}/${path[3]}`,
},
];
break;
}
case pathname.includes("/accounts"): {
presenceData.details = "Settings";
presenceData.state = "Changing their Settings";
break;
}
case pathname.includes("/reel/"): {
// One reel (Only from profile)
const profilename =
document.querySelector('[class="_ap3a _aaco _aacw _aacx _aad7 _aade"]')
?.textContent ??
document.querySelector(
"[class*='_acan _acao _acat _acaw _aj1- _ap30 _a6hd']"
)?.textContent;
presenceData.details = "Watching a reel";
presenceData.state = profilename;
presenceData.buttons = [
{ label: "Watch Reel", url: href },
{
label: "View Creator's Profile",
url: `https://www.instagram.com/${profilename}`,
},
];
break;
}
case pathname.includes("/reels/"): {
// Multiple reels (From anywhere)

if (!cached?.href || !cached?.video || cached?.href !== href) {
const video =
Array.from(document.querySelectorAll("video")).find(
video => !video.paused
) ?? document.querySelector("video");
if (!video?.paused) {
cached = {
video,
href,
user: video
?.closest('div[class*="x6ikm8r"]')
?.querySelector('[class*="x1943h6x"]')
?.textContent?.toLowerCase(),
};
return;
}
}

const user = cached?.user;
presenceData.details = "Watching a reel";
presenceData.state = user ?? "unknown creator";
presenceData.smallImageKey = "https://i.imgur.com/YSn0xd3.png";
presenceData.buttons = [
{ label: "Watch Reel", url: href },
{
label: "View Creator's Profile",
url: user ? `https://www.instagram.com/${user}` : "",
},
];
break;
}
case pathname.startsWith("/p"): {
const time = document.querySelector("time._1o9PC.Nzb55"),
profileName = document.querySelector(
"a.sqdOP.yWX7d._8A5w5.ZIAjV"
)?.textContent,
image = document.querySelector<HTMLImageElement>(
"div.eLAPa.RzuR0 div.KL4Bh img"
);

presenceData.details =
privacySetting || !profileName
? "Viewing a Post"
: `Viewing ${profileName}'s Post`;

if (time && time.getAttribute("datetime")) {
presenceData.state = getDateString(
new Date(time.getAttribute("datetime"))
);
}

if (!privacySetting && postImageSetting && image && image.src)
presenceData.largeImageKey = await getShortURL(image.src);

presenceData.buttons = [
{
label: "View Post",
url: `https://www.instagram.com/${path[1]}/${path[2]}`,
},
];
break;
}
case pathname.startsWith("/explore"): {
presenceData.details = "Exploring...";
break;
}
case pathname.startsWith("/nametag"): {
presenceData.details = "Viewing nametag";
break;
}
case pathname.startsWith("/direct/inbox"):
case pathname.startsWith("/direct/t"): {
presenceData.details = "Direct Messages";
break;
}
case profileName?.split(")")[0].replace("@", "") === path[1]: {
const profilePicture =
document.querySelector<HTMLImageElement>("img._6q-tv");

presenceData.details = `Viewing a Profile${privacySetting ? "" : ":"}`;
presenceData.state = `${
document
.querySelector("head > title")
?.textContent.split("(")[0]
.trim() ?? "Unknown"
} (${profileName.split(")")[0]})`;

if (profilePicture)
presenceData.smallImageKey = await getShortURL(profilePicture.src);

presenceData.buttons = [
{
label: "View Profile",
url: `https://www.instagram.com/${path[1]}`,
},
];
break;
}

if (!privacySetting && postImageSetting && image && image.src)
presenceData.largeImageKey = await getShortURL(image.src);

presenceData.buttons = [
{
label: "View Post",
url: `https://www.instagram.com/${path[1]}/${path[2]}`,
},
];
} else if (pathname.startsWith("/explore"))
presenceData.details = "Exploring...";
else if (pathname.startsWith("/nametag"))
presenceData.details = "Viewing nametag";
else if (
pathname.startsWith("/direct/inbox") ||
pathname.startsWith("/direct/t")
)
presenceData.details = "Direct Messages";
else if (profileName.split(")")[0].replace("@", "") === path[1]) {
const profilePicture =
document.querySelector<HTMLImageElement>("img._6q-tv");

presenceData.details = `Viewing a Profile${privacySetting ? "" : ":"}`;
presenceData.state = `${
document
.querySelector("head > title")
?.textContent.split("(")[0]
.trim() ?? "Unknown"
} (${profileName.split(")")[0]})`;

if (profilePicture)
presenceData.smallImageKey = await getShortURL(profilePicture.src);

presenceData.buttons = [
{
label: "View Profile",
url: `https://www.instagram.com/${path[1]}`,
},
];
}

if (document.querySelector("div.QY4Ed.P0xOK input.focus-visible")) {
presenceData.details = privacySetting ? "Searching" : "Searching:";
presenceData.state = document.querySelector<HTMLInputElement>(
"div.QY4Ed.P0xOK input.focus-visible"
)?.value;
}

if (privacySetting) {
delete presenceData.state;
delete presenceData.endTimestamp;
delete presenceData.buttons;
delete presenceData.smallImageKey;
if (presenceData.state) delete presenceData.state;
if (presenceData.endTimestamp) delete presenceData.endTimestamp;
if (presenceData.buttons) delete presenceData.buttons;
if (presenceData.smallImageKey) delete presenceData.smallImageKey;
}

presence.setActivity(presenceData);
Expand Down

0 comments on commit c4635bc

Please sign in to comment.