commit ebd56bbead040cf037d6cdad578cb40435b94d04 parent 9ecb7d695f4df0629a246e94757b5198bf5b3976 Author: Sergey Orlov <wladimirych@gmail.com> Date: Fri, 30 Apr 2021 13:42:19 +0300 search control: fix loading short yandex links Yandex has broken markup (unquoted value of "src" attribute) which causes failure in parseFromString(). Fallback is added in the form of extracting meta property value with a regexp. Diffstat:
M | src/lib/leaflet.control.search/providers/links.js | | | 14 | +++++++++++--- |
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/src/lib/leaflet.control.search/providers/links.js b/src/lib/leaflet.control.search/providers/links.js @@ -50,9 +50,17 @@ const YandexMapsUrl = { try { if (url.pathname.match(/^\/maps\/-\//u)) { isShort = true; - const xhr = await fetch(urlViaCorsProxy(url.toString())); - const dom = new DOMParser().parseFromString(xhr.response, 'text/html'); - actualUrl = new URL(dom.querySelector('meta[property="og:image:secure_url"]').content); + const pageText = (await fetch(urlViaCorsProxy(url.toString()))).response; + try { + const dom = new DOMParser().parseFromString(pageText, 'text/html'); + actualUrl = new URL(dom.querySelector('meta[property="og:image:secure_url"]').content); + } catch (_) { + let propertyContent = pageText.match( + /<meta\s+property\s*=\s*["']?og:image:secure_url["']?\s+content\s*=\s*["']?([^"' >]+)/u + )[1]; + propertyContent = propertyContent.replaceAll('&', '&'); + actualUrl = new URL(decodeURIComponent(propertyContent)); + } } else { actualUrl = url; }