1

If I write code in the JavaScript console of Chrome, I can retrieve the whole HTML source code by entering:

  var a = document.body.InnerHTML; alert(a); 

For fb_dtsg on Facebook, I can easily extract it by writing:

  var fb_dtsg = document.getElementsByName('fb_dtsg')[0].value;

Now, I am trying to extract the code "h=AfJSxEzzdTSrz-pS" from the Facebook Page. The h value is especially useful for Facebook reporting.

How can I get the h value for reporting? I don't know what the h value is; the h value is totally different when you communicate with different users. Without that h correct value, you can not report. Actually, the h value is AfXXXXXXXXXXX (11 character values after 'Af'), that is what I know.

Do you have any ideas for getting the value or any function to generate on Facebook page.

The Facebook Source snippet is below, you can view source on facebook profile, and search h=Af, you will get the value:

  <code class="hidden_elem" id="ukftg4w44">
<!-- <div class="mtm mlm">
  ...
   ....
  <span class="itemLabel fsm">Unfriend...</span></a></li>
  <li class="uiMenuItem" data-label="Report/Block...">
  <a class="itemAnchor" role="menuitem" tabindex="-1" href="/ajax/report/social.php?content_type=0&amp;cid=1352686914&amp;rid=1352686914&amp;ref=http%3A%2F%2Fwww.facebook.com%2      F%3Fq&amp;h=AfjSxEzzdTSrz-pS&amp;from_gear=timeline" rel="dialog">
  <span class="itemLabel fsm">Report/Block...</span></a></li></ul></div>

  ...
   ....
  </div> -->
  </code>

Please guide me. How can extract the value exactly?

I tried with following code, but the comment block prevent me to extract the code. How can extract the value which is inside comment block?

 var a = document.getElementsByClassName('hidden_elem')[3].innerHTML;alert(a);
Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60
Hafizi Vilie
  • 37
  • 1
  • 7
  • 1
    Why not just use the actual API? It has to be easier than illegally* scraping the main website (*- may not be illegal in your country, not a lawyer, etc etc) – Igy Nov 20 '12 at 23:04

1 Answers1

2

Here's my first attempt, assuming you aren't afraid of a little jQuery:

// http://stackoverflow.com/a/5158301/74757
function getParameterByName(name, path) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var html = $('.hidden_elem')[0].innerHTML.replace('<!--', '').replace('-->', '');
var href = $(html).find('.itemAnchor').attr('href');
var fbId = getParameterByName('h', href); // fbId = AfjSxEzzdTSrz-pS

Working Demo

EDIT: A way without jQuery:

// http://stackoverflow.com/a/5158301/74757
function getParameterByName(name, path) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(path);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var hiddenElHtml = document.getElementsByClassName('hidden_elem')[0]
    .innerHTML.replace('<!--', '').replace('-->', '');

var divObj = document.createElement('div');
divObj.innerHTML = hiddenElHtml;

var itemAnchor = divObj.getElementsByClassName('itemAnchor')[0];
var href = itemAnchor.getAttribute('href');

var fbId = getParameterByName('h', href);

Working Demo

I'd really like to offer a different solution for "uncommenting" the HTML, but I stink at regex :)

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • Cory, could you do me more one quick favor? I can use Jquery. it works fine. In that comment block, there are 6 links which has itemAnchor class. Basically your function pickup the first link. So How can I pickup the 6th link which has ItemAnchor class? Thank you very much. You can explain to me with Jquery. thanks Cory. You helps means a lot to me. – Hafizi Vilie Nov 21 '12 at 01:10
  • Please can you reslove this - http://jsfiddle.net/rtnNd/3/ Can you resolve for 6th class link? I tried with following code, it does not work for me. `var item = $('.itemAnchor')[6]; var href = $($('.hidden_elem')[1].innerHTML.replace('', '')).find(item).attr('href'); ` – Hafizi Vilie Nov 21 '12 at 01:43
  • Cory - http://stackoverflow.com/questions/13485484/extracting-values-from-array-with-javascript – Hafizi Vilie Nov 21 '12 at 02:31
  • @HafiziVilie: Looks like somebody beat me to it. Hopefully you've got all the answers you were looking for! – Cᴏʀʏ Nov 21 '12 at 15:39
  • @Cory: could you help me for final, this will be last question, thank you very much for my non-profit community project. – Hafizi Vilie Nov 23 '12 at 04:19
  • [link](http://stackoverflow.com/questions/13521211/callback-behavior-of-javascript-object-error-after-xmlhttprequest-onreadystatech) please cory can you help me. – Hafizi Vilie Nov 24 '12 at 01:47
  • 1
    @HafiziVilie: Please keep in mind that [so] is not a code-writing service. I've helped as much as I'm willing to spend time doing. I hope somebody else will help you with a solution. – Cᴏʀʏ Nov 24 '12 at 02:01
  • Sorry, if you are getting that way... I am really sorry. I am working on non-profit script for community only. I thought you could help me with some solutions. thanks for your help. – Hafizi Vilie Nov 24 '12 at 05:06