I need to decode it back to text:hello%20world
How can I do that?hello world
Failed to find a standard function in AHK for such a common issue.
I need to decode it back to text:hello%20world
How can I do that?hello world
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)
}
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
}
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
}
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
}
;====================================
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 : ""
}
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 : ""
}
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)
}
;=============================================================================
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)
}
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?just me wrote: ↑24 Dec 2020, 05:02[Mod. Looking for an encoder as well? UrlEscape() by just me]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 : "" }
Code: Select all
href="?page=popthai&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"
Code: Select all
href="?page=popthai&search=แมวสีขาวในพายุหิมะ?"
Code: Select all
url := "href=""?page=popthai&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.teadrinker wrote: ↑06 Jun 2022, 07:38Code: Select all
url := "href=""?page=popthai&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) }
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.