This script is a proof of concept for convenient lookup of words on webpages. I designed it for dictionary lookup when you need to look up several words on the same page, but it can be used for other purposes as the sources in the script demonstrate (and you can add new sources easily).
The main design principle was convenience, so that I don't even have to click on words, only hover above them with the mouse to get their definition. If you select some text then the selected text is used for lookup. When the definition appears it stays open while you keep the mouse cursor within the window and disappears instantly otherwise, so you don't even have to close it.
Here's a screencastof it in action.
Here's a screenshot:
The solution has a javascript part and an autohotkey part.
For the javascript part you need to install this bookmarklet which toggles hover help on a page if you want it:
Code:
javascript:void(z=document.body.appendChild(document.createElement('script')));void(z.language='javascript');void(z.type='text/javascript');void(z.src='http://www.autohotkey.net/~keyboardfreak/hover.js');void(z.id='hoverhelp');
Note that you can also enable hover help permanently for all pages using user javascript. It's a matter of taste.
And here's the Autohotkey part. You'll also need COM.ahk and IE.ahk
from here.
Code:
#NoEnv
delay_to_appear = 500
delay_to_hide = 10
gui_width = 500
gui_height = 300
browser_margin = 5
mouse_overlap = 5
;----------------------------------------------------------------------
SysGet, Screen, Monitor
gui_id =
gui_visible := false
gui_title := "dummy title because the windows is not found without it for some reason"
sources_num = 0
current_source = 1
add_source("Wikipedia", "http://en.wikipedia.org/wiki/Special:Search?search=", "document.getElementById('column-one').style.display='none';document.getElementById('content').style.marginLeft=0")
add_source("Google Define", "http://www.google.com/search?ie=utf-8&oe=utf-8&hl=en&q=define:", "document.getElementById('header').style.display='none';document.getElementById('tsf').style.display='none';document.getElementById('ssb').style.display='none'")
add_source("IMDb", "http://www.imdb.com/find?s=all&q=", "document.getElementById('nb15').style.display='none'")
add_source("Leo.de", "http://dict.leo.org/ende?lp=ende&lang=de&searchLoc=0&cmpType=relaxed&search=", "document.getElementById('mainnavigation').style.display='none';document.getElementById('searchrow').style.display='none';document.getElementById('header').style.display='none';if(document.getElementById('subnavigation')){document.getElementById('subnavigation').style.display='none';}var tds=document.getElementsByTagName('td');for (var i = 0; i < tds.length; i++)if(tds[i].className == 'sidebar') tds[i].style.display='none'")
coordmode, mouse, screen
Gui, +LastFound -Caption -SysMenu +Owner
Gui, Color, 66CCFF
Gui, Add, Text, section, Text:
Gui, Add, Edit, ys vText gTextChanged
list =
loop %sources_num%
{
name := sources%a_index%_name
if (list == "")
list = %name%
else
list = %list%|%name%
}
Gui, Add, DropDownList, ys Choose%current_source% vSource gSourceChanged, %list%
set_delay(delay_to_appear)
checkword:
if (a_timeidle < delay)
return
if (gui_visible)
{
mousegetpos x, y
if (x < gui_x or x > (gui_x + gui_width) or y < gui_y or y > (gui_y + gui_height))
{
gui, hide
gui_visible := false
set_delay(delay_to_appear)
SetTimer wait_for_page_load, off
}
}
Else
{
WinGetClass class, A
if (class <> "OpWindow" and class <> "MozillaUIWindowClass")
return
WinGetTitle title, A
pos := RegExMatch(title, "^\{([^}]+)\}.*", match)
if (pos == 0)
return
word := match1
pos := RegExMatch(word, "^FIXME")
if (pos > 0)
return
mousegetpos x, y
gui_x := x - mouse_overlap
if (gui_x + gui_width > ScreenRight)
gui_x := x - gui_width + mouse_overlap
gui_y := y - gui_height + mouse_overlap
if (gui_y < ScreenTop)
gui_y := y - mouse_overlap
Gui, Show, x%gui_x% y%gui_y% w%gui_width% h%gui_height% , %gui_title%
guicontrol,,Text, %word%
guicontrol focus, Text
sendinput {end}
;GuiControl, Choose, Source, 1
guicontrol focus, Source
;current_source = 1
if (gui_id == "")
{
top_margin = 30
IE_Init()
gui_id := WinExist(gui_title)
pweb := IE_Add(gui_id, browser_margin, browser_margin + top_margin, gui_width - (2 * browser_margin), gui_height - (2 * browser_margin) - top_margin)
}
gui_visible := true
set_delay(delay_to_hide)
load_url()
}
return
load_url()
{
global
IE_LoadURL(pweb, sources%current_source%_url . word)
if (sources%current_source%_pagefix <> "")
SetTimer wait_for_page_load, 100
else
SetTimer wait_for_page_load, off
}
textchanged:
if (not gui_visible)
return
settimer textchanged_idle, 500
return
textchanged_idle:
settimer textchanged_idle, off
guicontrolget word,,text
if (word <> "")
load_url()
Return
wait_for_page_load:
if (IE_ReadyState(pweb) == 4)
{
settimer wait_for_page_load, off
COM_Invoke(pweb, "document.parentwindow.execscript", sources%current_source%_pagefix)
}
return
GuiClose:
COM_Release(pweb)
Gui, Destroy
IE_Term()
return
set_delay(new_delay)
{
global delay
delay := new_delay
settimer checkword, %delay%
}
SourceChanged:
SetTimer sourcechanged_idle, 500
return
SourceChanged_idle:
SetTimer sourcechanged_idle, off
if (not gui_visible)
return
GuiControlGet source_name,,source
loop %sources_num%
{
if (source_name == sources%a_index%_name)
{
current_source := a_index
break
}
}
load_url()
Return
add_source(name, url, pagefix = "")
{
global
sources_num++
sources%sources_num%_name := name
sources%sources_num%_url := url
sources%sources_num%_pagefix := pagefix
}
Note that it's proof of concept, so here are some warnings:
The javascript code could use some more testing. It worked on the pages I tried, but there can be cases which it cannot handle well.
The javascript part was only tested on Firefox and Opera. I haven't used any special code, so it shouldn't be hard to make it work on IE.
The global variables in the javascript don't have a unique prefix, so they might clash with some other variable on the page which could prevent the script from working. This shouldn't be hard to fix either, but I'm lazy to do it.
The autohotkey script is not nicely documented and formatted. Again laziness.