fedimbed: stupid but funny if works
This commit is contained in:
parent
e635d2324b
commit
f24e645c62
1 changed files with 21 additions and 7 deletions
|
@ -33,6 +33,7 @@ const PATH_REGEX = {
|
|||
//cohost: /^\/[A-Za-z0-9]+\/post\/\d+-[A-Za-z0-9-]+\/?/,
|
||||
bluesky: BSKY_POST_REGEX,
|
||||
twitter: /^\/([a-z0-9_]+)\/status\/(\d+)\/?/i,
|
||||
truthsocial: /^\/@(.+?)\/posts\/\d+\/?/i,
|
||||
};
|
||||
|
||||
const PLATFORM_COLORS = {
|
||||
|
@ -113,13 +114,22 @@ const numberFormatter = Intl.NumberFormat("en-US");
|
|||
const domainCache = new Map();
|
||||
domainCache.set("cohost.org", "cohost"); // no nodeinfo
|
||||
|
||||
function firefoxUseragent() {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const version = Math.floor(124 + (now - 1710892800) / 2419200);
|
||||
|
||||
return `\`Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${version}.0) Gecko/20100101 Firefox/${version}.0\``;
|
||||
}
|
||||
|
||||
async function resolvePlatform(url) {
|
||||
const urlObj = new URL(url);
|
||||
if (domainCache.has(urlObj.hostname)) return domainCache.get(urlObj.hostname);
|
||||
|
||||
const ffua = firefoxUseragent();
|
||||
|
||||
try {
|
||||
const res = await fetch(urlObj.origin + "/.well-known/nodeinfo", {
|
||||
headers: {"User-Agent": FRIENDLY_USERAGENT},
|
||||
headers: {"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT},
|
||||
}).then((res) => res.text());
|
||||
|
||||
if (!res.startsWith("{")) {
|
||||
|
@ -137,7 +147,7 @@ async function resolvePlatform(url) {
|
|||
}
|
||||
|
||||
const nodeinfo = await fetch(probe.links[probe.links.length - 1].href, {
|
||||
headers: {"User-Agent": FRIENDLY_USERAGENT},
|
||||
headers: {"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT},
|
||||
}).then((res) => res.json());
|
||||
|
||||
if (!nodeinfo?.software?.name) {
|
||||
|
@ -171,7 +181,7 @@ async function getCrawledData(url, color, platformName) {
|
|||
try {
|
||||
const page = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? firefoxUseragent() : FRIENDLY_USERAGENT,
|
||||
},
|
||||
})
|
||||
.then((res) => res.text())
|
||||
|
@ -761,13 +771,14 @@ async function bluesky(msg, url, spoiler = false, minimal = false) {
|
|||
async function fetchPost(url, platform, forceMastoAPI = false) {
|
||||
let urlObj = new URL(url);
|
||||
let postData;
|
||||
const ffua = firefoxUseragent();
|
||||
|
||||
if (!forceMastoAPI) {
|
||||
let rawPostData;
|
||||
try {
|
||||
rawPostData = await signedFetch(url, {
|
||||
headers: {
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
}).then((res) => res.text());
|
||||
|
@ -778,7 +789,7 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
try {
|
||||
rawPostData = await fetch(url, {
|
||||
headers: {
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
Accept: "application/activity+json",
|
||||
},
|
||||
}).then((res) => res.text());
|
||||
|
@ -831,6 +842,9 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
redirUrl = urlObj.origin + "/api/v1/statuses/" + postId;
|
||||
} else if (PATH_REGEX.mastodon2.test(urlObj.pathname)) {
|
||||
redirUrl = url.replace(/^\/(.+?)\/statuses/, "/api/v1/statuses");
|
||||
} else if (urlObj.hostname === "truthsocial.com") {
|
||||
const postId = urlObj.pathname.match(PATH_REGEX.truthsocial)?.[2];
|
||||
redirUrl = urlObj.origin + "/api/v1/statuses/" + postId;
|
||||
} else if (PATH_REGEX.misskey.test(urlObj.pathname)) {
|
||||
let noteId = url.split("/notes/")[1];
|
||||
if (noteId.indexOf("/") > -1) {
|
||||
|
@ -867,7 +881,7 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
redirUrl,
|
||||
Object.assign(options, {
|
||||
headers: Object.assign(headers, {
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
}),
|
||||
})
|
||||
).then((res) => res.text());
|
||||
|
@ -880,7 +894,7 @@ async function fetchPost(url, platform, forceMastoAPI = false) {
|
|||
redirUrl,
|
||||
Object.assign(options, {
|
||||
headers: Object.assign(headers, {
|
||||
"User-Agent": FRIENDLY_USERAGENT,
|
||||
"User-Agent": urlObj.hostname === "truthsocial.com" ? ffua : FRIENDLY_USERAGENT,
|
||||
}),
|
||||
})
|
||||
).then((res) => res.text());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue