Tuesday, July 27, 2010

Debugbar, Autohotkey, Javascript

Great discoveries this past week. First of all, Debugbar is a great plugin for Internet Explorer for examining the DOM of websites, especially those that have complex dynamically created DOM with javascript. Also you can run javascript in the Debugbar console on the website without resorting to "navigating" out of the URL (you don't have to type the script into the URL input field, or navigate out of the native URL using links or favorites), which makes running these scripts possible when you have Web Controls/Agents on your source URL.

You can access the data within a frame in the DOM using the following javascript syntax:

str = top.myframename.document.GetElementById('myelementid').innerHTML;

and then use javascript REGULAR EXPRESSIONS (Regex) functions to parse the data into usable formats

Autohotkey is capable of launching javascript, but it is trickey. You have to either use the URL input field or Debugbar Javascript Console to do it, neither of which is elegant, or you have to use the COM library which is documented on the Autohotkey forum, again, which will require a learning curve. A better solution is to use Autohotkey to reliably click the correct objects in the browser, and the two tricks I have learned are:

CoordMode, Mouse, Screen

That line allows you to accurately base Mouse Movements and Click Coordinates to be relative to the screen instead of to the window, which helps when the script is popping up windows all over the place. The other trick is

Sleep, 100

Between each Click, it's good to pause the program for 1/10th of a second to let the computer catch up with itself. Prevents errors.

Monday, July 5, 2010

PHP Script accept query string data and place onto PDF form

require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('../pdfdoc\MedSolMRICTheadneck.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 200);

$pdf->SetFont('Arial','B',10);
$pdf->SetTextColor(0,0,255);
$pdf->SetXY(56, 50);
$pdf->Write(0, $_GET['lastname'].'---'.$_GET['firstname'].'---'.$_GET['dob']);

$pdf->Output();
?>

Notes:
-The $_GET[] array names are in quotes to make WAMP happy and match the query string names the the javascript bookmarklet that sent the query
-The main work in writing this is finding the XY coordinates in the third to last line
-Copy and paste the 4 line block for each data entry point on the form, each with its own XY coordinates
-Note the color 0,0,255 (blue). Other useful colors: 0,0,0 - black and 255,0,0 - red.
-Note the 'B' parameter in font for bold: helpful when filling out forms.
-Note the source file path with the backslashes again to make WAMP happy.

So, big picture: You've invoked a bookmarklet to navigate from a website with data to a PHP script on WAMP which pulls up a PDF document in your localhost directory tree and inserts the data on the form. Now you run your AutoHotKey script to print or fax it.

AutoHotKey Script for Emailing a PDF File

:*:][::

Send {Click 1228,100}
Send {Click 1145,254}
SetKeyDelay,700,100
Send !a
SetKeyDelay,50,100
Send doc@scanr.com
Send {Tab}{Tab}{tab}^a
SendRaw +18005551212
Send !s

return

The keystrokes ][ trigger two mouseclicks to pull "send page" from EI7, followed by keystrokes that send the attachment to an online faxing service.

(Note: the asterisk near the beginning causes the script to execute immediately with the ][ trigger, without a following spacebar. Sometimes, you need to precede the execution with a spacebar keystroke to "clear" previous keystrokes.)

Javascript to fetch and submit data

This script will fetch data from the input fields of a web site and submit the data as a query string to a URL of your choosing:

javascript:
var lastname_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtLName');
var lastname = lastname_elem.value;
alert(lastname);

var firstname_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtFName');
var firstname = firstname_elem.value;
alert(firstname);

var dob_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtDOB');
var dob = dob_elem.value;
alert(dob);

open('http://example.com?lastname='+lastname+
'&firstname='+firstname+
'&dob='+dob,
'_self',
'resizable,location,menubar,toolbar,scrollbars,status');

You just need to find out the element id for the input fields. This example pops the data from the input fields into alert text boxes before submitting the get request to the next web site, for debugging purposes.

Sunday, July 4, 2010

More on MAMP to WAMP migration - fpdf Scripts

Not sure if this is a MAMP to WAMP issue or a PHP 5.2 to 5.3 issue, since both changed at the same time but here is what I found while migrating fpdf scripts:

1. MAMP likes the &= syntax. Change to = in WAMP. If you don't, you will get a lengthy error message.
2. If you open a pdf file and view it in the Internet Explorer Browser, and get a whole lot of gibberish characters, if you close your browser and reopen it and try again, the pdf will open normally. It either has something to do with cacheing or with IE security settings in tying browser objects to Adobe Reader, a third-party application. I have also read that it is a IE6 bug, although it happened to me in IE7. If it persists, perhaps switching to firefox would help, I never did that experiment.