一个编码EUC-JP的网站使用GM_xmlhttpRequest请求后,使用encoding.min.js转码后结果不能正常显示
直接请求结果显示乱码,请求结果转码为EUC-JP显示乱码,请求二进制结果,转化为EUC-JP编码,以及再转化为UTF8均显示乱码,如何将结果正常显示日文文字
⚠️#NSFW#
function convertBufferToEucJp(buffer) {
// Convert buffer to Uint8Array
const uint8Array = new Uint8Array(buffer);
// Convert Uint8Array to string with UTF-8 encoding (assuming the buffer is UTF-8)
const utf8String = new TextDecoder("utf-8").decode(uint8Array);
// Convert UTF-8 string to EUC-JP
const eucJpString = Encoding.convert(utf8String, {
to: 'EUCJP',
from: 'UTF8'
});
return eucJpString;
}
GM_xmlhttpRequest({
method: "GET",
url: "https://seesaawiki.jp/av_video/d/%c6%e1%b2%ec%ba%ea%a4%e6%a4%ad%a4%cd",
responseType: "arraybuffer", // Get response as ArrayBuffer
onload: function(response) {
if (response.status >= 200 && response.status < 300) {
// Convert ArrayBuffer to EUC-JP string
const eucJpString = convertBufferToEucJp(response.response);
// Do something with the EUC-JP string
console.log(eucJpString);
} else {
console.error("Error fetching data:", response.status);
}
},
onerror: function(error) {
console.error("Request error:", error);
},
});
试了一下,把解码函数换成这个就可以了
参考来源: