Jump to content

Get Selection (or its position) in IE


Recommended Posts

Hi there

I'm working on a project where I need to know where the selection is in the IE document (it will only contain formatted text... no links, controls or multimedia)

and then insert some code in that position

Can anyone give any help ?

thanks in advance :)

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

no you don't get it...

it's in an embedded IE object... and I don't want to use Ctrl+F or stuff like that, in addition to the fact that I don't know how to retrieve the selection's position with Ctrl+F

I want to get the selection's position by using _IE_commands

#include <IE.au3>
$js="java script: document.body.contentEditable = 'true'; document.designMode = 'on'; void 0"
$gui=GUICreate("",600,600)
GUISetState()
$oIe= _IECreateEmbedded()
GUICtrlCreateObj($oIe,0,0,600,600)
_IENavigate($oIe, "http://torels.altervista.org")
_IENavigate($oIe,$js)
Do
Until GUIGetMsg()=-3

here is what I have but it's absolutely nohing

what I needed was a method to get the selection's position & length

thanks :)

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

so

  • run the code above
  • select anything in the page you get (which is editable because of the _IE_Navigate($oIe, $js))

How am I going to get the position of the selection ???

(so to insert some html code in that position)

my goal is basically to do in the $oIe what _GUICtrlEdit_GetSel() does in an edit

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

bump

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

  • Moderators

Have fun!

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

_IEErrorHandlerRegister()

$oIE = _IECreateEmbedded()
GUICreate("Embedded Web control Test", 640, 580, _
        (@DesktopWidth - 640) / 2, (@DesktopHeight - 580) / 2, _
        $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$GUIActiveX = GUICtrlCreateObj($oIE, 10, 40, 600, 360)
$GUI_Button_Paste = GUICtrlCreateButton("Paste", 120, 420, 100, 30)
$GUI_Button_Home = GUICtrlCreateButton("Home", 230, 420, 100, 30)

GUISetState() ;Show GUI

_IENavigate($oIE, "http://torels.altervista.org")
$oIE.document.body.contentEditable = True
$oIE.document.designMode = 'on'

; Waiting for user to close the window
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $GUI_Button_Home
            _IENavigate($oIE, "http://torels.altervista.org")
        Case $msg = $GUI_Button_Paste
            $oRange = _IE_CurrentSelectionGetRange($oIE)
            If Not @error Then
                _IE_RangePasteHTML($oRange, ClipGet())
            EndIf
    EndSelect
WEnd

GUIDelete()

Exit

Func _IE_CurrentSelectionGetRange(ByRef $oIE)
    $oRange = $oIE.document.selection.createRange()
    If IsObj($oRange) Then
        Return $oRange
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_IE_CurrentSelectionGetRange

Func _IE_RangePasteHTML(ByRef $oRange, $sHTML)
    $oRange.pasteHTML($sHTML)
    Return 1
EndFunc   ;==>_IE_RangePasteHTML
Link to comment
Share on other sites

  • 1 year later...

Hi Bigdaddy,

I am trying to insert a character in a textarea / text box but according to MSDN documentation pasteHTML does not work with textarea or text .

How do I go ahead with this?

Regards

DeltaRocked.

Have you tried it? Worked for me. The MSDN is not entirely clear: It might as well say "PasteHTML does not work when used incorrectly".
Link to comment
Share on other sites

Have you tried it? Worked for me. The MSDN is not entirely clear: It might as well say "PasteHTML does not work when used incorrectly".

I have tried with this code .

I have also tried with

I have tried $oIE.document.selection.createRange()

but always give me this error

The requested action with this object has failed.:

$oRange.pasteHTML('a')

$oRange.pasteHTML('a')^ ERROR

Where am i going wrong?

This site which I am visiting is http://www.google.com

Regards

DeltaRocked.

$oIE = _IEAttach($ie_handle, 'Embedded')
    $oActive_e = $oIE.document.activeElement.name
    $oForms = _IEFormGetCollection($oIE)
    $iNumForms = @extended
    Local $string = ''
    For $i = 0 To $iNumForms - 1
        $oForm = _IEFormGetCollection($oIE, $i)
        $val = _IEFormElementGetObjByName($oForm, $oActive_e)
        If @error == 0 Then
            $element_val_old = $val.value
            $i_old = $i
            ExitLoop
        EndIf
    Next
    ControlSend($ie_handle, '', $control_class_namenn, "?")
    $oForm = _IEFormGetCollection($oIE, $i_old)
    $val = _IEFormElementGetObjByName($oForm, $oActive_e)
    $element_val_new = $val.value
    $oRange = $oForm.document.selection.createRange() ; 
    $oRange.pasteHTML('a')
    MsgBox(0, '', $element_val_old & @CRLF & $element_val_new)
Link to comment
Share on other sites

That fails for me too. Not sure what the difference is between my test and yours.

Is something like this a suitable alternative? I have just google since you said you used that.

#include <IE.au3>
$oIE = _IECreate("http://www.google.com")
_IEErrorHandlerRegister()
$oActive_e = $oIE.document.activeElement.name
$oForms = _IEFormGetCollection($oIE)
$iNumForms = @extended
Local $string = ''
For $i = 0 To $iNumForms - 1
    $oForm = _IEFormGetCollection($oIE, $i)
    $val = _IEFormElementGetObjByName($oForm, $oActive_e)
    If @error == 0 Then
        $element_val_old = $val.value
        $i_old = $i
        ExitLoop
    EndIf
Next
;ControlSend($ie_handle, '', $control_class_namenn, "?")
$oForm = _IEFormGetCollection($oIE, $i_old)
$val = _IEFormElementGetObjByName($oForm, $oActive_e)
$element_val_new = $val.value
_IEPropertySet($val, "innertext", "lol")

;MsgBox(0, '', $element_val_old & @CRLF & $element_val_new)
Link to comment
Share on other sites

That fails for me too. Not sure what the difference is between my test and yours.

Is something like this a suitable alternative? I have just google since you said you used that.

Hi Manadar,

Solved this... This will insert the text at the current CARET position :) inside IE only.

$oRange = $val.document.selection.createRange()
$orange.text='d'
Edited by deltarocked
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...