Can I get a link title with AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Can I get a link title with AHK

20 Apr 2022, 09:59

For example I am pointing at some random youtube link,right click and copy the link
Is there is some way to get that link title with AHK without opening it in some browser and copying the titlebar or downloading the html file and searching for the title
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Can I get a link title with AHK

20 Apr 2022, 10:11

quick and dirtyy method

copy url hit q

Code: Select all

q::
gettitle := UrlDownloadToVar2(Clipboard)
document := ComObjCreate("HTMLfile")
document.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
document.designMode := "on"
document.write(gettitle)
MsgBox, % document.title
ExitApp
UrlDownloadToVar2(URL) {
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
try	WebRequest.Open("GET", url, false)
catch	error
	return error.Message
WebRequest.Send()
Return WebRequest.ResponseText
}
User avatar
Spawnova
Posts: 557
Joined: 08 Jul 2015, 00:12
Contact:

Re: Can I get a link title with AHK

20 Apr 2022, 10:22

Here's another way as well =P

Some googling also shows a way to get the title from a youtube no embed link, which returns very basic information in json format including title

Code: Select all

;GetTitleFromUrl(clipboard)
Msgbox % GetTitleFromUrl("https://www.youtube.com/watch?v=hZRggm8RkhM") ;example

exitapp

GetTitleFromUrl(url) {
	http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	http.Open("GET", "https://www.youtube.com/oembed?url=" url "&format=json", true)
	http.Send()
	http.WaitForResponse()
	
	if (RegExMatch(http.ResponseText,"\x22title\x22:\x22([^\x22]+)",title)) { ;could use a json library but this should work just as well
		return title1
	}
	return ""
}
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Can I get a link title with AHK

20 Apr 2022, 10:26

@AHKStudent works but why am I also getting a msg box saying
"You need a new app to open this about link"
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Can I get a link title with AHK

20 Apr 2022, 13:53

vsub wrote:
20 Apr 2022, 10:26
@AHKStudent works but why am I also getting a msg box saying
"You need a new app to open this about link"
post a sample link please

edit: this is an old issue on some pages, I never found a solution. There is something within the html (possibly a #) that triggers that
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Can I get a link title with AHK

20 Apr 2022, 14:36

AHKStudent wrote: I never found a solution
Just use RegEx instead of the HTMLFILE object.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Can I get a link title with AHK

20 Apr 2022, 15:04

teadrinker wrote:
20 Apr 2022, 14:36
AHKStudent wrote: I never found a solution
Just use RegEx instead of the HTMLFILE object.
I did manage to isolate the issue and it seems an iframe causes it, here is a quick sample

Code: Select all

var = 
(
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>A Title</title>
<iframe src="https://www.youtube.com/bb/bb.aspx?utm_source=bb" style="width:300px;height:250px;" scrolling="no"></iframe>
</body>
</html>
)
document := ComObjCreate("HTMLfile")
document.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
document.designMode := "on"
document.write(var)
MsgBox, % document.title
ExitApp
You might have to run it a few times for the box on windows 10 to pop up

For this case regex for the title is great, but in other cases that I want to get a lot of data and using regular JS selectors is much easier, regex is not optimal. For many months now I just ignore the popup, it doesn't hurt any of the data but if there was a solution that would be very nice.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Can I get a link title with AHK

20 Apr 2022, 17:05

AHKStudent wrote: JS selectors is much easier, regex is not optimal
Of course it depends on the use case, but I prefer RegEx whenever possible as it is more performant.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Can I get a link title with AHK

20 Apr 2022, 21:10

teadrinker wrote:
20 Apr 2022, 17:05
AHKStudent wrote: JS selectors is much easier, regex is not optimal
Of course it depends on the use case, but I prefer RegEx whenever possible as it is more performant.
That gave me an idea to use regex replace to just remove the iframe tag thanks :thumbup:

Code: Select all

q::
gettitle := UrlDownloadToVar2(Clipboard)
document := ComObjCreate("HTMLfile")
document.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
document.designMode := "on"
gettitle := RegExReplace(gettitle, "iframe","")
document.write(gettitle)
MsgBox, % document.title
ExitApp
UrlDownloadToVar2(URL) {
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
try	WebRequest.Open("GET", url, false)
catch	error
	return error.Message
WebRequest.Send()
Return WebRequest.ResponseText
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021, Spawnova, william_ahk and 344 guests