使用“•”而不是“.”来解析 URI

问题描述 投票:0回答:2

我使用的论坛有禁止直接商业链接的政策,所以我经常做的就是对其进行修改,使其保持可读性,但需要手动复制/粘贴/编辑才能工作。我将使用 www•example•com 而不是 www.example.com 。 SO 帖子编辑器按照您的预期对该 URI 进行编码,将

替换为
%E2%80%A2
(因此
https://www%E2%80%A2example%E2%80%A2com
),但是当我单击链接时,我会转到 https://xn--wwwexamplecom-kt6gha 。这也是论坛发帖后发回的HREF。

xn--
标头似乎是恒定的,前两个域组件的“粘合”也是如此,但令人烦恼的是其余部分随着域名的函数而变化。
-kt6gha
位是特定于域的,TLD 可以粘贴到其余部分,如此处或位于字母数字部分之后。

我猜这种转换是确定性的,但它可以逆转吗?最好在 userscript.js 中,这样我就可以为自己撤消自己的明智举动? ;)

url obfuscation userscripts demangler
2个回答
0
投票

所以这就是

punicode
,其中
is intended for the encoding of labels in the Internationalized Domain Names in Applications (IDNA) framework, such that these domain names may be represented in the ASCII character set allowed in the Domain Name System of the Internet

我从 https://stackoverflow.com/a/301287/1460868 提取并调整了解码器,使其可以在完整的 URL 上运行:

    this.ToUnicode = function ( domain ) {
        var protocol = '';
        if (domain.startsWith('https://')) {
            protocol = 'https://';
            domain = domain.substring(8);
        } else if (domain.startsWith('http://')) {
            protocol = 'http://';
            domain = domain.substring(8);
        }
        var ua = domain.split('/');
        domain = ua[0];
        urlpath = ua.slice(1);
        var domain_array = domain.split(".");
        var out = [];
        for (var i=0; i < domain_array.length; ++i) {
            var s = domain_array[i];
            out.push(
                s.match(/^xn--/) ?
                punycode.decode(s.slice(4)) :
                s
            );
        }
        var result = protocol + out.join(".") + '/' + urlpath.join('/');
        return result;
    }

(这是修改后的位,除了剥离的编码功能之外。)

我现在可以在这个片段中调用它,它对由愚蠢的上游论坛过滤器完成的链接进行了一些整理:

    // also do the same replacements in the URLs
    var links = document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++) {
        var link = /[\/\.]xn--/.test(links[i].href) ?
                punycode.ToUnicode(links[i].href)
                : links[i].href;
        urlRegexs.forEach(function (value, index) {
            var newlink = link.replace(value, urlReplacements[index]);
            if (newlink !== link) {
                links[i].href = newlink;
            }
        });
    }

我不明白的是,如果编码是标准的一部分,为什么浏览器不这样做!


0
投票

我对某些 IANA 符号也有类似的问题。因此,几年来,我会用“•.°Δ”签署我的消息,这已经变成了一些应用程序中的链接。 我希望该链接看起来像这样 'http://xn--nvg.xn--nba281o' 但结果是这样的 'http://xn--nvg. xn--nba281o'

所以我很好奇,您刚刚共享的代码是否能够读取或将代码转换回可用的格式。这段代码会被注入到哪里?

我的目标是实际上能够在任何功能应用程序中使用该链接(联系信息、域名条目、社交资料等)

© www.soinside.com 2019 - 2024. All rights reserved.