Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS 

Getting Started With WWW::Mechanize

This Ruby code uses WWW:mechanize to act like a web browser.

 require 'rubygems'
 require 'mechanize'

 agent = WWW::Mechanize.new
 page = agent.get('http://google.com/')


Refer to the documentation at http://mechanize.rubyforge.org/mechanize/. Then gem install mechanize, and try running the code in an irb session.

output (extract):
=> #<WWW::Mechanize::Page
 {url #<URI::HTTP:0xfdbbbb286 URL:http://www.google.com/>}
 {meta}
 {title "Google"}
 {iframes}
 {frames}
 {links
  #<WWW::Mechanize::Page::Link
   "Images"
   "http://images.google.com/imghp?hl=en&tab=wi">
  #<WWW::Mechanize::Page::Link
   "Maps"
   "http://maps.google.com/maps?hl=en&tab=wl">
  #<WWW::Mechanize::Page::Link
   "News"
   "http://news.google.com/nwshp?hl=en&tab=wn">
  #<WWW::Mechanize::Page::Link
   "Shopping"
   "http://www.google.com/prdhp?hl=en&tab=wf">
  #<WWW::Mechanize::Page::Link
   "Gmail"
   "http://mail.google.com/mail/?hl=en&tab=wm">

How to detect a browser iPhone ?

The browser is :


Mozilla/5.0 (iPhone; U; CPU like Mac OS X; fr) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3



Source: ab-d.fr
Internet with iPhone

Browser automation using perl LWP

// This is a sample code used to measure reports response times on a OAS application.
#!/usr/bin/perl
#
# LWP connection to the Datamart Portal
# Timing of the main reports
#

use strict;
require LWP::UserAgent;

my $ua = LWP::UserAgent->new;

sub isodate() {
        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
        $mon++; # 0-based index
        $year = $year + 1900;
        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
        return $date;
}

sub datamart_login {
        my ( $user, $pass ) = @_;
        my $time_begin=time();
        my $url='http://daprd:7782/portal/page?_pageid=37,134413,37_134422&_dad=portal&_schema=PORTAL';
        my $req = HTTP::Request->new( GET => $url );
        my $resp = $ua->request($req);
        my $loginform = $resp->content ;
        if ( $loginform !~ /Entrez votre nom utilisateur/ ) {
                die isodate()." Failed to get the logon page of the Web Site\n";
        } else {
                my $locale="";
                my ($v) = $loginform =~ /NAME=\"v\" value=\"(.+)\"/;
                my ($site2pstoretoken) = $loginform =~ /NAME=\"site2pstoretoken\" value=\"(.+)\"/;
                my ($submiturl) = $loginform =~ /form method=\"POST\" action=\"(.*?)\"/;
                $resp = $ua->post( $submiturl,
                   [
                     ssousername => $user,
                     password => $pass,
                     v => $v,
                     locale => $locale,
                     site2pstoretoken => $site2pstoretoken
                   ],
                );
                $resp = $ua->get($url);
                $resp = $ua->get($url);
                if ( $resp->content !~ /Crit..res de recherche/ ) {
                        die isodate()." Failed to get the main page of Portal\n";
                }
        }
        print join(";",isodate(),"Time to log on the Portal",time()-$time_begin,$url)."\n";
}

sub datamart_testurl {
        my ($label,$url,$expected)=@_;
        my $time_begin=time();
        my $resp;
        $resp = $ua->get($url);
        $resp = $ua->get($url) if $resp->content !~ /$expected/;        # We try two times !
        if ( $resp->content !~ /$expected/ ) {
                print STDERR isodate()." Test Failed on $label. $expected not found in response.\n";
                print STDERR $resp->as_string;
        } else {
                print join(";",isodate(),$label,time()-$time_begin,$url)."\n";
        }
}

$ua->timeout(1200);
$ua->cookie_jar({});
$ua->agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' );
#push @{ $ua->requests_redirectable }, 'POST';

# >>>> Main code here

datamart_login("xxxx","xxxx");

open FH,"/exploit/scripts/appli/check_datamart.ini" or die "Unable to open check_datamart.ini";
while (<FH>) {
        chomp();
        my ($report,$expected,$url) = split /;/;
        datamart_testurl($report,$url,$expected);
}
close FH;

# <<<< Main code here

Conditional comment

From the microsoft website. Better than browser sniffing, the content within the tags will be shown if the condition within [] is met, in the this case IE, but you could also use IE 7 for example
  <!--[if IE]>
    Content
  <![endif]-->

  <!--[if lt IE6]> //lower than IE6
    Content
  <![endif]-->


and the XSL version

  <xsl:comment>[if IE 7]&gt;
    Content &lt;![endif]</xsl:comment>


yubnub.py

#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="9 Dec 2006 - 10 Dec 2006"
__copyright__="Copyright 2006 Andrew Pennebaker"
__license__="GPL"
__version__="0.0.1"
__credits__="Based on Yubnub for Windows (http://www.opbarnes.com/blog/Programming/OPB/Utilities/yubnub.html)"
__URL__="http://snippets.dzone.com/posts/show/3120"

from html2txt import html2txt

import webbrowser
from urllib import urlopen
import re

import sys
from getopt import getopt

PARSER="http://yubnub.org/parser/parse?command="

BROWSER_MODE="BROWSER"
PLAIN_MODE="PLAIN"

def space2plus(s):
	return "+".join(s.split())

def yubnub(command=""):
	global PARSER

	return PARSER+space2plus(command)

def yubnubBrowser(command):
	return webbrowser.open(yubnub(command))

def cleanHTML(html):
	h=html2txt()
	h.feed(html)
	h.close()

	return h.output()

def yubnubPlain(command, clean=True):
	command=yubnub(command)

	try:
		url=urlopen(command)
		lines=url.readlines()
		url.close()

		lines="".join(lines)

		if clean:
			return cleanHTML(lines)

		return lines

	except IOError, e:
		return "Error connecting to "+command

def usage():
	print "Usage: "+sys.argv[0]+" [options] <command>"
	print "-b --browser"
	print "\n--plain (default)"
	print "\t-c --clean (default)"
	print "\t-d --dirty"
	print "\n--parser <parser> (experimental)"
	print "\n-h --help"

	sys.exit()

def main():
	global PARSER

	global BROWSER_MODE
	global PLAIN_MODE

	mode=PLAIN_MODE
	parser=PARSER
	clean=True

	systemArgs=sys.argv[1:]
	optlist, args=[], []
	try:
		optlist, args=getopt(systemArgs, "bhcd", ["browser", "plain", "clean", "dirty", "parser=", "help"])
	except:
		usage()

	for option, value in optlist:
		if option=="-h" or option=="--help":
			usage()

		elif option=="-b" or option=="--browser":
			mode=BROWSER_MODE
		elif option=="--plain":
			mode=PLAIN_MODE
		elif option=="-c" or option=="--clean":
			clean=True
		elif option=="-d" or option=="--dirty":
			clean=False
		elif option=="--parser":
			parser=value

	command=" ".join(args)

	if mode==BROWSER_MODE:
		yubnubBrowser(command)
	elif mode==PLAIN_MODE:
		for line in yubnubPlain(command, clean):
			sys.stdout.write(line)
		print ""

if __name__=="__main__":
	main()

Nokia - User Agent

// User Agent

Modello 3230
"Nokia3230/2.0 (5.0614.0) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0
Configuration/CLDC-1.0"

Modello 6260
"Nokia6260/2.0 (3.0448.0) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0
Configuration/CLDC-1.0"

Modello 6600
"Nokia6600/1.0 (5.27.0) SymbianOS/7.0s Series60/2.0 Profile/MIDP-2.0
Configuration/CLDC-1.0"

Modello 6620
"Nokia6620/2.0 (4.22.1) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0
Configuration/CLDC-1.0"

Modello 6630
"Nokia6630/1.0 (5.03.08) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0
Configuration/CLDC-1.1"

Modello 6670
"Nokia6670/2.0 (6.0540.0) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0
Configuration/CLDC-1.0"

Modello 6680
"Nokia6680/1.0 (4.04.07) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0
Configuration/CLDC-1.1"

Modello 6681
"Nokia6681/2.0 (5.37.01) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0
Configuration/CLDC-1.1"

Modello 9300
"Mozilla/4.0 (compatible; MSIE 5.0; Series80/2.0 Nokia9300/05.22
Profile/MIDP-2.0 Configuration/CLDC-1.1)"

Modello 9500
"Mozilla/4.0 (compatible; MSIE 5.0; Series80/2.0 Nokia9500/4.51
Profile/MIDP-2.0 Configuration/CLDC-1.1)"

cross-browser way to get selection.

function getSel(){
var w=window,d=document,gS='getSelection';
return (''+(w[gS]?w[gS]():d[gS]?d[gS]():d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}

Cool Effects in Browser

// description of your code here

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5); void(0);

Javascript Browser Check

// the navigator object stores the browser and additional secifications

<html><head><title>JS Browser Check</title></head><body>

<script type="text/javascript">
document.write("Your browser is " + navigator.appName);

if (navigator.appVersion.substring(0, 1) == "4")
  document.write("4th generation browser!");
  
document.write("patform/os:" + navigator.platform);
document.write("user agent data:" + navigator.userAgent);

if (navigator.cookieEnabled == true) {
  document.write("cookies enabled");
} else if (navigator.cookieEnabled == false) {
  document.write("no cookies.");
} else {
  document.write("cookies? No Info available.");
}

if (navigator.javaEnabled()) {
  document.write("java enabled.");
} else {
  document.write("java not available.");
}

if (navigator.language.indexOf("en") > -1) {
  document.write("language ins english");
}else if (navigator.language.indexOf("de") > -1) {
  document.write("language is german");
}

</script>

</body></html>

reformat unsigned lists (bullet lists)

// unsigned list look the same in firefox and internet explore
// but firefox uses padding and ie uses margin to indent
// so if you want to eliminate the indent you mat set both

// here the top margin is negative too
// so the bullet list is written directly under the text
// and the bottom-margin of the p-tag can remain (needet elsewhere)
ul {
	padding-left: 15px;
	margin-left: 0px;
	margin-top: -10px;
	margin-bottom: 10px;
}
« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS