Jump to content

Pseudo embed google chrome


JohnOne
 Share

Recommended Posts

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

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

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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.

Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

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.

Link to comment
Share on other sites

  • 3 years later...

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.

Link to comment
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.

Link to comment
Share on other sites

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.

Link to comment
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

 

Link to comment
Share on other sites

  • 10 months later...

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.

Link to comment
Share on other sites

  • 5 years later...

Well, this is an interesting way to do fake embedding, and I like it.

Since I have a 4k monitor and set the screen scale to 175%, the code above doesn't work right. Plus the kiosk mode now will always try to show full screen.

So I just use the switch "--app=", and hide the window control. So maybe this will work.

Updated:

  • F11, F12 and Alt-Space are disabled. So it will not have some embarrassing behavior. Anyone knows how to disable Fullscreen mode for Chrome?
  • Now you can drag the Pseudo Chrome around. Thought it's not recommended.
  • You can use CDP to control this chrome instance. Just browse "http://localhost:9222/json" and you will see.

It seem this could be a "quick-and-dirty" way to embed a website in your AutoIt app.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIEx.au3>
#include <Constants.au3>
#include <WinAPISysWin.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <ColorConstants.au3>

; Get the screen scale for high dpi screens
$scale = RegRead("HKCU\Control Panel\Desktop\WindowMetrics", "AppliedDPI") / 96

DllCall("User32.dll", "bool", "SetProcessDPIAware")

HotKeySet( "{F11}", "Nope")
HotKeySet( "{F12}", "Nope")
HotKeySet( "! ", "Nope")

_Example()

Func _Example()

    Local $hGui, $sURL = "https://www.google.com"
    Local $iLeft = 0, $iTop = 0, $iWidth = scale(1024), $iHeight = scale(768)

    $hGui = GUICreate("Test", $iWidth+scale(100), $iHeight+scale(100), Default, Default, BitOR( $GUI_SS_DEFAULT_GUI, $WS_SIZEBOX ) )
    $hGroup = GUICtrlCreateGroup( "Chrome Browser Holder", _
        $iLeft+scale(25), $iTop+scale(25), $iWidth+scale(50), $iHeight+scale(50), _
        $BS_CENTER _
        )

    $aGroupPos = ControlGetPos( $hGui, "", $hGroup )

    GUICtrlSetBkColor( -1, $COLOR_Yellow)
    GUICtrlSetFont( -1, 12, 700, 0, "Comic Sans Ms")
    GUICtrlSetResizing( -1, 802)    ; Not changing size
    Local $aMain = ChromeCreatePseudoEmbedded($iLeft+scale(50), $iTop+scale(50), $iWidth, $iHeight, $hGUI, $sURL)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                WinClose($aMain[1]) ; Chrome kiosk window
                GUIDelete($hGui) ; Own gui
                ProcessWaitClose($aMain[0]) ; Chrome executable
                Exit
            Case $GUI_EVENT_PRIMARYDOWN
                _ControlMove($hGroup)
                Local $aNew = ControlGetPos( $hGui, "", $hGroup )
                If $aNew[0] <> $aGroupPos[0] or $aNew[1] <> $aGroupPos[1] Then
                    WinMove( $aMain[1], "", $aNew[0]+scale(25), $aNew[1]+scale(25) )
                    $aGroupPos = $aNew
                    GUICtrlSetState( $hGroup, @SW_HIDE)
                    GUICtrlSetState( $hGroup, @SW_SHOW)
                EndIf
        EndSwitch

        If Not WinExists( $aMain[1] ) Then
            GUIDelete($hGui)
            Exit
        EndIf
        ; Sleep(100)
    WEnd

EndFunc   ;==>_Example
Exit

Func _ControlMove($iControlID) ; By Melba23
    Local Const $SC_MOVE = 0xF010
    Local $aReturn = GUIGetCursorInfo()
    If @error Then
        Return 0
    EndIf
    If $aReturn[4] = $iControlID Then
        GUICtrlSendMsg($iControlID, $WM_SYSCOMMAND, BitOR($SC_MOVE, $HTCAPTION), 0)
    EndIf
EndFunc   ;==>_ControlMove

Func scale($iLen)
    Return Int( $iLen * $scale)
EndFunc

Func Nope()
    ; Do nothing here.
EndFunc

;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 " _
        & "--incognito " _
        & "--disable-plugins " _
        & "--disable-extensions " _
        & "--remote-debugging-port=9222 " _
        & "--disable-windows10-custom-titlebar " _
        & "--app=" & $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])
    C("h:" & $acMain[1] & " t:" & WinGetTitle($acMain[1]))

    ; Return $acMain
    WinMove( $acMain[1], "", $i_Left, $i_Top, $i_Width, $i_Height)

    c( "w:" & $i_Width& " h:" & $i_Height)
    $iStyle = _WinAPI_GetWindowLong($acMain[1], $GWL_STYLE)
    c( "istyle:" & Hex($iStyle) )
    $iNewStyle = BitAND($iStyle, BitNOT($WS_SIZEBOX), BitNOT($WS_SYSMENU), BitNOT($WS_CAPTION) )
    _WinAPI_SetWindowLong($acMain[1], $GWL_STYLE, $iNewStyle )
    c( "istyle2:" & $iNewStyle )
    ; $iExStyle = _WinAPI_GetWindowLong($acMain[1], $GWL_EXSTYLE)

    ; _WinAPI_SetWindowLong($acMain[1], $GWL_EXSTYLE, BitOR($iExStyle, $WS_EX_MDICHILD) )
    ; c( "exstyle:" & Hex($iExStyle) )
    ; NoClose($acMain[1])
    _WinAPI_SetParent($acMain[1], $h_Parent)

    WinSetState( $acMain[1], "", @SW_RESTORE)
    WinSetState( $acMain[1], "", @SW_LOCK)

    GUISetState(@SW_SHOW, $h_Parent)

    WinActivate($acMain[1])

    Return $acMain

EndFunc   ;==>ChromeCreatePseudoEmbedded

Func q($str)
    Return '"'&$str&'"'
EndFunc

Func c($str)
    ConsoleWrite($str&@CRLF)
EndFunc

 

Edited by philpw99
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...