Determine whether the user selected Refresh in WebBrowser Control - webbrowser-control

How can I detect if the user selected Refresh via the context menu? The NavigateComplete2 method does not get invoked when the user selects it.
More importantly, to set own custom user agent, one way to do it is to hook the BeforeNavigate2 event method and it is necessary to know if the user has selected Refresh or navigating a new url.
Any insight would be appreciated.
This demonstrates the NavigateComplete2 method does not get fired when Refresh is selected.
oWB := new WebBrowserControl("http://stackoverflow.com")
Class WebBrowserControl
{
__New(strURL) {
static WB
Gui, New, Resize
Gui, Add, ActiveX, vWB w780 h580, Shell.Explorer
Gui, show, w800 h600
ComObjConnect(WB, this)
WB.Navigate(strURL)
Loop
Sleep 10
Until (WB.readyState=4 && WB.document.readyState="complete" && !WB.busy)
Return
GuiClose:
ExitApp
}
NavigateComplete2(oParams*) {
ComObjError(false)
WB := oParams[1]
msgbox, 64, Navigate Completed
, % "WB.locationURL :`t`t" WB.locationURL "`n"
. "WB.Document.URL:`t`t" WB.Document.URL "`n"
. "windowlocation.href:`t" WB.document.parentWindow.location.href
}
BeforeNavigate2(oParams*) {
WB := oParams[8]
strURL := oParams[2]
msgbox % "Loading URL:`t`t" strURL "`n"
. "WB.locationURL :`t`t" WB.locationURL "`n"
. "WB.Document.URL:`t`t" WB.Document.URL "`n"
. "location.href:`t`t" WB.document.parentWindow.location.href "`n"
. "WB.ReadyState:`t`t" WB.readystate "`n"
. "WB.document.readystate:`t" WB.document.readystate "`n"
. "WB.Busy:`t`t`t" WB.Busy "`n"
}
}

One way to see if a refresh (or new page) has been initiated is to monitor the mouse state (in Chrome, not sure about other browsers) with: if/while (A_Cursor = "AppStarting"). This is true when the mouse cursor has turned into an hourglass.
Comparing the previous and the new URL would tell you if this is a new request or a refresh: ControlGetText CurrentURL, Chrome_OmniboxView1, Chrome
Hope this helps.

Related

AutoHotKey not finding image on a window

I recently discovered AutoHotKey, this scripting language seemed to be amazing !
Unfortunately, I can't manage to make my script find an image on a window (BlueStacks in my case).
Can someone please help me, my script is :
CoordMode, Pixel, screen
CoordMode, Mouse, screen
*ESC::ExitApp
ImgFound := false
while(ImgFound = false)
{
ImageSearch, ButtonX, ButtonY, 0, 0, A_ScreenWidth, A_ScreenHeight, *50 C:\Users\...\Documents\...\test.png
if (ErrorLevel = 2)
{
MsgBox Could not execute script.
ExitApp
}
else if (ErrorLevel = 1)
{
ImgFound := false
}
else
{
MsgBox Found.
click, %ButtonX%, %ButtonY%
ImgFound := true
}
}
Your while loop is unreachable code.
The code execution stops when the first hotkey label is encountered. This is called the Auto-execute Section.
Move your hotkey definition to be at the very bottom.
(All hotkeys always defined by hotkeys labels get always created regardless of being in the auto-execute section or not)

Outlook Inspector Activate doesn't bring in foreground

I have a PowerShell script that search for an email and then open it
Once I have the MailItem object, I get his inspector with MailItem.GetInspector and then I try to display the mail in foreground thanks to Inspector.Display and Inspector.Activate. The doc for Activate method says :
Activates an inspector window by bringing it to the foreground and
setting keyboard focus.
But the Activate doesn't work, the mail is open, but it stays in background, it's not in foreground. And I don't know and don't find why.
My PowerShell script :
param(
[string] $Subject,
[string] $Path
)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$subfolder = $Path.Split('/')
$folder = $namespace.Folders($subfolder[1])
for($i=2; $i -lt $subfolder.Length; $i++) {
$folder = $folder.Folders($subfolder[$i])
}
$filter = "#SQL=urn:schemas:httpmail:subject LIKE '%"+$Subject+"%'"
$mail = $folder.items.find($filter)
$inspector = $mail.GetInspector
$inspector.Display()
$inspector.Activate()
I try with only $inspector.Display() or inspector.Activate(), but the result is the same, the window is displayed, but stay in background.
Thanks for your help !!
Windows would not let a background process (such as outlook.exe) to set the foreground window - keep in mind that even through your process might be in the foreground, the call is marshalled into the outlook.exe address space, which it turn executes it.
To work around that, you'd need to attach the current foreground window to your thread using AttachThreadInput() Windows API function, but you cannot call it from PS.
If using Redemption is an option (I am its author), it exposes SafeInspector.Active method, which will work whether outlook or your process are in the background. In VBS:
Set sInspector = CreateObject("Redemption.SafeInspector")
sInspector.Item = inspector
sInspector.Activate

AutoHotkey - Perform action based on clicked menu item

I looked around the AHK forums, google, and here on stack overflow and could not find any method for pulling this off, so thanks BIG TIME in advance if you can figure this one out. I am using notepad in my examples, but I'm writing this script for a complicated Point Of Sale program.
I want to do something almost exactly like this, but using the application menus as described below instead of a button.
I would like to have a script that performs an action if a certain menu item is clicked. It does not matter whether that is determined by menu position, label or otherwise. Cursor position, however, is probably not viable for my needs.
A simple example of this would be to open a MsgBox when File-Open is selected with the mouse in notepad.
I have implemented a few techniques that enabled me to pull variables out of most control objects following a left click (buttons, drop-down menus, etc), but when I use the application menu (for example, in notepad: file-save or file-open) It returns a blank string. For example:
#z::
MouseGetPos,,,,Ctrl,2
ControlGetText, Text,,ahk_id %Ctrl%
MouseClick, left
Sleep 2500
MsgBox, %Text%
(Note that I used windows-z so as to not return a MsgBox after every left-click.)
Will return 'cancel' if the cancel button was pressed in the 'file-save as' dialogue of notepad, but returns nothing if I activate it while selecting anything from the application menus (file, edit, format, etc.)
I know that these menus are accessible to AHK, as you can do the following do activate 'File-Save' from the notepad menu:
WinMenuSelectItem, Untitled - Notepad,File,Save
But when I use window spy, I see the objects behind the menu, rather than strings that are actually in the menu.
Are these menus actually separate windows that I need to switch to active to read? Or am I trying to do something impossible?
Big thanks in advance, this is something that's been driving me crazy!
Menus are not that easy to get info from, at least not with autohotkeys built-in commands, you will need to use DllCall's to get the info you need.
I have made you an example that works with notepad for me on win7 64b
What i did in the example is look for notepad run it if its not there then get the ID (hWnd) of the notepad window
When you press ctrl + LButton a DllCall to GetMenu using the ID of the notepad window is used to get a menu handle.
After that more DllCall's are used to get things like GetMenuItemCount, GetSubMenu and GetMenuItemInfo.
What this does is it lets us loop over the menu items one by one and getting info about them, in this example we're getting the state of the items, specifically the highlight state.
When an item is found that has the highlight state the script stores the indexs of the item and breaks the loops.
As this is only an example all it does is display a string message with the index numbers and sends a LButton press
Example:
ifWinNotExist, ahk_class Notepad
{
Run, notepad.exe
WinWait, ahk_class Notepad
}
WinGet, hWnd, ID,
^Lbutton::
hMenu :=DllCall("GetMenu", "Uint", hWnd)
loop % (count := DllCall("GetMenuItemCount", "ptr", hMenu))
{
menu_index := A_index
; Note that menu item positions are zero-based.
hSubMenu := DllCall("GetSubMenu", "Uint", hMenu, "int", (A_index-1))
; Set the capacity of mii to sizeof(MENUITEMINFO)
VarSetCapacity(mii, A_PtrSize=8 ? (Size:=80) : (Size:=48), 0)
; Set the cbSize field to sizeof(MENUITEMINFO)
NumPut(Size, mii, 0)
; Set the mask to whatever you want to retrieve.
; In this case I set it to MIIM_STATE=1.
NumPut(1, mii, 4)
loop % (subCount := DllCall("GetMenuItemCount", "ptr", hSubMenu))
{
Sub_index := A_index
; Note that menu item positions are zero-based.
DllCall("GetMenuItemInfo", "UInt", hSubMenu, "UInt", (A_index-1), "UInt", 1, "UInt", &mii)
; Get the state field out of the struct.
fState := NumGet(mii, 12)
if (fState & 0x80) ; 0x80 MFS_HILITE | 0x8 MFS_CHECKED
{
MenuString := "Found in top #" menu_index " Sub pos #" Sub_index
break 2
}
}
}
if (MenuString)
{
Tooltip % MenuString
}
else
{
tooltip no menu item highlighted
}
send {Lbutton}
return
I hope that helps you get an idea of what is needed to do what your trying to do, other possible DllCalls to Look at are MenuItemFromPoint, GetMenuString.
This can take time to get right, but with the Dllcalls i have shown in this example, I hope that you can find other examples of how to use them with autohotkey on the forums.
This AutoHotkey script should do what you require.
It has been tested on Notepad (Windows 7), and should be readily adaptable to other programs that use the standard context menus still used in the vast majority of software. Although many programs use custom menu bars, the context menus are often still standard context menus.
I have provided two scripts, one will show a ToolTip and block triggering a menu item, whenever any menu item is pressed, which is useful for understanding and for diagnostic purposes.
The second script will show a ToolTip and block triggering any menu item if its text string starts with 'Open'.
Note: the text of the Notepad menu item is
Open... Ctrl+O (Open...[tab]Ctrl+O) and not simply 'Open'.
-
;==================================================
;tested on Notepad (Windows 7)
;block any clicks on any menu items when notepad is the active window
;note: to bypass this and click an item anyway, use ctrl+click
;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...
#IfWinActive, ahk_class Notepad
;LButton::
CoordMode, Mouse, Screen
MouseGetPos, , , hWnd
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass in #32768
{
vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}
SendInput {LButton Down}
KeyWait, LButton
MouseGetPos, vPosX, vPosY, hWnd
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass in #32768
{
ControlSend, Dummy, {Click, 0, 0}, ahk_class Notepad
vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}
SendInput {LButton Up}
Return
#IfWinActive
;==================================================
;tested on Notepad (Windows 7)
;block any clicks on the 'open' menu item when notepad is the active window
;note: to bypass this and click an item anyway, use ctrl+click
;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...
#IfWinActive, ahk_class Notepad
LButton::
CoordMode, Mouse, Screen
MouseGetPos, , , hWnd
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass in #32768
{
vItemText := ""
oAcc := Acc_Get("Object", "1", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
if (oAcc.accState(A_Index) & 0x80) ;MF_HILITE := 0x80
if (1, vItemText := oAcc.accName(A_Index))
break
if (SubStr(vItemText, 1, 4) = "Open")
{
vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}
}
SendInput {LButton Down}
KeyWait, LButton
MouseGetPos, vPosX, vPosY, hWnd
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass in #32768
{
ControlSend, Dummy, {Click, 0, 0}, ahk_class Notepad
vItemText := ""
oAcc := Acc_Get("Object", "1", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
if (oAcc.accState(A_Index) & 0x80) ;MF_HILITE := 0x80
if (1, vItemText := oAcc.accName(A_Index))
break
if (SubStr(vItemText, 1, 4) = "Open")
{
vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}
}
SendInput {LButton Up}
Return
#IfWinActive
;==================================================
Note:
The code samples posted in the two links below, achieve related goals.
Is it possible to catch the close button and minimize the window instead? AutoHotKey
AutoHotKey: Run code on Window Event (Close)
Note:
The function JEE_MenuIsActive, link below, can be used to check whether
the menu bar/sysmenu bar is active
in order to distinguish title bar menus from right-click context menus.
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893

Phpbb new successful member registration pop up window

I would like to create a pop up welcome message for the new member that's successfully registered.
But I m having problem of finding where should I put the code, I have check the ucp_register.html ,, but I don't think that is the display content after the member successfully registered, can anyone help me please? Thanks
It would likely to be a more robust solution to display the popup on the first time the user is logged in as an activated user -- after registration they may not be activated, or they may close the browser window immediately after registration.
The way to do this would be to add a column (say, user_JBL_seen_message INT to the phpbb_users table in the database, then modify functions.php to check that column:
In functions.php, find:
// The following assigns all _common_ variables that may be used at any point in a template.
Before, add:
if($user->data['is_registered'] && $user->data['is_active'] && !$user->data['is_bot'])
{
if(isset($user->data['user_JBL_seen_message']) && !$user->data['user_JBL_seen_message']))
{
$showPopup = true;
$sql = 'UPDATE ' . USERS_TABLE . ' SET user_JBL_seen_message = 1
WHERE user_id = ' . (int)$user->data['user_id'];
if (!$result = $db->sql_query($sql))
{
return false;
}
}
}
Then, find:
$template->assign_vars(array(
After, add:
'JBL_POPUP' => $showPopup,
Then, you can add the popup HTML code to your overall_header.html template file, where appropriate...
<!-- IF JBL_POPUP -->
.... your HTML popup code here.....
<!-- END IF -->
If you don't want existing users to see the popup, then fill the new column with 1s.
I also agree with Damien's suggestion to use a jQuery UI dialog rather than a popup -- most users' browsers will block popups. However, use jQuery in noconflict mode to avoid conflicts with other mods.

How do I grab hold of a pop-up that is opened from a frame?

I am testing a website using WatiN.
On one of the pages I get a "report" in an Iframe, within this I frame there is a link to download and save the report. But since the only way to get to the link is to use frame.Link(...) the pop-up closes immediately after opening; Code snippet below
//Click the create graph button
ie.Button(Find.ById("ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_btnCreateGraph")).Click();
//Lets export the data
ie.Div(Find.ById("colorbox"));
ie.Div(Find.ById("cboxContent"));
ie.Div(Find.ById("cboxLoadedContent"));
Thread.Sleep(1000);//Used to cover performance issues
Frame frame = ie.Frame(Find.ByName(frameNameRegex));
for (int Count = 0; Count < 10000000; Count++) {double nothing = (Count/12); }//Do nothing I just need a short pause
//SelectList waits for a postback which does not occur.
try
{
frame.SelectList(Find.ById("rvReport_ctl01_ctl05_ctl00")).SelectByValue("Excel");
}
catch (Exception)
{
//Do nothing
}
//Now click export
frame.Link(Find.ById("rvReport_ctl01_ctl05_ctl01")).ClickNoWait();
IE ieNewBrowserWindow = IE.AttachTo<IE>(Find.ByUrl(urlRegex));
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(150);
fileDownloadHandler.WaitUntilDownloadCompleted(200);
I have tried using ie instead of frame which is why all those ie.Div's are present.
if I use frame the pop-up window opens and closes instantly.
If I use ie I get a link not found error.
If I click on the link manually, while the test is "trying to find the link" the file will download correctly.
I have changed the code to use a different page that doe not have the frame and I still get the same problem download pop-up closes instantly.
[STAThread]
public void TestForMeterDataExport()
{
// Open a new Internet Explorer window and
// goto the website.
IE ie = new IE("https://<URL>", true);
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler("C:\\Documents and Settings\\karnold\\Desktop\\MeterUsageReport_Large.xls");
Regex urlRegex = new Regex("<URL>\\?Mode=true&ReportID=[a-z A-Z 0-9]{30,33}&ControlID=[a-z A-Z 0-9]{30,33}&Culture=1033&UICulture=1033&ReportStack=1&OpType=Export&FileName=BuildingMeterDataReport&ContentDisposition=OnlyHtmlInline&Format=Excel");
//Find the Username text field and input the user ID
ie.TextField(Find.ByName("ctl00$ContentPlaceHolder1$txtUsername")).TypeText("<Name>");
//Find the Password text field and input the password
ie.TextField(Find.ByName("ctl00$ContentPlaceHolder1$txtPassword")).TypeText("PASS");
//Go ahead and login
ie.Button(Find.ByName("ctl00$ContentPlaceHolder1$butLogin")).Click();
//Let's use the Reports Tab
ie.Link(Find.ByUrl("https://<URL>")).Click();
// Let's get the meter data
ie.Link(Find.ByUrl("https://<URL>")).Click();
//Let's choose University of
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_ucFacility_ddlFacility")).SelectByValue("5041");
//Set the date range for which we want to get data
ie.TextField(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_DateRangePicker1_dpBeginDate_TextBox")).TypeText("12/09/10");
ie.TextField(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_DateRangePicker1_dpEndDate_TextBox")).TypeText("12/10/10");
//Click the create report button
ie.Button(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_TopBoxContentPlaceHolder_TopBoxContentPlaceHolder_btnSubmit")).ClickNoWait();
//Lets export the data
Thread.Sleep(2000);
//SelectList waits for a postback which does not occur.
try
{
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl00")).SelectByValue("Excel");
}
catch (Exception)
{
ie.SelectList(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl00")).FireEventNoWait("onchange");
//fire the postback event
}
//Now click export
ie.Link(Find.ById("ctl00_ctl00_ctl00_ContentPlaceHolder1_ContentAreaContentPlaceHolder_ContentAreaContentPlaceHolder_rvMain_ctl01_ctl05_ctl01")).ClickNoWait();
IE ieNewBrowserWindow = IE.AttachTo<IE>(Find.ByUrl(urlRegex));
fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(10);
fileDownloadHandler.WaitUntilDownloadCompleted(20);
}// close TestForMeterDataExport()
Hopefully some one can tell me what I am doing wrong. Thank you
Here is the error that I get when the program can't find the handle maybe it will help
TestCase 'M:WebTest.CommandLine.WatiNConsoleWebAndDB.TestForMeterDataExport'
failed: Error HRESULT E_FAIL has been returned from a call to a COM component.
System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
at SHDocVw.IWebBrowser2.get_Document()
at WatiN.Core.Native.InternetExplorer.IEBrowser.get_NativeDocument()
at WatiN.Core.Native.InternetExplorer.IEWaitForComplete.WaitForCompleteOrTimeout()
at WatiN.Core.WaitForCompleteBase.DoWait()
at WatiN.Core.DomContainer.WaitForComplete(IWait waitForComplete)
at WatiN.Core.Native.InternetExplorer.AttachToIeHelper.FinishInitializationAndWaitForComplete(IE ie, SimpleTimer timer, Boolean waitForComplete)
at WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(Constraint findBy, Int32 timeout, Boolean waitForComplete)
at WatiN.Core.Browser.AttachTo(Type browserType, Constraint constraint, Int32 timeout)
at WatiN.Core.Browser.AttachTo(Type browserType, Constraint constraint)
at WatiN.Core.Browser.AttachTo[T](Constraint constraint)
Web+DB_test_app.cs(139,0): at WebTest.CommandLine.WatiNConsoleWebAndDB.TestForMeterDataExport()
Thanks to Baptiste for the pointer.
//Set the handles and the file save as name
FileDownloadHandler handler = new FileDownloadHandler("MeterUsageReport_Large_Iframe.xls");
// add a watcher to look for the save file local
ie.AddDialogHandler(handler);
//Do not close dialog boxes immediately
ie.DialogWatcher.CloseUnhandledDialogs = false;
//create a single use instance that will be easily cleaned up and avoid having windows open after we are done with them
using (new UseDialogOnce(ie.DialogWatcher, handler))
{
//Now click export
frame.Link(Find.ById("rvReport_ctl01_ctl05_ctl01")).ClickNoWait();
//Grab hold of the poup dialog and download the file
handler.WaitUntilFileDownloadDialogIsHandled(30);
handler.WaitUntilDownloadCompleted(35);
}
Now for the fun this will only work if all of the work is done on localhost.
if you need to hit a server that is not local and ou want to use IE well then
2) You will need to edit the security for "trusted sites to allow scripting of downloads and Iframes.
* a) open IE
* b) Tools -> Internet options
* c) Security tab.
* d) make sure "trusted site" is highlighted
* e) click custom level, Make sure all .Net and .Net reliant components are set to enabled.
* f) Enable or prompt all activeX components
* g) Enable all downloads
* h) Enable .Net framework setup
* i) Enable scripting of web browser controls
* j) Enable allow websites to open windows without address's or status bars.
* k) Enable Launching programs or files from an Iframe.
* l) Everything under scripting is set to enabled or prompt.
* Just so you know, localhost is treated as a "low" security area so tests run on localhost do not need these changes.
* Refer to http://support.microsoft.com/kb/174360