HTML tooltip creator editor: ; select a text in an html editor and create/edit/remove a ToolTip

Post your working scripts, libraries and tools for AHK v1.1 and older
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

HTML tooltip creator editor: ; select a text in an html editor and create/edit/remove a ToolTip

09 Apr 2018, 15:50

You can create and add tooltips in any editor (they look like that: https://jsfiddle.net/cmftodzy/ )

(Don't hesitate to propose some improvement, it needs it)

But this script needs 2 things to work:

1) Winclip (2 files: WinClipAPI.ahk WinClip.ahk. You can download them here https://autohotkey.com/board/index.php? ... ach_id=203. Or from this page https://autohotkey.com/board/topic/7467 ... pulations/ )

Copy WinClipAPI.ahk & WinClip.ahk somewhere, get then update the path in the code below.

2) You need to add some CSS (see below the script ahk)


How it works:
To make it work you must first run the ahk script (double-click on it). (it'll do nothing until you press the shortcut)

1/ To create a new tooltip: select the word (or sentence) then press the keyboard shortcut of your choice (alt-x by default)the word should appear on the first field, add the tooltip on the second field. Press ok

2/ To modify an existing tooltip: select the word with the whole tooltip sentence (which can be tricky) then press your shortcut you should see both field (word + its tooltip) if not, try again, until it works!).

2/ To remove the tooltip, repeat step 2, but just delete everything in the tooltip field (the text should appear in both field). Then press ok.


The Mytooltip.ahk:

Code: Select all


!x ; set the shortcut here (currently working with alt-X  → Alt(!) CTRL(^) shift(+) win(#)

; select a text in an html editor and create/edit/remove a ToolTip
;  exemple of tooltip <span class="tooltipzota">my front text<span class="tooltiptextzota"> my back text</span></span>
; tooltip css: https://www.w3schools.com/css/css_tooltip.asp
;  WinClip:  https://autohotkey.com/board/topic/74670-class-winclip-direct-clipboard-manipulations/

#Include C:\Users\User\Desktop\Ahk\modul_ahk\WinClip\WinClip.ahk ;set the path here
#Include C:\Users\User\Desktop\Ahk\modul_ahk\WinClip\WinClip.ahk  ;set the path here


; get highlighted text
WinClip.Clear()
WinClip.Copy()
fromHTML := WinClip.GetHTML()
Send ^x
;msgbox,  % fromHTML


; check if the word "tooltip"  is in "fromHTML"
istooltip := "tooltipzota"
If InStr(fromHTML,istooltip){

	; if "tooltip"  is in "fromHTML", strip all the surrounding html tags and keep only the frontext and backtext only,

			regexError:=RegExMatch(fromHTML, "<span class=""tooltipzota"">(.*)<span class=""tooltiptextzota""", frontext)
			;msgbox %frontext%
			frontexta:= StrReplace(frontext, "<span class=""tooltipzota"">")
			;msgbox %frontexta%
			frontextb:= StrReplace(frontexta, "<span class=""tooltiptextzota""")
			;msgbox %frontextb%

			regexError:=RegExMatch(fromHTML, "<span class=""tooltiptextzota"">(.*)</span></span", backtext)
			;msgbox %backtext%
			backtexta:= StrReplace(backtext, "<span class=""tooltiptextzota"">")
			;msgbox %backtexta%
			backtextb:= StrReplace(backtexta, "</span></span")
			;msgbox %backtextb%

	; then display frontext and backtext in a gui to edit them
			Gui, Add, Edit, w400  vInputfronttext, %frontextb%
			Gui, Add, Edit, w400  vInputbacktext , %backtextb%
			Gui, add, button, x5 y130 h20 w70 gsub1, Ok
			Gui Show
			return

	; then get the edited version of frontext and backtext and decided how to paste them
	sub1:
	{
		 		; get the value saved in the variable of edits
							Gui,submit
							;msgbox %Inputfronttext%
							;msgbox %Inputbacktext%

				; if backtext is empty, then don't paste a tooltip
							if (Inputbacktext ="") {
								html := % "<span>" . Inputfronttext . "</span>"
								WinClip.SetHTML(html)
								send {del}
								send {Left}
								;send {Right}
								WinClip.Paste()
							}

				; if backtext is not empty, then paste the new tooltip
							else {
								;construct the html string:
								html := % "<span class='tooltipzota'>" . Inputfronttext . "<span class='tooltiptextzota'>" . Inputbacktext . "</span></span>"
								;msgbox %html% ; ●

								WinClip.SetHTML(html)
								send {del}
								send {Left}
								;send {Right}
								WinClip.Paste()
							}
	}  ; end of sub1
	ExitApp
	Return

	; close ahk if messagebox is closed
}
; if the word "tooltip"  is not in "fromHTML", open an empty gui
else{
		; strip all the surrounding html tags and keep only the frontext and backtext only,
				regexerror2 := RegExMatch(fromHTML, "<!--StartFragment-->(.*)<!--EndFragment-->", fronttext2)
				;msgbox %fronttext2%
				fronttext2a:= StrReplace(fronttext2, "<!--StartFragment-->")
				;msgbox %fronttext2a%
				fronttext2b:= StrReplace(fronttext2a, "<!--EndFragment-->")
				;msgbox %fronttext2b%

		; then display frontext and backtext in a gui to edit them
				Gui, Add, Edit, w400  vInputfronttext2, %fronttext2b%
				Gui, Add, Edit, w400  vInputbacktext2 ,
				Gui, add, button, x5 y130 h20 w70 gsub2, OK
				Gui Show
				return

	sub2:
	{
	 		; get the value saved in the variable of edits
					Gui,submit
					; if backtext is empty, then don't paste a tooltip
								if (Inputbacktext2 ="") {
									html := % "<span>" . Inputfronttext2 . "</span>"
									WinClip.SetHTML(html)
									send {del}
									WinClip.Paste()
								}

			; if backtext is not empty, then paste the new tooltip
								else {
									;construct the html string:
									html := % "<span class='tooltipzota'>" . Inputfronttext2 . "<span class='tooltiptextzota'>" . Inputbacktext2 . "</span></span>"
									;msgbox %html% ; ●

									WinClip.SetHTML(html)
									send {del}
									WinClip.Paste()
								}

	}  ; end of sub2
	ExitApp
	Return
	; close ahk if messagebox is closed

}
GuiClose:   ;; todo : sent a ctrl-z to
		;sleep 500
		;send ^{tab}
		;send ^z
		;MsgBox close
		ExitApp
		Return

Return


● add this to your CSS

Code: Select all

/* from https://www.w3schools.com/css/tryit.asp?filename=trycss_tooltip*/
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
}

/* Tooltip text */
.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;
 
    /* Position the tooltip text - see examples below! */
    position: absolute;
    z-index: 1;
	left:0px;
	top:15px;
}

/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
    visibility: visible;
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 226 guests