Try to Get Facebook User Id by URL Change and Chrome Extension


Sometimes Facebook URL will change from:

https://www.facebook.com/profile.php?id=12345678

To

https://www.facebook.com/my.user.name

From the URL change, we know the id of my.user.name is 12345678.

So I write a Chrome extension to detect URL change in facebook.com and help you get the id of users by URL change.

You can load extension in developer mode. When the URL change is detected, you can see the user name : id pair in the extension console (not Chrome DevTools console).

manifest json:

manifest.json | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "manifest_version": 2,

  "name": "FBUrlId",
  "description" : "Try to get Facebook Id from URL change",
  "version": "0.1",

  "background": {
      "scripts": ["eventPage.js"]
  },

  "permissions": [
    "tabs"
  ]
}

background.js:

eventPage.js | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var reId = /^https:\/\/www\.facebook\.com\/profile\.php\?id=(\d+)$/;
var reUser = /^https:\/\/www\.facebook\.com\/([a-zA-Z0-9.]+)$/;
var lastUrlIsId = false;

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.hasOwnProperty('url')) {

    console.log(changeInfo.url);

    // check if URL consists of username
    var resultUser = changeInfo.url.match(reUser);
    if (resultUser) {
      //console.log(resultUser[1]); // username
      if (lastUrlIsId) {
        console.log(resultUser[1] + " : " + id);
      }
    }

    // check if URL consists of Id
    var resultId = changeInfo.url.match(reId);
    if (resultId) {
      //console.log(resultId[0]); // URL
      //console.log(resultId[1]); // id
      id = resultId[1];
      lastUrlIsId = true;
    } else {
      lastUrlIsId = false;
    }

  }
});

References:

[1]javascript - How to listen for url change with Chrome Extension - Stack Overflow
[2]debugging - Where to read console messages from background.js in a Chrome extension? - Stack Overflow
[3]