Guest User

Untitled

a guest
Jun 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # a slightly more advanced form filler
  4. #
  5. # uses settings file like: $keydir/<domain>
  6.  
  7. # user arg 1:
  8. # edit: force editing of the file (fetches if file is missing)
  9. # load: fill forms from file (fetches if file is missing)
  10. # new: fetch new file
  11.  
  12. # usage example:
  13. # bind LF = spawn /usr/share/uzbl/examples/scripts/formfiller2.pl
  14. # bind L _ = spawn /usr/share/uzbl/examples/scripts/formfiller2.pl %s
  15.  
  16. use strict;
  17. use Switch;
  18.  
  19. my $keydir = $ENV{XDG_CONFIG_HOME} . "/uzbl/keys";
  20. my ($config,$pid,$xid,$fifo,$socket,$url,$title,$cmd) = @ARGV;
  21. if($fifo eq "") { die "No fifo"; };
  22.  
  23. sub domain {
  24. my ($url) = @_;
  25. $url =~ s#http(s)?://([A-Za-z0-9\.-]+)(/.*)?#$2#;
  26. return $url;
  27. };
  28.  
  29. #my $editor = "xterm -e vim";
  30. my $editor = "gvim";
  31.  
  32. # ideally, there would be some way to ask uzbl for the html content instead of having to redownload it with
  33. # Also, you may need to fake the user-agent on some sites (like facebook)
  34. # my $downloader = "curl -A 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042810 GranParadiso/3.0.10' ";
  35. my $downloader = "curl -s";
  36.  
  37. my @fields = ("type","name","value");
  38.  
  39. my %command;
  40.  
  41. $command{load} = sub {
  42. my ($domain) = @_;
  43. my $file = "$keydir/$domain";
  44. if( -e $file){
  45. open(FH,$file);
  46. my (@lines) = <FH>;
  47. close(FH);
  48. $|++;
  49. open(FIFO,">>$fifo");
  50. print "opened $fifo\n";
  51. foreach my $line (@lines) {
  52. if($line !~ m/^#/){
  53. my ($type,$name,$value) = ($line =~ /\s*(\w+)\s*\|\s*(.*?)\s*\|\s*(.*?)\s*$/);
  54. switch ($type) {
  55. case "" {}
  56. case "checkbox" { printf FIFO 'act js document.getElementsByName("%s")[0].checked = %s;', $name, $value}
  57. case "submit" { printf FIFO 'act js function fs (n) {try{n.submit()} catch (e){fs(n.parentNode)}}; fs(document.getElementsByName("%s")[0]);', $name }
  58. else { printf FIFO 'act js document.getElementsByName("%s")[0].value = "%s";', $name, $value}
  59. }
  60.  
  61. print FIFO "\n";
  62. }
  63. }
  64. $|--;
  65. close(FIFO);
  66. } else {
  67. $command{new}->($domain);
  68. $command{edit}->($domain);
  69. }
  70. };
  71. $command{edit} = sub {
  72. my ($domain) = @_;
  73. my $file = "$keydir/$domain";
  74. if(-e $file){
  75. system ($editor, $file);
  76. } else {
  77. $command{new}->($domain);
  78. }
  79. };
  80. $command{new} = sub {
  81. my ($domain) = @_;
  82. my $file = "$keydir/$domain";
  83. open(FILE,">>$file");
  84. $|++;
  85. print FILE "#make sure that there are no extra submits, since it may trigger the wrong one\n";
  86. printf FILE "#%-10s | %-10s | %s\n", @fields;
  87. print FILE "#------------------------------\n";
  88. my @data = `$downloader $url`;
  89. foreach my $line (@data){
  90. if($line =~ m/<input ([^>].*?)>/i){
  91. $line =~ s/.*(<input ([^>].*?)>).*/\1/;
  92. printf FILE " %-10s | %-10s | %s\n", map { my ($r) = $line =~ /.*$_=["'](.*?)["']/;$r } @fields;
  93. };
  94. };
  95. close(FILE);
  96. $|--;
  97. };
  98. $command{$cmd}->(domain($url));
Add Comment
Please, Sign In to add comment