0

I get the httprequest result from a URL whose charset is EUC-JP and shows Garbled characters.

I tried to get the Buffer result and convert it to EUC-JP, but the result still shows garbled characters.

How to get the UTF8 result?

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-08-25
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/encoding.min.js

// ==/UserScript==
    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);
        },
    });
1
  • Why are you trying to convert from UTF8 to EUCJP, should that not be the other way around? Commented Sep 9, 2024 at 8:12

1 Answer 1

0

Convert the ArrayBuffer result from EUCJP to UNICODE, which shows normally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.