如何逆向转换xn--开头的国际化域名?求Userscript实现方案
可以逆向还原,用UserScript实现的方案
这种xn--开头的地址是Punycode编码的国际化域名(IDN),是论坛把含Unicode字符(这里的•,对应Unicode U+2022)的域名转成了ASCII兼容格式,完全可以确定性逆向解码。
实现思路
- 识别页面中所有包含
xn--的Punycode域名链接; - 用浏览器原生的
punycode.toUnicode()方法解码Punycode,还原出带•的原始混淆域名; - 可选:把还原后的
•替换成.,得到正常可直接访问的域名; - 更新链接的
href属性和显示文本,让链接恢复正常跳转或可读性。
完整UserScript代码
// ==UserScript== // @name 还原论坛混淆的Punycode链接 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 把论坛转成Punycode的混淆链接还原成原始格式 // @author You // @match *://你的论坛域名/* // @grant none // ==/UserScript== (function() { 'use strict'; // 遍历页面所有含Punycode的链接 const links = document.querySelectorAll('a[href*="xn--"]'); links.forEach(link => { const href = link.href; // 提取Punycode域名部分 const punycodeMatch = href.match(/https?:\/\/(xn--[a-z0-9-]+)/i); if (punycodeMatch) { const punycodeDomain = punycodeMatch[1]; // 解码为Unicode格式(带•的原始域名) const unicodeDomain = punycode.toUnicode(punycodeDomain); // 替换•为.得到正常可访问域名(可选,按需保留或删除此行) const normalDomain = unicodeDomain.replace(/•/g, '.'); // 更新链接跳转地址 const newHref = href.replace(punycodeDomain, normalDomain); link.href = newHref; // 同步更新链接显示文本(如果原文本是Punycode) if (link.textContent.includes('xn--')) { link.textContent = link.textContent.replace(punycodeDomain, unicodeDomain); } } }); })();
注意事项
- 现代Chrome、Firefox、Edge浏览器都原生支持
punycodeAPI,旧浏览器可自行添加Punycode polyfill; - 务必修改
@match规则为你实际使用的论坛域名,比如*://forum.example.com/*,避免脚本在不必要的网站运行; - 若需要保留原始的
•格式,删除代码中生成normalDomain的行,直接用unicodeDomain更新href即可。
内容的提问来源于stack exchange,提问作者RJVB
相关产品推荐
相关产品推荐

