~ Magic Tool Lore ~
         to basic   

(¯`·.¸  There is never just 'one' way  ¸.·´¯)
Learning a browser new tricks
- 'Sit', 'roll over' and 'play dead' -

Published @ Searchlores in January 2004 | Version 0.01 | By Cinix


A True ~S~ toolbox is full of those 'independant' little, but very usefull tools. Wouldn't it be great to have them all work together, and obey your every command?

 Contents

 Introduction
 Copy & Paste... Yeh right!

Copying and pasting, or for that matter re-typing, an URL in your favorite 'Seekers Magic Tool' like wget or the latest Sourcerer from Mordred,...We've all done it, to great extent in our quests for knowledge, and tought nothing off it but Mordred really ticked me off by saying: Now open the same URL in Sourcerer (copy & paste the URL - that's the only way which is anyway the fastest, so get used to it). I don't want to get used to it, I want my tools to obey my every command and preferably without much efford.


 Approach

A ~S~ most important tool is without a doubt his browser, nearly all other tools are used in order to complement it. So we will try to optimize the way we use (or abuse) our browser by teaching it some new tricks.

We could start by reversing the guts out of it but this hardly the most optimal way. For one this method isn't browser independant, isn't easy to maintain and adapt for new tools and not all of us are fluently speaking machinecode.

The main way we 'actually' communicate with our browser, is when we use the 'addressbar'. Trough there we can tell it everything, but like an old dog it only responds to the tricks it already knows. Imagine what could happen if we used something like the following code as a bookmarklet to grab data from a page (in this case the current url), convert it into a command and pass it along the address bar for the browser to obey.

(I'll use Mordred's Sourcerer as an example trougout the essay)[0]

<script language="javascript">
   new_trick = "fetch " + location.href + " and open with sourcerer";
   location.href = new_trick;
</script>

Of cource a command like: "fetch http://www.searchlores.org/cinix_run.htm and open with sourcerer" is a bit on the demanding side, but if we base our new trick on some of the older tricks our browser already knows, I'm sure we will find a workable solution.

There is only one main rule: the tool we wish to command from our browser needs to support commandline arguments.[1]


 Solution 1
 A custom protocol handler

I'm sure you all have seen links starting with sig2dat://, ed2k://, copernic://, ... these are all custom protocol handlers installed together with applications that wander the net: desktop based search engines, P2P appz,... We could in fact create our own custom protocol handler to launch an application like sourcerer.[2]

<script language="javascript">
   new_trick = "sourcerer://" + location.href;
   location.href = new_trick;
</script>

Or as a [Bookmarklet]

Implementing a custom protocol handler in the latest Opera versions for windows is exactly like the Microsoft IE appoach. Add a registry key with all needed data.

   Windows Registry Editor Version 5.00

   [HKEY_CLASSES_ROOT\sourcerer]
   @="URL:Sourcerer Protocol"
   "URL Protocol"=""
   "EditFlags"=dword:00000002

   [HKEY_CLASSES_ROOT\sourcerer\DefaultIcon]
   @="C:\\path_to_handler\\handler.exe"

   [HKEY_CLASSES_ROOT\sourcerer\shell]

   [HKEY_CLASSES_ROOT\sourcerer\shell\open]

   [HKEY_CLASSES_ROOT\sourcerer\shell\open\command]
   @="C:\\path_to_handler\\handler.exe \"%1\""

In order to startup sourcerer we also need to write a protocol handler program (here handler.exe) that will do the following:

Download the code for: Solution 1 (Visual Basic 6 Source Code)

Or we could rewrite sourcerer to do this all since we have the source code, but that is not always the case.

Problems with this solution


 Solution 2
 Local webserver: Apache - PHP

Running a web server locally is in my opinion a better way to go. You can start it up (when you need it) and shut it down (when you don't) without much effort and is easy to adapt, expand and customize.

<script language="javascript">
   new_trick = "http://localhost:80/run.php?app=sourcerer&cmdln=" + location.href;
   location.href = new_trick;
</script>

Or as a [Bookmarklet]

Now how would our run.php code then look like?

<?php 
   if(isset($_GET["app"]) && $_GET["app"]=="sourcerer"){
      $exe = "start /D C:\path_to_sourcerer\sourcerer.exe";
      if(isset($_GET["cmdln"]){
         $exe .= $_GET["cmdln"];
      }
      exec($exe);
      
      // return an answer
      // just instruct the browser to go back to the page we used the bookmarklet on.
      echo ("<html>");
      echo ("<script language='javascript'>");
      echo ("history.go(-1);");
      echo ("</script>");
      echo ("</html>");
   }
   else{
      // show an error message
      echo ("error running application");
   }
?>

Problems with this solution


 Solution 3
 Local webserver: A custom application

The third solution is somewhat a mix between the first and the second and is my personal favorite. It is basically the hander with an internal webserver so we don't need to start messing in the registry, it will support all types of browsers and we don't need to install a full blown webserver with added php functionality.

<script language="javascript">
   new_trick = "http://localhost:81/sourcerer/" + location.href;
   
   // or for the more demanding amongst you, it now is somewhat possible :)
   // new_trick = "http://localhost:81/fetch " + location.href + " and open with sourcerer";
   
   location.href = new_trick;
</script>

Or as a [Bookmarklet]

In order to startup sourcerer, our program will do the following:

Download the code for: Solution 3 (Visual Basic 6 Source Code)

Problems with this solution


 Notes

[0]: It is only fair to say I adapted Mordred's Sourcerer in such a way that a commandline like sourcerer.exe http://page_you_want_to_open/ also works so there is no need to include a filename where the source needs to be saved.

[1],[3]: As you might know: all rules are bendable. For a non commandline enabled tool in a graphical OS, we can always create a wrapper tool that takes the commandline arguments, starts up the tool we need and sends the application the appropriate low level system messages like: "fill in this text in that textbox", "that button clicked"...

[2]: In fact there are some run:// protocol handler programs out there but some caution must be executed since they allow all programs to execute, and the protocol handler is known with those that might lead you to obsure, javascript bug infested web pages.


 Contributions
 and paths you may want to explore

The content of this essay covers only some ideas I had with some proof of concept code. This is by far complete, polished or fully bugfree functional code. Feel free to use or abuse as you see fit and build upon the base I have provided here, but remember to share. If you for example created a search bot with a webinterface, a wrapper tool,... or if you adapted and improved the code here or even just ported it to another language (C, Java, ...)

Remarks, suggestions or contributions: just stick them on the ~ S ~ messageboard. I'm sure some interesting discussions and or ideas can come to life there.


Cinix


Petit image

(c) III Millennium: [fravia+], all rights reserved, reversed, reviled and revealed