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
Can I get a link title with AHK
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Can I get a link title with AHK
quick and dirtyy method
copy url hit q
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
}
Re: Can I get a link title with AHK
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
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 ""
}
Some of my AHK programs: 3D Voxel Game - Platformer Game
GitHub projects : Image/Pixel scanning class - Direct2D overlay class
GitHub projects : Image/Pixel scanning class - Direct2D overlay class
Re: Can I get a link title with AHK
@AHKStudent works but why am I also getting a msg box saying
"You need a new app to open this about link"
"You need a new app to open this about link"
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Can I get a link title with AHK
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
-
- Posts: 4412
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Can I get a link title with AHK
Just use RegEx instead of the HTMLFILE object.AHKStudent wrote: ↑I never found a solution
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Can I get a link title with AHK
I did manage to isolate the issue and it seems an iframe causes it, here is a quick sampleteadrinker wrote: ↑20 Apr 2022, 14:36Just use RegEx instead of the HTMLFILE object.AHKStudent wrote: ↑I never found a solution
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
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.
-
- Posts: 4412
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Can I get a link title with AHK
Of course it depends on the use case, but I prefer RegEx whenever possible as it is more performant.AHKStudent wrote: ↑JS selectors is much easier, regex is not optimal
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Can I get a link title with AHK
That gave me an idea to use regex replace to just remove the iframe tag thanksteadrinker wrote: ↑20 Apr 2022, 17:05Of course it depends on the use case, but I prefer RegEx whenever possible as it is more performant.AHKStudent wrote: ↑JS selectors is much easier, regex is not optimal
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
}
Who is online
Users browsing this forum: mstrauss2021, Spawnova, william_ahk and 344 guests