Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Created November 26, 2023 15:13
Show Gist options
  • Save dvygolov/5aa497808b08e60b591161e69cff1951 to your computer and use it in GitHub Desktop.
Save dvygolov/5aa497808b08e60b591161e69cff1951 to your computer and use it in GitHub Desktop.
This script redirects you to a post with real ID from the post page with pfbid.... Just run it in browser's console on the pfbid... post's page.
async function fetchFacebookPostId() {
const inputUrl = window.location.href;
if (!inputUrl || !inputUrl.includes("pfbid")) {
alert("No pfbid found in URL!");
return;
}
const fetchUrl = `https://www.facebook.com/plugins/post.php?href=${encodeURIComponent(inputUrl)}`;
try {
const response = await fetch(fetchUrl);
const html = await response.text();
const realId = extractRealId(html);
if (realId) {
const realUrl = `https://www.facebook.com${realId}`;
window.location.href = realUrl;
} else {
alert("Unable to extract the real post ID!");
}
} catch (error) {
console.error("Error fetching the post.php URL:", error);
}
}
function extractRealId(htmlString) {
// Create a new DOM Parser
var parser = new DOMParser();
// Parse the HTML string
var doc = parser.parseFromString(htmlString, 'text/html');
// Use querySelector to find the element with the specific href
var link = doc.querySelector('div[role=feed]>a');
// Check if the link is found
if (link) {
// Extract the href attribute
var href = link.getAttribute('href');
return href;
} else {
// Return a default or error message if not found
return null;
}
}
await fetchFacebookPostId();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment