用JavaScript提取Facebook页面的源代码 [英] Extracting the source code of a facebook page with JavaScript

查看:175
本文介绍了用JavaScript提取Facebook页面的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var a = document.body.InnerHTML;警报(A); 

对于Facebook上的fb_dtsg,我可以通过写入来轻松地解压缩:

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

现在,我试图从Facebook页面中提取代码h = AfJSxEzzdTSrz-pS。 h值对于Facebook报告特别有用。



如何获取报告的h值?我不知道h值是多少?当您与不同用户进行通信时,h值完全不同。没有这个正确的价值,你不能报告。实际上,h值是AfXXXXXXXXXXX(Af之后的11个字符值),这就是我所知道的。



您是否有任何想法来获取价值或任何功能在Facebook页面上生成。



Facebook来源片段在下面,您可以在Facebook个人资料中查看来源,并搜索h = Af,您将获得以下值:

  ; code class =hidden_​​elemid =ukftg4w44> 
<! - < div class =mtm mlm>
...
....
< span class =itemLabel fsm> Unfriend ...< / span>< / a>< / li>
< li class =uiMenuItemdata-label =Report / Block ...>
< a class =itemAnchorrole =menuitemtabindex = - 1href =/ ajax / report / social.php?content_type = 0& amp; cid = 1352686914& amp; amp; ; amp; ref = http%3A%2F%2Fwww.facebook.com%2 F%3Fq& amp; h = AfjSxEzzdTSrz-pS& amp; from_gear =时间线rel =dialog>
< span class =itemLabel fsm>报告/块...< / span>< / a>< / li>< / ul>< / div>

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

请指导我。如何精确提取价值?



我尝试使用以下代码,但评论块阻止我提取代码。如何提取评论块内的值?

  var a = document.getElementsByClassName('hidden_​​elem')[3] .innerHTML; alert(a); 


解决方案

这是我第一次尝试,假设你不害怕的一点jQuery:

  // http://stackoverflow.com/a/5158301/74757 
function getParameterByName (name,path){
var match = RegExp('[?&]'+ name +'=([^&] *)')exec(path);
返回匹配&& 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

工作演示



编辑:没有jQuery的方式:

  // http://stackoverflow.com/a/5158301/74757 
函数getParameterByName(name,path) {
var match = RegExp('[?&]'+ name +'=([^&] *)')exec(path);
返回匹配&& 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);

工作演示



我真的想为取消注释HTML提供不同的解决方案,但是stag at regex:)


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);

解决方案

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 :)

这篇关于用JavaScript提取Facebook页面的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆