Search in google depending on whether what is selected is a text or a URL

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Search in google depending on whether what is selected is a text or a URL

Post by Elermino » 13 Jan 2022, 17:14

If a normal text is selected, it will search google normally, but if a url is selected, it will go directly to the page.

Code: Select all

!g::
port:=Clipboard
Send, ^c ;Ctrl C
Sleep, 100
Run, % (clip := Trim(clipboard)) ~= "^(https?://)?([\w\d]([\w\d]|(-[\w\d]))*\.)+\w+(/\S*)?$" 					
        ? (clip ~= "^(https?://|www\.)" ? "" : "http://") . clip    
        : "https://www.google.com/search?q=" . fix(clip)
Return

fix(url) 
	{
 	RegExMatch(url, "^(https?://)?(.+?)(/)?$", part)
 	Loop, Parse, part2
  		text .= A_LoopField ~= "[$&+,:;@""<>`%{}|\\^~[\] `]" ? Format("%{:X}", Asc(A_LoopField)) : A_LoopField
 	Return part1 text part3
	}
Clipboard := port

Return to “Scripts and Functions (v1)”