Jump to content

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

Basic Webpage Controls with JavaScript / COM - Tutorial


  • Please log in to reply
335 replies to this topic
rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
I know with CSS you can add !important to the code to make it important, but I didn't realize that you couldn't do that in the Javascript implementation. I did some searching and found that I can use setAttribute to do what I want but I'm not quite sure how to rewrite it with COM_Invoke.
element.setAttribute('style', 'display:inline !important');
I tried a couple of things, but none of them seem to work. Also when I use COM_Invoke(pwb, "Navigate", "javascript: document.getElementById('gsliteswf').previous();") I get a message that says, "Are you sure you want to navigate away from this page?." It seems like that code is changing the address instead of working on the DOM, which is what I intended. Ideas? Thanks.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
tank would know more about this stuff that I would ( I've never seen !important before ), but you already touched on something that should work. Try:
url := "[color=olive]javascript: document.all['gsliteswf'].setAttribute('style', 'display:inline !important')[/color]"

COM_Invoke(pwb, "Navigate", url)


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
this is the first i have ever heard of such but as i look into it it seems hit and miss to be supported in IE therfor whats the point?

I suppose the javascript method is the best way to do it. IMO styles at the tag always take precidence over stylesheet any how so again what really is the point

and finally I am not going to spend any more time on such an obscure and unreliable CSS feature
Never lose.
WIN or LEARN.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
jethrow thanks for the idea, but it doesn't seem to work. I am a web designer and !important is neither obscure nor unreliable so I am not sure where you got your information. It works perfectly fine in IE7 (with a doctype) and IE8. If you are interested in learning something, tank, everything you need to know about !important can be found at http://www.electrict...-important-css/. Thanks anyway, but I guess I'll have to figure this out on my own.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
i had previously seen that link which plainly states the pains of using IE with it. If your doing data mining with ahk over pages you control you would be better suited to use other than ahk otherwise you better not count on pages having a proper doctype declaration. But whatever knock yourself out with that one and go ahead and beleive its not obscure. I have been a web designer for years now and never heard of it till today and cant imagine a practical use for it
Never lose.
WIN or LEARN.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
Hey Tank... sorry if "pissed you off I have" :-) I figured out a way around needing !important. I have been working on embedding Grooveshark inside an AHK app. Grooveshark, http://www.grooveshark.com, is an online music player. The app has Play/Pause, Next, and Previous buttons in a systray menu which allows Grooveshark to be controlled without being open. As of now everything is fully functional but I get a message saying "Are you sure you want to navigate away from this page?" The message only pops up when I click next or previous in the systray menu. The code being used is as follows and the same thing happens in IE as well as the AHK COM app.

BROWSER - javascript: function playNext(){document.getElementById('gsliteswf').next();}playNext();

AHK COM - COM_Invoke(pwb, "Navigate", "javascript: function playNext(){document.getElementById('gsliteswf').next();}playNext();")
Am I doing something wrong and is there any way around this message? Thanks.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

The message only pops up when I click next or previous in the systray menu. The code being used is as follows and the same thing happens in IE as well as the AHK COM app.

So, do you still get this message when you use javascript through the url address bar? If so, keep in mind that this is no longer truly an AHK question, but rather a web-development question. If that is the case, this forum isn't necessarily the best place to ask ( though some members may possibly sill answer your question ).

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Why are you using Navigate for this purpose? Are you unable to use execScript for some reason?

COM_Invoke(pwb, "execScript", "javascript: function playNext(){document.getElementById('gsliteswf').next();}playNext();")


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007

Why are you using Navigate for this purpose? Are you unable to use execScript for some reason?

COM_Invoke(pwb, "execScript", "[color=red]javascript: [/color]function playNext(){document.getElementById('gsliteswf').next();}playNext();")

take the red out

as far as the use in navigation best to add the following
javascript: function playNext(){document.getElementById('gsliteswf').next();}playNext();[color=red]void(0);[/color]

Never lose.
WIN or LEARN.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
jethrow this problem does occur in the browser address bar as well. It seems like it is in the code somewhere, but I tried searching the JS code and couldn't find anything. Could this be part of JQuery or something? If so would there be a way around it?

sinkfaze/tank thanks for the execScript code. I didn't know about that so I modified the JS alert example at the beginning of this thread. However, this doesn't work and I receive an error saying:

COM Error Notification
Function Name: "execScript"
ERROR: Unkown name.
(0x80020006)
COM_Invoke(pwb, "execScript", "javascript: function playNext(){document.getElementById('gsliteswf').next();}playNext();")
Above is the code that triggers the error. For the record, I have IE8 installed and the Grooveshark website runs in IE8 Standards Mode. Any ideas?

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

COM Error Notification
Function Name: "execScript"

That's because execScript() is a method of the window object, not the web browser object. Try:
COM_Invoke(pwb, "[color=red]document.parentWindow.[/color]execScript", "function playNext(){document.getElementById('gsliteswf').next();}playNext();")
That should fix the COM Error, but I'm not sure if this will solve your problem.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
cant beleive i missed te execsxript problem putting the crack down now :wink:
Never lose.
WIN or LEARN.

rhinox202
  • Members
  • 37 posts
  • Last active: Sep 18 2014 07:19 PM
  • Joined: 23 Apr 2009
Thanks jethrow, that worked perfectly. I had narrowed it down to an "onbeforeunload" event and was working on detaching the event, but this works... so I don't need to.

tank, you seem to have written quite a bit for the COM tutorial, so I was wondering why the "Enter" key doesn't work inside of Flash? I have Grooveshark embedded in the script and if I do a search and press enter nothing happens, I have to click the search button to initiate the search. I used the WinGetClass in the WM_KEYDOWN to get the name and it is "MacromediaFlashPlayerActiveX" instead of "Internet Explorer_Server" but I don't know what that means. I tried adding the Flash name to the if statement but that didn't seem to work. Below is the chunk of code I am referring to.
WM_KEYDOWN(wParam, lParam, nMsg, hWnd)
{
	If (wParam = 0x09 || wParam = 0x0D || wParam = 0x2E || wParam = 0x26 || wParam = 0x28) ; tab enter delete up down
	{
		;msgbox, enter pressed
		WinGetClass, Class, ahk_id %hWnd%
		msgbox, %Class%
		If (Class = "Internet Explorer_Server")
		{
			Global pipa
			VarSetCapacity(Msg, 28)
			NumPut(hWnd,Msg), NumPut(nMsg,Msg,4), NumPut(wParam,Msg,8), NumPut(lParam,Msg,12)
			NumPut(A_EventInfo,Msg,16), NumPut(A_GuiX,Msg,20), NumPut(A_GuiY,Msg,24)
			DllCall(NumGet(NumGet(1*pipa)+20), "Uint", pipa, "Uint", &Msg)
			Return 0
		}
	}
}
Thanks so much everyone (tank and sinkfaze). Other than the Enter issue, I have a little more cleanup to do, but once that is done if anyone is interested I will send them the code. Thanks again.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
I have "0" experience with flash and no practical use for it at this time. So I am a piss poor resource for this issue.
Never lose.
WIN or LEARN.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
First I want to say thanks to Jethrow, Tank, Sean and Sinkfaze for their efforts.
I like to base web control on ElementID, so I wrote a few ElementID based Web page control functions + example for beginners.


Functions:

;===ElementID based  Web page control functions by Learning one===
WP_SetDDL(pwb, ElementID, ItemNumber) {		; WP means web page, DDL means DropDownList 
	COM_Invoke(pwb, "document.all.item[" ElementID "].selectedIndex", ItemNumber-1)	; 1 based
}

WP_GetText(pwb, ElementID) {	
	Return COM_Invoke(pwb, "document.all.item[" ElementID "].value")
}

WP_SetText(pwb, ElementID, text="") {	
	COM_Invoke(pwb, "document.all.item[" ElementID "].value", text)
}

WP_Click(pwb, ElementID) {	
	COM_Invoke(pwb,"document.all.item[" ElementID "].click")
}

WP_Navigate(pwb, URL) {	
	COM_Invoke(pwb, "Navigate", URL)
}

WP_WaitToLoad(pwb) {	
	While !(COM_Invoke(pwb,"readyState") = 4)	; by Jethrow or Sean?
	Sleep, 100
}

Example:
;===Description=========================================================================
; ElementID based  Web page control example	with explanations		; by Learning one



;===Settings============================================================================
; Use Compact iWebBrowser3.ahk by Tank to get values below. Look at "Index" value in iWebBrowser3.ahk GUI - that is ElementID.
; Link:		 			https://ahknet.autohotkey.com/~tank/ahk%20web%20recorder.zip
; AHK forum topic: 		http://www.autohotkey.com/forum/topic51270.html

KeywordsBox = 74		; this means: ElementID of "Keywords Box" is 74
AuthorBox = 86
SortByDropDownList = 152
SearchMessageTitleOnlyRadio = 121
SearchButton = 164

URL = http://www.autohotkey.com/forum/search.php			; AHK Search address



;===Auto-execute continuation===========================================================
COM_Init()													; Initialize COM
pwb := COM_CreateObject("InternetExplorer.Application")		; Create new instance of IE and store pointer of web browser in variable pwb
COM_Invoke(pwb , "Visible=", "True")						; make IE visible 
WP_Navigate(pwb, URL)										; navigate to specified URL - web address. "WP" means web page.
WP_WaitToLoad(pwb)											; wait until page is loaded

WP_SetText(pwb, KeywordsBox, "Webpage Controls")			; set text "Webpage Controls" in "Keywords Box"
WP_SetText(pwb, AuthorBox, "jethrow")						; set text "jethrow" in "Author Box"

WP_SetDDL(pwb, SortByDropDownList, 4) 	; "DDL" means DropDownList, "4" means item number 4 - author. So; select item number 4 in "Sort by" DropDown list.
WP_Click(pwb, SearchMessageTitleOnlyRadio)	; click on "Search message title only" radio
WP_Click(pwb, SearchButton)					; click on Search Button

COM_Release(pwb)							; release object
COM_Term()									; terminate COM
ExitApp										; press "back" in IE to see what script did.

;===Some other useful lines===
;WP_SetText(pwb, AuthorBox)					; omit last parameter to clear text from Author Box.
;MsgBox % WP_GetText(pwb, KeywordsBox)		; shows context of "Keywords Box" in MsgBox
;COM_Invoke(pwb, "Quit")					; closes IE


;===Functions===========================================================================
#Include Com.ahk			; COM by Sean


;===ElementID based  Web page control functions by Learning one===
WP_SetDDL(pwb, ElementID, ItemNumber) {		; WP means web page, DDL means DropDownList 
	COM_Invoke(pwb, "document.all.item[" ElementID "].selectedIndex", ItemNumber-1)	; 1 based
}

WP_GetText(pwb, ElementID) {	
	Return COM_Invoke(pwb, "document.all.item[" ElementID "].value")
}

WP_SetText(pwb, ElementID, text="") {	
	COM_Invoke(pwb, "document.all.item[" ElementID "].value", text)
}

WP_Click(pwb, ElementID) {	
	COM_Invoke(pwb,"document.all.item[" ElementID "].click")
}

WP_Navigate(pwb, URL) {	
	COM_Invoke(pwb, "Navigate", URL)
}

WP_WaitToLoad(pwb) {	
	While !(COM_Invoke(pwb,"readyState") = 4)	; by Jethrow or Sean?
	Sleep, 100
}


Escape::ExitApp