How to paste, click and copy parts of webpage with ComObject

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Benny-D
Posts: 259
Joined: 12 Mar 2014, 10:09

How to paste, click and copy parts of webpage with ComObject

02 Jul 2014, 16:43

Let's say I have a list of nouns

List =
(
cat
lion
cheetah
tiger
)


that I want to translate into another language using Google translate page:Image

The following steps need to be done:

1) pasting a noun into the left field of the page;
2) clicking on "Translate" button;
3) copying the translated noun from the right field;

Of course, I could do all of that using Send and Click commands, but I wonder if it's possible to be done using ComObject.

I am not asking to write a complete script for me, but could you, please, direct me as to what I should start with here. I know almost nothing about ComObject.

In case it matters, I am using FireFox.
FanaticGuru
Posts: 1361
Joined: 30 Sep 2013, 22:25

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 17:24

Benny-D wrote: In case it matters, I am using FireFox.
It matters a lot. COM is a Microsoft thing and is not supported by most non-Microsoft software which means you would have to be willing to use Internet Explorer for this. "Use" is a loose term though, Internet Explorer can be used through COM with out ever being visible to the user.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
joedf
Posts: 7339
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 17:37

Interesting idea.. Apparently, the google translate API is not free.

Here's a start.. working on it right now :)

Code: Select all

;google translate
#SingleInstance, off
PID_Self := DllCall("GetCurrentProcessId")
GroupAdd, Self_ID, ahk_pid %PID_Self%

SourceLang := "auto" ;country code of the source language
DestLang := "de" ;country code of the destination language
Word := "pomme" ;word to be translated

GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Word

Gui Add, ActiveX, w980 h640 vWB, Shell.Explorer  ; The final parameter is the name of the ActiveX component.
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
WB.Navigate(GoogleTranslateURL)  ; This is specific to the web browser control.
Gui Show
return

GuiClose:
ExitApp

class WB_events
{
    NavigateComplete2(wb, NewURL)
    {
        ToolTip %NewURL%
    }
}

#IfWinActive ahk_group Self_ID
^h::
	MsgBox % WB.document.getElementById("result_box").innerHTML
return
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
joedf
Posts: 7339
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 18:18

small Update:

Code: Select all

;google translate
#SingleInstance, off

SourceLang := "auto" ;country code of the source language
DestLang := "de" ;country code of the destination language
Words := "Je mange une pomme rouge. J'aime bien le chocolat au lait." ;words to be translated

GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Words
;StringReplace,GoogleTranslateURL,GoogleTranslateURL,%A_Space%,`%20,All

Gui Add, Edit, w200 h150 vResultBox, Loading...
Gui Add, ActiveX, x0 y0 w0 h0 vWB, Shell.Explorer  ; The final parameter is the name of the ActiveX component.
Gui, Add, StatusBar,, Loading...
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
WB.Navigate(GoogleTranslateURL)  ; This is specific to the web browser control.
Gui Show
return

GuiClose:
ExitApp

class WB_events
{
	;NavigateComplete2(wb, NewURL)
	;DownloadComplete(wb, NewURL)
	DocumentComplete(wb, NewURL)
	{
		if (WB.ReadyState == 4) { ; see http://stackoverflow.com/a/9835755/883015
			t_s:=A_TickCount
			while (StrLen(WB.document.getElementById("result_box").innerHTML)==0) {
				;wait
			}
			SB_SetText("Done. JS Loaded in " (A_TickCount-t_s) " ms.")
			GuiControl,,ResultBox, % WB.document.getElementById("result_box").innerHTML
		}
		return
	}
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
FanaticGuru
Posts: 1361
Joined: 30 Sep 2013, 22:25

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 18:23

Here is my version that works for me:

Code: Select all

IE := ComObjCreate("InternetExplorer.Application")
;~ IE.Visible := true
IE.Navigate("translate.google.com")

While IE.readyState!=4 || IE.document.readyState!="complete" || IE.busy
		Sleep 50

Word := "Hello"
DetectHiddenWindows, On
ControlSend, Internet Explorer_Server1, %Word%, ahk_class IEFrame

While !(StrLen(IE.document.all.result_box.innertext))
		Sleep 50

Result := IE.document.all.result_box.innertext
IE.Quit

MsgBox % Result
joedf's way of sending the information via the address is probably better than sending it into the input box as that allows control of input and output languages.

We are both basically getting the information back out the same way with a little different syntax. It is good to see two different ways you can get innertext.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
joedf
Posts: 7339
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 19:32

Ok updated :D
Full blown GUI app

Code: Select all

;google translate - test  joedf - 2014/07/02
#SingleInstance, off

Gui Add, Text, x6 w40, Input:
Gui Add, ComboBox, yp+1 x+4 w80 vCBSource, auto||en|de|fr|es
Gui Add, Edit, w300 h80 vInputBox,
Gui Add, Text, x6 w40, Result:
Gui Add, ComboBox, yp x+4 w80 vCBDest, en||de|fr|es
Gui Add, Edit, w300 h100 vResultBox,
Gui Add, Button, gBtnTranslate vBtnTranslate +Default, Translate
Gui Add, ActiveX, x0 y0 w0 h0 vWB, Shell.Explorer  ; The final parameter is the name of the ActiveX component.
WB.silent := true ;Surpress JS Error boxes
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
Gui, Add, StatusBar,, Idle.
Gui Show
return

GuiClose:
ExitApp

BtnTranslate:
	GuiControlGet,Words,,InputBox
	if Words is Space
		return
	
	GuiControl,Disable,BtnTranslate
	GuiControlGet,SourceLang,,CBSource
	GuiControlGet,DestLang,,CBDest
	
	if SourceLang is Space
		GuiControl,ChooseString,CBSource, % SourceLang:="auto"
	if DestLang is Space
		GuiControl,ChooseString,CBDest, % DestLang:="en"
	
	/* SourceLang := "auto" ;country code of the source language
	 * DestLang := "de" ;country code of the destination language
	 * Words := "Je mange une pomme rouge. J'aime bien le chocolat au lait." ;words to be translated
	 */

	GoogleTranslateURL := "http://translate.google.com/#" SourceLang "/" DestLang "/" Words
	;StringReplace,GoogleTranslateURL,GoogleTranslateURL,%A_Space%,`%20,All
	
	SB_SetText("Loading...")
	WB.Navigate(GoogleTranslateURL)  ; This is specific to the web browser control.
return

class WB_events
{
	;NavigateComplete2(wb, NewURL)
	;DownloadComplete(wb, NewURL)
	DocumentComplete(wb, NewURL)
	{
		if (WB.ReadyState == 4) { ; see http://stackoverflow.com/a/9835755/883015
			SB_SetText("Downloaded. Loading JS...")
			t_s:=A_TickCount
			while (StrLen(WB.document.getElementById("result_box").innerHTML)==0) {
				;wait
			}
			t_e:=A_TickCount
			;Quick-Remove Html Tags
			data:=RemoveHtmlTags(WB.document.getElementById("result_box").innerHTML)
			GuiControl,,ResultBox, % data
			ResultLang:=GetGoogleTranslateLanguage(WB.document.getElementById("gt-sl-sugg").innerHTML)
			SB_SetText("Done. JS Loaded in " (t_e-t_s) " ms. Input Language: " ResultLang)
			GuiControl,Enable,BtnTranslate
		}
		return
	}
}

RemoveHtmlTags(data) {
	return RegExReplace(data,"<(\/)?[^<>]*(\/)?>")
}

GetGoogleTranslateLanguage(langbar_data) {
	langs:=StrSplit(langbar_data,"</div>")
	Main_lang:="Unknown"
	for each, lang in langs
		if InStr(lang,"aria-pressed=""true""")
			Main_lang:=Trim(RemoveHtmlTags(lang))
	if k:=InStr(Main_lang," - ") {
		StringLeft,Main_lang,Main_lang,%k%
		Main_lang := Trim(Main_lang) " [Detected]"
	}
	return Main_lang
}
Image
EDIT: FanaticGuru! cool, hmm lol... i have a size 0px control... :P I forgot about IE := ComObjCreate("InternetExplorer.Application") :P
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
Benny-D
Posts: 259
Joined: 12 Mar 2014, 10:09

Re: How to paste, click and copy parts of webpage with ComOb

02 Jul 2014, 21:09

Oh my God!!! FanaticGuru and joedf, you guys are just too generous! Thank you so much!
I am studying your scripts now.
clina1j
Posts: 49
Joined: 22 Apr 2016, 18:39

Re: How to paste, click and copy parts of webpage with ComObject

25 Apr 2016, 13:44

I know that I am coming at this a few years later, but I am trying to use the code as a template for what I wish to do. When I copied all of the text that Jodef posted, and saved it as GoogleTranslate.ahk, when I tried to run it I got the following error message:

Error: Call to nonexistent function.

Specifically: StrSplit(langbar_data, "</div>")

Then it references line 76, and states that the program will exit.
I assume that something changed since the first version was released. Could anyone tell me what it is?
clina1j
Posts: 49
Joined: 22 Apr 2016, 18:39

Re: How to paste, click and copy parts of webpage with ComObject

25 Apr 2016, 13:50

Never Mind. I replaced the content of line 76 with. Somehow, that did the trick

StringSplit, langs, langbar_data, "</div>"
gregster
Posts: 3379
Joined: 30 Sep 2013, 06:48

Re: How to paste, click and copy parts of webpage with ComObject

25 Apr 2016, 13:58

clina1j wrote:Never Mind. I replaced the content of line 76 with. Somehow, that did the trick

StringSplit, langs, langbar_data, "</div>"

The function Strsplit was introduced in AHK version 1.1.13 ...
clina1j
Posts: 49
Joined: 22 Apr 2016, 18:39

Re: How to paste, click and copy parts of webpage with ComObject

25 Apr 2016, 15:41

That would do it. I have only AutoHotKey 1.1.09.04. (Stupid Company App Store with out of date software ...)

Return to “Ask For Help”

Who is online

Users browsing this forum: cainxabel, KHA, MIRKOSOFT, tmplinshi and 199 guests