Created
          November 28, 2022 12:22 
        
      - 
      
- 
        Save t-wy/66faed8679d127793891ecb775efdaa9 to your computer and use it in GitHub Desktop. 
    Get Facebook canonical Post ID (share URL) without pfbid parameter
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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) | 
- Open Inspector
- Ctrl+F looks for "share_fbid" in HTML
- Get the value of data-content-len="??????"
- In console, try
 $x('//*[contains(@data-content-len,??????)]')[0].innerText.match(/share_fbid":"(.+?)",/)[1]
- If success, the pfbid can be replaced by this result
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])Further simplify~ run the following code in console will copy the shortest link form
fb.com/1234567890to clipboardcopy('fb.com/'+[...document.getElementsByTagName("script")].filter(x => x.innerHTML.includes("share_fbid"))[0].innerText.match(/share_fbid":"(.+?)",/)[1])
amazing!
  (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.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
@gcobc12632 Facebook has created several URL formats since then. Some URL still keeps the ID, and for those looking like
https://www.facebook.com/.../posts/pfbid..., the post ID can no longer be found in any element.However you may still try to create an embed via "…" at the top-right corner of the post or through https://developers.facebook.com/docs/plugins/embedded-posts/. The hyperlink tied to the Facebook logo at the top right corner should still be pointing to the URL with the original ID instead of the post ID.