以前自己也写过Windows下自动打开多个浏览器测试某个URI,提高浏览器兼容性测试效率。

但是写的browse.bat文件还是最基础简陋的

  1. @echo off
  2. if '%1'=='-c' (
  3. start /d "C:\Program Files\Google\Chrome\Application\" chrome.exe -new-tab %2
  4. exit
  5. )
  6. if '%1'=='-f' (
  7. start /d "C:\Program Files\Mozilla Firefox" firefox.exe -new-tab %2
  8. exit
  9. )
  10. if '%1'=='-i' (
  11. start /d "C:\Program Files\Internet Explorer" iexplore.exe %2
  12. exit
  13. )
  14. if '%1'=='-d' (
  15. rundll32 url.dll,FileProtocolHandler %2
  16. exit
  17. )
  18.  
  19. if '%1'=='-a' (
  20. call browse -c %2
  21. call browse -f %2
  22. call browse -i %2
  23. ) else (
  24. explorer %1

今天恰巧看到cygwin安装目录下用perl写的url_handler.pl,感觉作者的思路挺有趣。

代码共赏析:C:\cygwin\bin\url_handler.pl

  1. if ($ENV{BROWSER}) {
  2. push(@try, split(/:/, $ENV{BROWSER}));
  3. } else { # set some defaults
  4. push(@try, 'firefox -a firefox -remote openURL\(%s\)');
  5. push(@try, 'mozilla -remote openURL\(%s\)');
  6. push(@try, 'opera -remote openURL\(%s\)');
  7. push(@try, 'galeon -n');
  8. push(@try, 'lynx'); # prefer lynx over links as it can handle news:-urls
  9. push(@try, qw('links2 -g' links w3m));
  10. push(@try, 'kfmclient newTab'); # has no useful return-value on error
  11. }

上面的亮点:

1.使用了@try这样一个思路,尝试打开.(没有理解perl的@try的含义?)

2.没有用很多的if-else而是用push放入一个数组(没有查阅过perl的push含义?)

  1. #! /usr/bin/perl -w
  2. # example of how to call an appropriate viewer
  3. #
  4. # URLs must start with a scheme and shell metas should be already quoted
  5. # (tin doesn't recognize URLs without a scheme and it quotes the metas)
  6.  
  7. use strict;
  8. use warnings;
  9.  
  10. (my $pname = $) =~ s#^.*/##;
  11. die "Usage: $pname URL" if $#ARGV != 0;
  12.  
  13. # version Number
  14. my $version = "0.1.1";
  15.  
  16. my ($method, $url, $match, @try);
  17. $method = $url = $ARGV[];
  18. $method =~ s#^([^:]+):.*#$1#io;
  19.  
  20. # shell escape
  21. $url =~ s#([\&\;\`\'\\\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\010\013\020\011])#\\$1#g;
  22.  
  23. if ($ENV{"BROWSER_".uc($method)}) {
  24. push(@try, split(/:/, $ENV{"BROWSER_".uc($method)}));
  25. } else {
  26. if ($ENV{BROWSER}) {
  27. push(@try, split(/:/, $ENV{BROWSER}));
  28. } else { # set some defaults
  29. push(@try, 'firefox -a firefox -remote openURL\(%s\)');
  30. push(@try, 'mozilla -remote openURL\(%s\)');
  31. push(@try, 'opera -remote openURL\(%s\)');
  32. push(@try, 'galeon -n');
  33. push(@try, 'lynx'); # prefer lynx over links as it can handle news:-urls
  34. push(@try, qw('links2 -g' links w3m));
  35. push(@try, 'kfmclient newTab'); # has no useful return-value on error
  36. }
  37. }
  38.  
  39. for my $browser (@try) {
  40. # ignore empty parts
  41. next if ($browser =~ m/^$/o);
  42. # expand %s if not preceded by odd number of %
  43. $match = $browser =~ s/(?<!%)((?:%%)*)%s/$$url/og;
  44. # expand %c if not preceded by odd number of %
  45. $browser =~ s/(?<!%)((?:%%)*)%c/$:/og;
  46. # reduce dubble %
  47. $browser =~ s/%%/%/og;
  48. # append URL if no %s expansion took place
  49. $browser .= " ".$url if (!$match);
  50. # leave loop if $browser was started successful
  51. last if (system("$browser 2>/dev/null") == );
  52. }
  53. exit ;
  54.  
  55. __END__
  56.  
  57. =head1 NAME
  58.  
  59. url_handler.pl - Spawn appropriate viewer for a given URL
  60.  
  61. =head1 SYNOPSIS
  62.  
  63. B<url_handler.pl> I<URL>
  64.  
  65. =head1 DESCRIPTION
  66.  
  67. B<url_handler.pl> takes an URL as argument and spawns the first executable
  68. viewer found in either B<$BROWSER_I<SCHEME>> or B<$BROWSER>.
  69.  
  70. =head1 ENVIRONMENT
  71.  
  72. =over
  73.  
  74. =item B<$BROWSER_I<SCHEME>>
  75.  
  76. The user's preferred utility to browse URLs of tye I<SCHEME>. May actually
  77. consist of a sequence of colon-separated browser commands to be tried in
  78. order until one succeeds. If a command part contains %s, the URL is
  79. substituted there, otherwise the browser command is simply called with the
  80. URL as its last argument. %% is replaced by a single percent sign (%), and
  81. %c is replaced by a colon (:).
  82. Examples:
  83.  
  84. =over 4
  85.  
  86. =item $BROWSER_FTP="wget:ncftp"
  87.  
  88. =item $BROWSER_GOPHER="lynx:links"
  89.  
  90. =item $BROWSER_MAILTO="mutt:pine -url"
  91.  
  92. =item $BROWSER_NEWS="lynx"
  93.  
  94. =item $BROWSER_NNTP="lynx"
  95.  
  96. =back
  97.  
  98. Z<>
  99.  
  100. =item B<$BROWSER>
  101.  
  102. The user's preferred utility to browse URLs for which there is no special
  103. viewer defined via B<$BROWSER_I<SCHEME>>. Again it may actually consist of a
  104. sequence of colon-separated browser commands to be tried in order until one
  105. succeeds. If a command part contains %s, the URL is substituted there,
  106. otherwise the browser command is simply called with the URL as its last
  107. argument. %% is replaced by a single percent sign (%), and %c is replaced
  108. by a colon (:).
  109. Examples:
  110.  
  111. =over
  112.  
  113. =item $BROWSER="firefox -a firefox -remote openURL\(%s\):opera:konqueror:links2 -g:lynx:w3m"
  114.  
  115. =back
  116.  
  117. =head1 SECURITY
  118.  
  119. B<url_handler.pl> was designed to work together with B<tin>() which only
  120. issues shell escaped absolute URLs thus B<url_handler.pl> does not try hard
  121. to shell escape its input nor does it convert relative URLs into absolute
  122. ones! If you use B<url_handler.pl> from other applications be sure to at
  123. least shell escaped its input!
  124.  
  125. =head1 AUTHOR
  126.  
  127. Urs Janssen E<lt>urs@tin.orgE<gt>
  128.  
  129. =head1 SEE ALSO
  130.  
  131. http://www.catb.org/~esr/BROWSER/
  132. http://www.dwheeler.com/browse/secure_browser.html
  133.  
  134. =cut

自己写的browse.bat与perl写的url_handler.pl的比较的更多相关文章

  1. 横竖两个数字塔的效果BAT批处理怎么写?

    横竖两个数字塔的效果BAT批处理怎么写?@echo offfor /l %%a in (0,1,1) do (        for /l %%i in (0,1,9) do (        for ...

  2. 再议perl写多线程端口扫描器

    再议perl写多线程端口扫描器 http://blog.csdn.net/sx1989827/article/details/4642179 perl写端口多线程扫描器 http://blog.csd ...

  3. 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!

    瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...

  4. 刺猬大作战(游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4)

    游戏特性[编辑] 游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4[2]. 0.9.12开始支持实时动态缩放游戏画面. 个性化[编辑] 刺猬大作战有着高度定制性 游戏模式: ...

  5. php 写内容到文件,把日志写到log文件

    php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...

  6. 以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewControllers方法即可达到效果了

    以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewC ...

  7. 出错的方法有可能是JDK,也可能是程序员写的程序,无论谁写的,抛出一定用throw

    应对未检查异常就是养成良好的检查习惯. 已检查异常是不可避免的,对于已检查异常必须实现定义好应对的方法. 已检查异常肯定跨越出了虚拟机的范围.(比如“未找到文件”) 如何处理已检查异常(对于所有的已检 ...

  8. C++代写,代写C++,C++程序代写,C++ assignment代写

    C++代写,代写C++,C++程序代写 关于C++代写,我们的涉猎范围: C++数据结构.算法题目 C++操作系统os题目 C++网络编程networking题目 C++ Linux题目 C++ Wi ...

  9. 剖析手写Vue,你也可以手写一个MVVM框架

    剖析手写Vue,你也可以手写一个MVVM框架# 邮箱:563995050@qq.com github: https://github.com/xiaoqiuxiong 作者:肖秋雄(eddy) 温馨提 ...

随机推荐

  1. Activity总结

    1)Activity在app构建中的位置: 2)与window和视图体系.事件处理的关系: 3)生命周期: 4)启动方式: 5)activity管理: 6)加载管理:

  2. Android UI开发神兵利器之Android Action Bar Style Generator

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/x359981514/article/details/26283129 ActionBar是3.0后的 ...

  3. centos7下安装docker(7docker base command 命令词典)

    上一章中我总结了学习docker 镜像时所用过的命令,今天先来将docker base command 记录一下,参考:https://docs.docker.com/edge/engine/refe ...

  4. c++函数库中一些实用的函数

    有一些程序,虽然写起来不难,但是可能比较麻烦或容易出错,这时就可以用c++函数库里自带的一些实用的函数. 这里只记录一些不太常见的函数. ------------------------------- ...

  5. JSON数据解析(自写)

    自写的JSON解析数据 void setup() { Serial.begin(115200); char chArray[50] = "some characters"; Str ...

  6. 说明split()与join()函数的区别?

    前者是切割成数组的形式,后者是将数组转换成字符串join函数获取一批字符串,然后用分隔符字符串将它们连接起来,从而返回一个字符串.Split函数获取一个字符串,然后再分隔符处将其断开,从而返回一批字符 ...

  7. infura的使用

    infura 官网: https://infura.io/本地安装geth的方法需要花比较多的时间和空间来同步区块,利用infura可以简单很多,infura提供公开以太坊和测试节点,可以利用infu ...

  8. 【Luogu P1074】靶形数独

    Luogu P1074 题意:给一个数独,问怎么填会使每个位置填的数乘以它的权值得到的和最大.其中每个位置的权值在题面中给出了. 思路:首先我们考虑搜索.由于我们不可能搜每个格子取太多的数,所以我们从 ...

  9. jquery中的选择器:has和:not的用法

    这两个选择器可以帮助我们在选择父级和子孙之间关系的dom更从容~ <div><p><span>Hello</span></p></di ...

  10. php和js字符串的acsii码函数

    简单普及下编码知识: javascript中有charCodeAt(),根据字符查找ascii码. String.fromCharCode(),根据ascii码查找对应的字符. console.log ...