URL encode-decode Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Haswell
Posts: 90
Joined: 21 Feb 2016, 17:11

URL encode-decode

Post by Haswell » 23 Dec 2020, 05:42

I work with urls:
hello%20world
I need to decode it back to text:
hello world
How can I do that?
Failed to find a standard function in AHK for such a common issue.
Focusing our efforts on non-productive and non-creative endeavours wastes lives as surely as war.
Jacque Fresco / The best that money can't buy

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode  Topic is solved

Post by teadrinker » 23 Dec 2020, 06:28

There are several methods to do this. One of them:

Code: Select all

str := "hello%20world"
MsgBox, % decoded := EncodeDecodeURI(str, false)
MsgBox, % EncodeDecodeURI(decoded)

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}

User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: URL encode-decode

Post by mikeyww » 23 Dec 2020, 07:05

Code: Select all

MsgBox, % urlToText("hello%20world")

urlToText(url) {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&p=292740#p292740
 VarSetCapacity(text, 600)
 DllCall("shlwapi\PathCreateFromUrl" (A_IsUnicode?"W":"A")
  , "Str", "file:" url, "Str", text, "UInt*", 300, "UInt", 0)
 Return text
}

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 23 Dec 2020, 07:56

@mikeyww
Looks like PathCreateFromUrl doesn't support UTF-8:

Code: Select all

str := "привет"
MsgBox, % encoded := EncodeDecodeURI(str)
MsgBox, % EncodeDecodeURI(encoded, false)
MsgBox, % urlToText(encoded)

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}


urlToText(url) {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&p=292740#p292740
 VarSetCapacity(text, 600)
 DllCall("shlwapi\PathCreateFromUrl" (A_IsUnicode?"W":"A")
  , "Str", "file:" url, "Str", text, "UInt*", 300, "UInt", 0)
 Return text
}

User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: URL encode-decode

Post by mikeyww » 23 Dec 2020, 08:22

We should hold @jeeswg accountable for this! :D

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 23 Dec 2020, 08:47

It won't work, he left us. :)

User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: URL encode-decode

Post by mikeyww » 23 Dec 2020, 08:48

In that case, we should switch to your script!

garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: URL encode-decode

Post by garry » 23 Dec 2020, 11:33

thank you for the examples, I tried this

Code: Select all

url:="https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3A%2F%2F星期日_привет_Спасибо_большое_stackoverflow.com%2F"
y:="returnurl="           ;- delimiter to get this >            https://星期日_привет_Спасибо_большое_stackoverflow.com/
a:=StrSplit(url,y)
url:=a[2] . "`r`n" 
decode := uriDecode(url)
msgbox,%decode%
;fileappend,%decode%,test41.txt,utf-8
return

uriDecode(str) {
   Loop
      If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
         StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
      Else Break
   Return, str
}
;====================================

Haswell
Posts: 90
Joined: 21 Feb 2016, 17:11

Re: URL encode-decode

Post by Haswell » 23 Dec 2020, 11:34

Thanks for these solutions!
Looks a bit complex and unreliable (especially teadrinker's one).
If something will be changed in future the script stop working.
I wonder why it doesn't still implemented in a standard library in a good manner.
Focusing our efforts on non-productive and non-creative endeavours wastes lives as surely as war.
Jacque Fresco / The best that money can't buy

just me
Posts: 9426
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: URL encode-decode

Post by just me » 24 Dec 2020, 05:02

Code: Select all

; ======================================================================================================================
; UrlUnescape() -> https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-urlunescapea
; URL_DONT_UNESCAPE_EXTRA_INFO = 0x02000000
; URL_UNESCAPE_AS_UTF8         = 0x00040000 (Win 8+)
; URL_UNESCAPE_INPLACE         = 0x00100000
; ======================================================================================================================
UrlUnescape(Url, Flags := 0x00100000) {
   Return !DllCall("Shlwapi.dll\UrlUnescape", "Ptr", &Url, "Ptr", 0, "UInt", 0, "UInt", Flags, "UInt") ? Url : ""
}
[Mod. Looking for an encoder as well? :arrow: UrlEscape() by just me]
Last edited by BoBo on 03 Feb 2021, 04:36, edited 1 time in total.
Reason: Added link that points to just me's UrlEscape()-function. HTH :o)

garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: URL encode-decode

Post by garry » 24 Dec 2020, 06:04

thank you again for the solutions / merry Xmas
EDIT : I think problem with not latin characters / removed cyrillic letters

Code: Select all

;- user 'just me'
url:="https%3A%2F%2Fstackoverflow.com%2F"
decode :=UrlUnescape(url)
msgbox,%decode%

UrlUnescape(Url, Flags := 0x00100000) {
   Return !DllCall("Shlwapi.dll\UrlUnescape", "Ptr", &Url, "Ptr", 0, "UInt", 0, "UInt", Flags, "UInt") ? Url : ""
}
Last edited by garry on 24 Dec 2020, 09:40, edited 1 time in total.

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 24 Dec 2020, 06:23

@garry
Why do you use unencoded привет_Спасибо_большое? :) It must be encoded to use in URL.

garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: URL encode-decode

Post by garry » 24 Dec 2020, 09:24

@teadrinker , sorry I was wrong , must correct script above

Code: Select all

;url2:="https://zh-yue.wikipedia.org/wiki/李香蘭"
url2:="https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3D%E6%9D%8E%E9%A6%99%E8%98%AD"

;- works also fine here 
;- url2:="https%3A%2F%2F%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82_%D0%A1%D0%BF%D0%B0%D1%81%D0%B8%D0%B1%D0%BE_%D0%B1%D0%BE%D0%BB%D1%8C%D1%88%D0%BE%D0%B5_stackoverflow.com%2F"


decoded := EncodeDecodeURI(url2, false)
encoded := EncodeDecodeURI(decoded)
msgbox,-------------------------------------------------`nDECODED=`n%decoded%`n-------------------------------------------------`nENCODED=`n%encoded%`n-------------------------------------------------
return
;---------------
;- from user 'teadrinker'
EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}
;=============================================================================

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 24 Dec 2020, 09:58

Also, there is such option:

Code: Select all

url := "https://zh-yue.wikipedia.org/wiki/李香蘭"

MsgBox, % EncodeDecodeURI(url,, false)

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}

garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: URL encode-decode

Post by garry » 24 Dec 2020, 10:48

@teadrinker , thank you for this example , it works both way ( encode and decode )

flipside555
Posts: 25
Joined: 04 Jan 2019, 03:33

Re: URL encode-decode

Post by flipside555 » 06 Jun 2022, 05:34

just me wrote:
24 Dec 2020, 05:02

Code: Select all

; ======================================================================================================================
; UrlUnescape() -> https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-urlunescapea
; URL_DONT_UNESCAPE_EXTRA_INFO = 0x02000000
; URL_UNESCAPE_AS_UTF8         = 0x00040000 (Win 8+)
; URL_UNESCAPE_INPLACE         = 0x00100000
; ======================================================================================================================
UrlUnescape(Url, Flags := 0x00100000) {
   Return !DllCall("Shlwapi.dll\UrlUnescape", "Ptr", &Url, "Ptr", 0, "UInt", 0, "UInt", Flags, "UInt") ? Url : ""
}
[Mod. Looking for an encoder as well? :arrow: UrlEscape() by just me]
I have unicode URLs as in the example below. The above code doesn't seem to be able to handle them. Is there a quick fix?

Code: Select all

href="?page=popthai&amp;search=%E0%B9%81%E0%B8%A1%E0%B8%A7%E0%B8%AA%E0%B8%B5%E0%B8%82%E0%B8%B2%E0%B8%A7%E0%B9%83%E0%B8%99%E0%B8%9E%E0%B8%B2%E0%B8%A2%E0%B8%B8%E0%B8%AB%E0%B8%B4%E0%B8%A1%E0%B8%B0%3F"
Expected result:

Code: Select all

href="?page=popthai&search=แมวสีขาวในพายุหิมะ?"

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 06 Jun 2022, 07:38

Code: Select all

url := "href=""?page=popthai&amp;search=%E0%B9%81%E0%B8%A1%E0%B8%A7%E0%B8%AA%E0%B8%B5%E0%B8%82%E0%B8%B2%E0%B8%A7%E0%B9%83%E0%B8%99%E0%B8%9E%E0%B8%B2%E0%B8%A2%E0%B8%B8%E0%B8%AB%E0%B8%B4%E0%B8%A1%E0%B8%B0%3F"""

MsgBox, % EncodeDecodeURI(url, false)

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: URL encode-decode

Post by JnLlnd » 29 Jan 2023, 17:21

teadrinker wrote:
06 Jun 2022, 07:38

Code: Select all

url := "href=""?page=popthai&amp;search=%E0%B9%81%E0%B8%A1%E0%B8%A7%E0%B8%AA%E0%B8%B5%E0%B8%82%E0%B8%B2%E0%B8%A7%E0%B9%83%E0%B8%99%E0%B8%9E%E0%B8%B2%E0%B8%A2%E0%B8%B8%E0%B8%AB%E0%B8%B4%E0%B8%A1%E0%B8%B0%3F"""

MsgBox, % EncodeDecodeURI(url, false)

EncodeDecodeURI(str, encode := true, component := true) {
   static Doc, JS
   if !Doc {
      Doc := ComObjCreate("htmlfile")
      Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
      JS := Doc.parentWindow
      ( Doc.documentMode < 9 && JS.execScript() )
   }
   Return JS[ (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") ](str)
}
Thanks for this function, teadrinker. I discovered that when the string to decode includes % that is not part of a code like %20, JS[decodeURIComponent] returns an empty string. It could be safe to add some code to return the original string if JS result is empty.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: URL encode-decode

Post by JnLlnd » 29 Jan 2023, 17:29

JnLlnd wrote:
29 Jan 2023, 17:21
I discovered that when the string to decode includes % that is not part of a code like %20, JS[decodeURIComponent] returns an empty string. It could be safe to add some code to return the original string if JS result is empty.
Precision: I understand that an URI to decode should not include % that are not part of a %nn code. So this is not a JS bug. This being said, in my usage of the function, it is still safer to test if JS result is empty.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: URL encode-decode

Post by teadrinker » 29 Jan 2023, 17:36

JnLlnd wrote: I understand that an URI to decode should not include % that are not part of a %nn code.
I'd say, it mustn't include % character, otherwise it is an incorrect URI-encoded string.

Post Reply

Return to “Ask for Help (v1)”