Sign in to follow this  
Followers 0
JohnOne

Pseudo embed google chrome

13 posts in this topic

#1 ·  Posted (edited)

Inspired from '?do=embed' frameborder='0' data-embedContent>>

You cannot use chrome to navigate or access the DOM, but I suppose you just might want to display your page, or your app.

You need latest version of chrome as far as I know.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIEx.au3>
#include <Constants.au3>

_Example()

Func _Example()

    Local $hGui, $sURL = "https://www.google.co.uk"
    Local $iLeft = 0, $iTop = 0, $iWidth = 1024, $iHeight = 768

    $hGui = GUICreate("Test", $iWidth, $iHeight)
    Local $aMain = ChromeCreatePseudoEmbedded($iLeft, $iTop, $iWidth, $iHeight, $hGUI, $sURL)


    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then
            WinClose($aMain[1]) ; Chrome kiosk window
            WinClose($hGui) ; Own gui
            ;ProcessClose($aMain[0]) ; Chrome executable
            ProcessWaitClose($aMain[0]) ; Chrome executable
            Exit
        EndIf
    WEnd

EndFunc   ;==>_Example

;Returns a 1D array where [0] is the process id of the chrome executable which is launched
;and [1] is the handle to the chrome window.
;They are both needed to cleanly close.
Func ChromeCreatePseudoEmbedded($i_Left, $i_Top, $i_Width, $i_Height, $h_Parent, $sURL)
;http://www.autoitscript.com/forum/topic/152173-pseudo-embed-google-chrome/

    Local $acMain[2]

    $acMain[0] = Run(@ProgramFilesDir & "\Google\Chrome\Application\chrome.exe --chrome-frame -kiosk " & $sURL)
    If @error Then
        MsgBox(0, 0, @error)
        Exit
    EndIf

    Local $ah_HWND
    Do
        $ah_HWND = _WinAPI_EnumProcessWindows($acMain[0])
        Sleep(10)
    Until IsArray($ah_HWND)

    $acMain[1] = HWnd($ah_HWND[1][0])

    _WinAPI_SetParent($acMain[1], $h_Parent)
    _WinAPI_SetWindowLong($acMain[1], $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

    ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 2)
    GUISetState(@SW_SHOW, $h_Parent)
    WinActivate($acMain[1])

    Return $acMain

EndFunc   ;==>ChromeCreatePseudoEmbedded

.EDIT: Remove ProcessClose(), Closes itself after WinClose.

EDIT2: I have also noticed you can include more urls, and ctrl tab between them.

For example $sURL = "https://www.google.co.uk http://www.yahoo.com"

I'm not yet sure though whether that would leave orphan window handles or processes after closing.

.

Edited by JohnOne
1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



I was able to use this for a customer that insists on viewing a portal through Chrome. Thanks for the contribution :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Any way to do this without a full chrome install?(what files are "required")

Share this post


Link to post
Share on other sites

I doubt it, but that does not mean no.

I believe there is a portable version of chrome, but I don't know if kiosk mode is supported and do not have the time to test it.

It should be trivial to test though if it is something you want.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

This may be a little late, but you can very likely do the same with Iron Portable.  It's built off the Chrome source, but it's portable and it doesn't call home to Google.

Share this post


Link to post
Share on other sites

Just for the feedback: I tried your example, I got a weird behavior: the embedded windows goes all gray, mosty when you loose the focus, and it's flickering a bit when loading a page if I'm not mistaken.

Also one things I had to change: my own chrome.exe is located here: C:UserslyrAppDataLocalGoogleChromeApplicationchrome.exe

Thanks for the contribution.

Share this post


Link to post
Share on other sites

Is it possible to enable right click for context menu?

I've tried the usual method but the menu is in a layer below the embedded layer, same with adding buttons.

Share this post


Link to post
Share on other sites

It is not under any layers as far as I know, chrome just disables it in kiosk mode, you have to embed it without that to have context menu available.

$acMain[0] = Run(@ProgramFilesDir & "\Google\Chrome\Application\chrome.exe --chrome-frame " & $sURL)

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks for your reply, I tried that but it didn't work as I wanted it. I want everything, kiosk and right click.

I had another idea, I don't mind having a gap at the bottom, so that the embedded window is about ~50 pixels from the bottom of the gui window. Although struggling to make that  work, seems kiosk always wants to be full window.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I think you can just adjust the height like so..

ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 2)

;becomes

ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 30)

 

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks JohnOne, I already tried that too but didn't work.

Here's my code, although I'm a nube, so it's probably a bit rough.

#include <WindowsConstants.au3>
#include <WinAPIEx.au3>

Opt("GUIOnEventMode", 1)

Global Const $GUI_EVENT_CLOSE = -3
Global Const $GUI_ENABLE = 64
Global Const $GUI_DISABLE = 128

Global $hGUI1, $s1, $hGUI2 = 9999, $hButton2, $hButton3 = 9999 ; Predeclare the variables with dummy values to prevent firing the Case statements
Global $MenuItem1, $MenuItemExit, $exit = 0
Local  $ContextMenu, $separator1, $InputLocation, $l, $UserLocation
Local Const $sFilePath = "C:\Users\Home\OneDrive\AutoIt3\EarthWind\EWConfig.ini"

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
Local $fDiff = 3700000

Local $hGui, $sURL = "https://earth.nullschool.net/#current/wind/surface/level/orthographic=-27.12,35.50,554 http://www.google.com"
Local $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight - 1

$hGUI1 = GUICreate('',(@DesktopWidth),(@DesktopHeight - 1),0,1,$WS_POPUP)
Local $aMain = ChromeCreatePseudoEmbedded($iLeft, $iTop, $iWidth, $iHeight, $hGUI1, $sURL)
$Input1 = GUICtrlCreateInput("$UserLocation", 16, 8, 121, 21)
GUISetState(@SW_SHOW)
$ContextMenu = GUICtrlCreateContextMenu()
$MenuItem1 = GUICtrlCreateMenuItem("Set Default Location", $ContextMenu)
GUICtrlSetOnEvent($MenuItem1, "gui2")
$separator1 = GUICtrlCreateMenuItem("", $ContextMenu) ; create a separator line
$MenuItemExit = GUICtrlCreateMenuItem("Exit", $ContextMenu)
GUICtrlSetOnEvent($MenuItemExit, "On_Close")
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
GUISetState()

Local $ProgramFilesDir = EnvGet('ProgramFiles(x86)') ; for 64bit Win it will return a valid path.
if not $ProgramFilesDir then $ProgramFilesDir = @ProgramFilesDir ; for 32bit Win this will "repair" the broken return from above.

While 1
    If $fDiff > 3600000 Then
        Send("{F5}")
        $hTimer = TimerInit()
    EndIf
    $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
    Consolewrite($fDiff & @CRLF)
    Sleep(1000)
WEnd


 Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close") ; Call a common GUI close function
    $InputLocation = GUICtrlCreateInput($l, 16, 8, 121, 21)
    GUICtrlSetData(-1, IniRead($sFilePath, "Location", "key1", "Oxford" ))
    $hButton3 = GUICtrlCreateButton("Cancel",  20, 100, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    $hButton2 = GUICtrlCreateButton("OK",  110, 100, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button") ; Call a common button function
    GUISetState()
 EndFunc   ;==>gui2


 Func On_Close()
    Switch @GUI_WINHANDLE ; See which GUI sent the CLOSE message
         Case $hGUI1
            WinClose($aMain[1]) ; Chrome kiosk window
            WinClose($hGui1) ; Own gui
            ProcessWaitClose($aMain[0]) ; Chrome executable
            Exit ; If it was this GUI - we exit <<<<<<<<<<<<<<<
         Case $hGUI2
            GUIDelete($hGUI2) ; If it was this GUI - we just delete the GUI <<<<<<<<<<<<<<<
            GUICtrlSetState($hButton2, $GUI_ENABLE)
    EndSwitch
 EndFunc


 Func On_Button()
     Switch @GUI_CTRLID ; See which button sent the message
         ;Case $hButton1
             ;MessageBox(1) ; We can call a function with parameters here <<<<<<<<<<<<<<<<<<<
         Case $hButton2
             $s1 = GUICtrlRead($InputLocation, $l) ; Read input 1 until the text has been set in input 2; because it's in a while loop the process will be executed until the script has exited
             IniWrite($sFilePath, "Location", "key1", $s1)
             ConsoleWrite(IniRead($sFilePath, "Location", "key1", "Error") & @CRLF)
             GUICtrlSetState($hButton2, $GUI_DISABLE)
             On_Close()
         Case $hButton3
             ConsoleWrite($s1 & @CRLF)
             GUICtrlSetState($hButton2, $GUI_DISABLE)
             On_Close()
     EndSwitch
 EndFunc

 Func ChromeCreatePseudoEmbedded($i_Left, $i_Top, $i_Width, $i_Height, $h_Parent, $sURL)
;http://www.autoitscript.com/forum/topic/152173-pseudo-embed-google-chrome/

    Local $acMain[2]
    $acMain[0] = Run(@ProgramFilesDir & "\Google\Chrome\Application\chrome.exe --chrome-frame -kiosk" & $sURL)
    If @error then
        $acMain[0] = Run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --chrome-frame -kiosk " & $sURL)
        If @error Then
            MsgBox("", "Error", "Please install Google Chrome")
            Exit
        EndIf
    Endif

    Local $ah_HWND
    Do
        $ah_HWND = _WinAPI_EnumProcessWindows($acMain[0])
        Sleep(10)
    Until IsArray($ah_HWND)

    $acMain[1] = HWnd($ah_HWND[1][0])

    _WinAPI_SetParent($acMain[1], $h_Parent)
    _WinAPI_SetWindowLong($acMain[1], $GWL_STYLE, $WS_POPUP + $WS_VISIBLE)

    ;ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 2)
    ControlMove($acMain[1], "", "", $i_Left + 1, $i_Top + 1, $i_Width - 2, $i_Height - 30)
    GUISetState(@SW_SHOW, $h_Parent)
    WinActivate($acMain[1])

    Return $acMain

EndFunc   ;==>ChromeCreatePseudoEmbedded

 

Share this post


Link to post
Share on other sites

#13 ·  Posted

I hope I'm not gravedigging when I ask this, but is there a way to re-direct the frame to a new url after the previous one has loaded? Also would there be a way to scroll the window all the way to the bottom.

 

I am working on an IM program and am thinking about trying to use google chrome to display the formatted text instead of IE.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0