Firebug控制台详解

Firebug是网页开发的利器,能够极大地提升工作效率。

但是,它不太容易上手。我曾经翻译过一篇《Firebug入门指南》,介绍了一些基本用法。今天,继续介绍它的高级用法。

===================================

Firebug控制台详解

作者:阮一峰


控制台(Console)是Firebug的第一个面板,也是最重要的面板,主要作用是显示网页加载过程中产生各类信息。

一、显示信息的命令

Firebug内置一个console对象,提供5种方法,用来显示信息。

最简单的方法是console.log(),可以用来取代alert()或document.write()。比如,在网页脚本中使用console.log("Hello World"),加载时控制台就会自动显示如下内容。

另外,根据信息的不同性质,console对象还有4种显示信息的方法,分别是一般信息console.info()、除错信息console.debug()、警告提示console.warn()、错误提示console.error()。

比如,在网页脚本中插入下面四行:

  console.info("这是info");

  console.debug("这是debug");

  console.warn("这是warn");

  console.error("这是error");

加载时,控制台会显示如下内容。

可以看到,不同性质的信息前面有不同的图标,并且每条信息后面都有超级链接,点击后跳转到网页源码的相应行。

二、占位符

console对象的上面5种方法,都可以使用printf风格的占位符。不过,占位符的种类比较少,只支持字符(%s)、整数(%d或%i)、浮点数(%f)和对象(%o)四种。

比如,

  console.log("%d年%d月%d日",2011,3,26);

  console.log("圆周率是%f",3.1415926);

%o占位符,可以用来查看一个对象内部情况。比如,有这样一个对象:

  var dog = {} ;

  dog.name = "大毛" ;

  dog.color = "黄色";

然后,对它使用o%占位符。

  console.log("%o",dog);

三、分组显示

如果信息太多,可以分组显示,用到的方法是console.group()和console.groupEnd()。

  console.group("第一组信息");

    console.log("第一组第一条");

    console.log("第一组第二条");

  console.groupEnd();

  console.group("第二组信息");

    console.log("第二组第一条");

    console.log("第二组第二条");

  console.groupEnd();

点击组标题,该组信息会折叠或展开。

四、console.dir()

console.dir()可以显示一个对象所有的属性和方法。

比如,现在为第二节的dog对象,添加一个bark()方法。

  dog.bark = function(){alert("汪汪汪");};

然后,显示该对象的内容,

  console.dir(dog);

五、console.dirxml()

console.dirxml()用来显示网页的某个节点(node)所包含的html/xml代码。

比如,先获取一个表格节点,

  var table = document.getElementById("table1");

然后,显示该节点包含的代码。

  console.dirxml(table);

六、console.assert()

console.assert()用来判断一个表达式或变量是否为真。如果结果为否,则在控制台输出一条相应信息,并且抛出一个异常。

比如,下面两个判断的结果都为否。

  var result = 0;

  console.assert( result );

  var year = 2000;

  console.assert(year == 2011 );

七、console.trace()

console.trace()用来追踪函数的调用轨迹。

比如,有一个加法器函数。

  function add(a,b){

    return a+b;

  }

我想知道这个函数是如何被调用的,在其中加入console.trace()方法就可以了。

  function add(a,b){

    console.trace();

    return a+b;

  }

假定这个函数的调用代码如下:

  var x = add3(1,1);

  function add3(a,b){return add2(a,b);}

  function add2(a,b){return add1(a,b);}

  function add1(a,b){return add(a,b);}

运行后,会显示add()的调用轨迹,从上到下依次为add()、add1()、add2()、add3()。

八、计时功能

console.time()和console.timeEnd(),用来显示代码的运行时间。

  console.time("计时器一");

  for(var i=0;i<1000;i++){

    for(var j=0;j<1000;j++){}

  }

  console.timeEnd("计时器一");

九、性能分析

性能分析(Profiler)就是分析程序各个部分的运行时间,找出瓶颈所在,使用的方法是console.profile()。

假定有一个函数Foo(),里面调用了另外两个函数funcA()和funcB(),其中funcA()调用10次,funcB()调用1次。

  function Foo(){

    for(var i=0;i<10;i++){funcA(1000);}

    funcB(10000);

  }

  function funcA(count){

    for(var i=0;i<count;i++){}

  }

  function funcB(count){

    for(var i=0;i<count;i++){}

  }

然后,就可以分析Foo()的运行性能了。

  console.profile('性能分析器一');

  Foo();

  console.profileEnd();

控制台会显示一张性能分析表,如下图。

标题栏提示,一共运行了12个函数,共耗时2.656毫秒。其中funcA()运行10次,耗时1.391毫秒,最短运行时间0.123毫秒,最长0.284毫秒,平均0.139毫秒;funcB()运行1次,耗时1.229ms毫秒。

除了使用console.profile()方法,firebug还提供了一个"概况"(Profiler)按钮。第一次点击该按钮,"性能分析"开始,你可以对网页进行某种操作(比如ajax操作),然后第二次点击该按钮,"性能分析"结束,该操作引发的所有运算就会进行性能分析。

十、属性菜单

控制台面板的名称后面,有一个倒三角,点击后会显示属性菜单。

默认情况下,控制台只显示Javascript错误。如果选中Javascript警告、CSS错误、XML错误都送上,则相关的提示信息都会显示。

这里比较有用的是"显示XMLHttpRequests",也就是显示ajax请求。选中以后,网页的所有ajax请求,都会在控制台面板显示出来。

比如,点击一个YUI示例,控制台就会告诉我们,它用ajax方式发出了一个GET请求,http请求和响应的头信息和内容主体,也都可以看到。

current working directory of a process

lsof -p {pid}
pwdx {pid}

10 个最酷的 Linux 单行命令


  1. sudo !!

    以 root 帐户执行上一条命令。

  2. python -m http.server 8000

    利用 Python 搭建一个简单的 Web 服务器,可通过 http://$HOSTNAME:8000 访问。

  3. :w !sudo tee %

    在 Vim 中无需权限保存编辑的文件。

  4. cd -

    更改到上一次访问的目录。

  5. ^foo^bar

    将上一条命令中的 foo 替换为 bar,并执行。

  6. cp filename{,.bak}

    快速备份或复制文件。

  7. mtr google.com

    traceroute + ping。

  8. !whatever:p

    搜索命令历史,但不执行。

  9. $ssh-copy-id user@host

    将 ssh keys 复制到 user@host 以启用无密码 SSH 登录。

  10. ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg

    把 Linux 桌面录制为视频。

How can I generate random numbers in shell scripts?

How can I generate random numbers in shell scripts?

   This depends on the shell, and the facilities available from the
   OS.

   a. Some shells have a variable called RANDOM, which evaluates to a
      different value every time you dereference it. If your shell has
      this variable,

        $ number=$RANDOM will produce a random number.

   b. Some systems have a /dev/urandom device, which generates a
      stream of bits. This can be accessed using the dd(1) utility. An
      example of this (from a more extensive discussion of different
      techniques at http://www.shelldorado.com/scripts/cmds/rand)

        n=`dd if=/dev/urandom bs=1 count=4 2>/dev/null | od -t u4 | \
        awk 'NR==1 {print $2}'`

        also:

        od -vAn -N4 -tu4 < /dev/urandom

   c. Use a utility such as awk(1), which has random number generation
      included. This approach is the most portable between shells and
      operating systems.

        awk 'BEGIN {srand();print rand()}'

      Note that this doesn't work with older versions of awk. This
      requires a version supporting the POSIX spec for srand(). For
      example, on Solaris this will not work with /usr/bin/awk, but
      will with nawk or /usr/xpg4/bin/awk.

      Also, if you call this line more than once within the same
      second, you'll get the same number you did the previous time.

各个Linux版本的本地root密码破解方法

各个Linux版本的本地root密码破解方法
这段时间老碰到有人问及各个linux版本的本地root密码破解方法,我这里自己以及在网络上搜集了些资料,希望对看到了这文章而又恰好用得到的技术人员有点帮助:

(一)RedHat/CentOS/Fedora 系统密码破解

1.在grub选项菜单按E进入编辑模式
2.编辑kernel 那行最后加上S (或者Single)
3.按B,启动到single-user mode
4.进入后执行下列命令
# mount -t proc proc /proc
# mount -o remount,rw /
#passwd
#sync
#reboot

(二)Debian linux 系统密码破解

1.在grub选项菜单'Debian GNU/Linux,...(recovery mode)',按e进入编辑模式
2.编辑kernel那行最后面的 ro single 改成 rw single init=/bin/bash,按b执行重启
3.进入后执行下列命令
root@(none)#mount -a
root@(none)#passwd root
root@(none)#reboot

(三)Freebsd 系统密码破解

1.开机进入引导菜单
2.选择每项(按4)进入单用户模式
3.进入之后输入一列命令
root@#mount -a
root@#fsck -y
root@#passwd(修改密码命令)
root@#root(要破解密码的用户名)
Enter new unix password:
root@#init 6 (重启)

(四)Solaris 系统密码破解

1.在grub选项菜中选择solaris failasfe 项
2.系统提示Do you wish to have it mounted read-write on /a ?[y,n,?] 选择y
3.就进入单用户模式
4.输入下列命令:passwd
root@#init 6 (重启)

(五)NetBsd 系统密码破解

1.开机:当出现提示符号并开始倒数五秒时, 键入以下指令:
> boot -s (进入单用户模式命令)
2.在以下的提示符号中
Enter pathname of shell or RETURN for sh:
按下 Enter。
3.键入以下指令:
# mount -a
# fsck -y
4.使用 passwd 更改 root 的密码。
5.使用 exit 指令进入多人模式。

(六)SUSE 系统密码破解

1.重新启动机器,在出现grub引导界面后,在启动linux的选项里加上init=/bin/bash,通过给内核传递init=/bin/bash参数使得OS在运行login程序之前运行bash,出现命令行。
2.稍等片刻出现(none)#:命令行。
3.这时输入mount -n / -o remount,rw 表示将根文件系统重新mount为可读写,有了读写权限后就可以通过passwd命令修改密码了。
4.这时输入passwd命令就可以重置密码了
5.修改完成后记得用mount -n / -o remount,ro将根文件系统置为原来的状态。