Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Browser-independent extensible mouse hover help


  • Please log in to reply
10 replies to this topic
keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004
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:

Posted Image


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:

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.

#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.

supergrass
  • Members
  • 51 posts
  • Last active: Oct 15 2013 01:01 PM
  • Joined: 21 Feb 2007
I get this error:
Posted Image


Also, do I need Sean's COM function library?

Rdgs,

Supergrass

keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004
I forgot to mention it. You need COM.ahk and IE.ahk from here:

<!-- m -->http://www.autohotke...topic19225.html<!-- m -->

Drugwash
  • Members
  • 1064 posts
  • Last active: Feb 01 2014 11:57 PM
  • Joined: 07 Sep 2008
Hmmm... might be useful sometime... thanks. Looks similar in concept with WordPress' gimmick. ;)

I do have a couple questions though (pardon my noobness on the matter):
- how to actually install the javascript part (I'm using SlimBrowser, based on IE)
- anything else needs changed for browser detection, apart from
if (class <> "OpWindow" and class <> "MozillaUIWindowClass"


keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004

Hmmm... might be useful sometime... thanks. Looks similar in concept with WordPress' gimmick. ;)


What do you mean? Is there a similar thing for wordpress? I tried to search for an existing solution, before starting to write this.

I do have a couple questions though (pardon my noobness on the matter):
- how to actually install the javascript part (I'm using SlimBrowser, based on IE)


Add a new bookmark and copy the javascript to the link field of the bookmark.

- anything else needs changed for browser detection, apart from

if (class <> "OpWindow" and class <> "MozillaUIWindowClass"


Nope, that's all as far as the Autohotkey code is concerned. The trickier part is the javascript code, because it was not tested on IE, so it may not work out of the box.

Drugwash
  • Members
  • 1064 posts
  • Last active: Feb 01 2014 11:57 PM
  • Joined: 07 Sep 2008

What do you mean? Is there a similar thing for wordpress? I tried to search for an existing solution, before starting to write this.

Not exactly a dictionary thing, but a small preview window of another page (snap-view or something, forgot the exact name).

Add a new bookmark and copy the javascript to the link field of the bookmark.

Bookmark... there's no such item in my browser. Do you mean 'Favorites'? If so, I've tried that and doesn't work. Otherwise... dunno how to do it. There's a 'Run own script' option in SB with javascript/VB selection and I tried to paste the js there and run it but nothing happened; what exactly am I supposed to get/see after running the script?


- anything else needs changed for browser detection, apart from

if (class <> "OpWindow" and class <> "MozillaUIWindowClass"


Nope, that's all as far as the Autohotkey code is concerned. The trickier part is the javascript code, because it was not tested on IE, so it may or may not work out of the box.

Got that sorted out, I think. SlimBrowser window class is Internet Explorer_Server7.

Thanks for your help so far.

keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004

Bookmark... there's no such item in my browser. Do you mean 'Favorites'? If so, I've tried that and doesn't work. Otherwise... dunno how to do it. There's a 'Run own script' option in SB with javascript/VB selection and I tried to paste the js there and run it but nothing happened; what exactly am I supposed to get/see after running the script?


Yes, it's called Favorites on IE.

If hover help is activated then you should see the current word the mouse points to in the window title. See the browser title bar on the screencast.

If you have Firefox installed then you should try to install it on Firefox first, so that you have it working on your system. If it works properly there (as it should) then you can try IE again.

If it does not work on IE then we have to wait for someone who is knowledgeable with IE Javascript to sort it out.

Drugwash
  • Members
  • 1064 posts
  • Last active: Feb 01 2014 11:57 PM
  • Joined: 07 Sep 2008
Guess it doesn't work - no word in window title. :(

I tried to convert that video to something more decent than 77MB but failed. My player picks up a bad codec (GTCODEC.DLL) for such video types and crashes. VLC only shows a gray screen. :?

The only browser I use is SB, don't have others installed; none of them does what I need to and recent versions don't work on my 98SE system anymore. So can't test in FF.

Anyway, I though js is the same no matter the browser type... maybe I'm wrong; as I said, the web stuff kinda passed me by.

EDIT: Oh, I forgot to mention: I'm using dual-view and browser window is on secondary monitor... if that matters. I see there's a screen detection there but not multi-monitor aware, maybe it has something to do with all this.

EDIT2:] I'm finding quite a few issues while debugging through the script. Not all pertaining to it though.

First one would be that A_TimeIdle (and A_TimeIdlePhysical, for that matter) won't work in any OS below Win2000. I've recently had a hard time with it in another script - nothing to do without a lot of work and jumping through hoops. :(

Then, SB window class appears to be different than what AUSpy reported. Dunno the circumstances, but it's SlimBrowser MainFrame while the script is running. Maybe it's getting the main window, not the currently active tab.

Next thing is I'm not quite sure what this RegEx is searching for (RegEx is another flaw of mine); could you please elaborate a bit:
pos := RegExMatch(title, ".*\[(.+)\].*", match)
If it's searching for some text between square brackets, it's not gonna find any, coz SB doesn't do that. Titlebar text is: SlimBrowser - Current tab's title
Same goes for this one :oops: :
pos := RegExMatch(word, "^FIXME")

Anyway, after commenting out the two RegEx and fixing whatever I mentioned above, I could see the bluish GUI with the Wikipedia/Google page inside (according to selection). So there's still hope for me. :)

I hope the above will in some way be useful to you if you wanna enhance the script. I'll try to find some workaround for my 9x issues after I get details on those RegEx. ;)

keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004

If it's searching for some text between square brackets, it's not gonna find any, coz SB doesn't do that.


Yep, that's the problem.

The javascript code puts the current word to the end of the browser title like this: [word]

That's where the autohotkey gui is picking up the word to be searched for with the regex. So commenting out the regex kind of defeats this purpose. :)

So the problem is the javascript part doesn't work correctly in IE. Javascript has subtle differences between browsers, that's why someone with IE Javascript knowledge is needed to fix it.

keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004
BTW, if someone can get it to work for himself/herself: the three sources (wikipedia, etc.) provided in the script are just examples.

You can add any other lookup you want pretty easily. See the add_source function and the examples in the script.

Drugwash
  • Members
  • 1064 posts
  • Last active: Feb 01 2014 11:57 PM
  • Joined: 07 Sep 2008

The javascript code puts the current word to the end of the browser title like this: [word]
That's where the autohotkey gui is picking up the word to be searched for with the regex.

Ah, I understand now. Thanks for the explanation, makes perfect sense.

I wonder, could JS be failing because I removed MS Java Virtual Machine from my system? I do have Sun Java 1.6.0 u7 installed though...

Can the same function be done with VB? I can run custom VB scripts in SlimBrowser but dunno VB at all. :oops:

[EDIT] After searching around, I found out setAttribute, appendChild and other syntax used in hover.js (as well as in the starter script) are not supported by IE. Looks like a dead end for me. :(