Skip to content

Instantly share code, notes, and snippets.

@t-wy
Created November 28, 2022 12:22
Show Gist options
  • Select an option

  • Save t-wy/66faed8679d127793891ecb775efdaa9 to your computer and use it in GitHub Desktop.

Select an option

Save t-wy/66faed8679d127793891ecb775efdaa9 to your computer and use it in GitHub Desktop.
Get Facebook canonical Post ID (share URL) without pfbid parameter
javascript: fbid = /share_fbid":"(\d+)"/g.exec([...document.getElementsByTagName("script")].filter(x => x.innerHTML.includes("share_fbid"))[0].innerHTML)[1]; alert(location.href.split("/").slice(0, -1).join("/") + "/" + fbid);
// can be used in console / bookmark / address bar (remember to add back javascript if the browser strips it automatically)
@gcobc12632
Copy link

Further simplify~ run the following code in console will copy the shortest link form fb.com/1234567890 to clipboard

copy('fb.com/'+[...document.getElementsByTagName("script")].filter(x => x.innerHTML.includes("share_fbid"))[0].innerText.match(/share_fbid":"(.+?)",/)[1])

amazing!

@JZersche
Copy link

JZersche commented Jul 17, 2025

  (function getDebugInfo(flag) {
    const debugInfoObject = {};

    try {
      const debugMatch = document.body.outerHTML.match(/(debug_info":null,"id":")(\w+)/);
      if (!debugMatch) throw new Error("Debug info not found in the HTML.");

      const debugInfoEncoded = debugMatch[2].match(/\S{20,60}/)[0];
      const debugInfoDecoded = atob(debugInfoEncoded);
      const debugInformation = debugInfoDecoded.match(/(\d+)/g);

      if (!debugInformation) throw new Error("Invalid debug info format.");

      const profileAnchor = document.querySelector('.xjp7ctv a.x1i10hfl[role="link"][aria-label]');
      if (!profileAnchor) throw new Error("❌ profile anchor element not found");

      const profileName = profileAnchor.getAttribute('aria-label').replace(/, view story/,'');
      const profileURL = profileAnchor.href.replace(/https:\/\/www\.facebook\.com\//, '').replace(/\?.+/, '');

      const profileID = debugInformation[0] || null;
      const postID = debugInformation[1] || null;
      const postPFBID = (window.location.href.match(/posts\/(pfbid\S+)/) || [])[1] || null;

      debugInfoObject['Profile ID'] = profileID;
      debugInfoObject['Post ID'] = postID;
      debugInfoObject['Post PFBID'] = postPFBID;
      debugInfoObject['Profile Name'] = profileName;
      debugInfoObject['Profile URL'] = profileURL;

      if (flag === "ALL" || flag === 0) console.log("Debug Info Object:", debugInfoObject);
      if (flag === "PROFILEID" || flag === 1) console.log("Profile ID:", profileID);
      if (flag === "POSTID" || flag === 2) console.log("Post ID:", postID);
      if (flag === "POSTPFBID" || flag === 2) console.log("Post PFBID:", postPFBID);
      if (flag === "COMBINE" || flag === 3) console.log("Combined URL:", 'facebook.com/' + profileID + '/posts/' + postID);

      const regex = /(facebook\.com\/(\d+)\/posts\/(\d+)|facebook\.com\/([a-zA-Z]{1}\S+)\/posts\/(\d+)|\/pfbid[0-9a-zA-Z]+)/;
      const match = window.location.href.match(regex);
      if (match) {
        const filtered = match.filter((val, idx) => val !== undefined && idx > 0);
        debugInfoObject['URL Match'] = filtered;
      } else {
        debugInfoObject['URL Match'] = null;
      }

    } catch (error) {
      console.error("Error:", error.message);
    }

    console.log("DEBUG FLAG:", flag);
    return debugInfoObject;

  })('ALL');

Here's another way.

@KittisakPh
Copy link

Thx God @JZersche !!!

@JZersche
Copy link

JZersche commented Dec 8, 2025

due to some Facebook updates, this only works if you're not logged in, otherwise it still works but returns some wrong information related too the profileAnchor selecting the wrong profile, and displaying the wrong name. But it still returns the correct post id, and works fine if you're not logged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment