0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

愛しのべるさんの為に…♡

Last updated at Posted at 2024-09-04

使い方
① https://jp.nexon.com/mypage/point にアクセス
② F12
③ コンソールにペタリ (許可警告出たら allow pasting を打ち込んでください)
④ 勝手にページ追って計算されて、最後にポップアップ出力されます

適当に作成した物なので許してクレメンス
ちょっと修正しました 2024/09/27

point.png

こんな感じで表示されます。
一応何でチャージしたか表示もするようにしてます

※お支払い方法はページ毎の合計だから気にしないでね(titleで取得してるから…)

動作環境
chrome、Brave 確認済

修正後

async function fetchHTML(url) {
    const response = await fetch(url);
    return new DOMParser().parseFromString(await response.text(), 'text/html');
}

function formatNumber(number) {
    return number.toLocaleString(); // 数字をカンマで区切り
}

async function getChargesFromPage(doc) {
    const charges = Array.from(doc.querySelectorAll('td:nth-child(2)'))
                         .map(td => parseInt(td.innerText.replace(/,/g, '')) || 0);
    
    // `td class="title"` から支払い方法を取得
    const methods = Array.from(doc.querySelectorAll('td.title'))
                        .map(el => el.innerText.trim())
                        .filter(method => !method.startsWith('お支払い方法:')); // お支払い方法:を除外

    return { charges, methods };
}

async function calculateTotalCharges() {
    let totalCharges = {};
    let chargeCounts = {}; // 各支払い方法のチャージ回数をカウント
    let currentPage = 1;
    const baseUrl = window.location.href.split('?')[0];

    while (true) {
        const doc = await fetchHTML(`${baseUrl}?page=${currentPage}`);
        const { charges, methods } = await getChargesFromPage(doc);

        charges.forEach((charge, index) => {
            const method = methods[index];
            if (method) {
                if (!totalCharges[method]) {
                    totalCharges[method] = 0;
                    chargeCounts[method] = 0; // 初期化
                }
                totalCharges[method] += charge;
                chargeCounts[method] += 1;
            }
        });

        const pageTotal = charges.reduce((sum, point) => sum + point, 0);
        console.log(`ページ ${currentPage} の合計: ${formatNumber(pageTotal)} ポイント`);
        console.log(`ページ ${currentPage} の支払い方法別合計: `, 
            Object.fromEntries(
                Object.entries(totalCharges).map(([method, total]) => [method, formatNumber(total)])
            )
        );

        const nextPageLi = doc.querySelector('li.now + li a');
        if (nextPageLi) {
            currentPage++;
        } else {
            break;
        }
    }

    const grandTotal = Object.values(totalCharges).reduce((sum, point) => sum + point, 0);
    const resultMessage = `総課金額: ${formatNumber(grandTotal)} ポイント\n` +
                          `全ページの支払い方法別合計:\n` +
                          Object.entries(totalCharges)
                                .map(([method, total]) => 
                                    `${method}: ${formatNumber(total)} ポイント (回数: ${chargeCounts[method]})`
                                )
                                .join('\n');

    console.log(resultMessage);
    alert(resultMessage); // ポップアップで結果を表示
}

// 計算を開始
calculateTotalCharges();

修正前

async function fetchHTML(url) {
    const response = await fetch(url);
    return new DOMParser().parseFromString(await response.text(), 'text/html');
}

function formatNumber(number) {
    return number.toLocaleString(); // 数字をカンマで区切り
}

async function getChargesFromPage(doc) {
    const charges = Array.from(doc.querySelectorAll('td:nth-child(2)'))
                         .map(td => parseInt(td.innerText.replace(/,/g, '')) || 0);
    
    // `class="title"` から支払い方法を取得
    const methods = Array.from(doc.querySelectorAll('.title'))
                        .map(el => el.innerText.trim());

    return { charges, methods };
}

async function calculateTotalCharges() {
    let totalCharges = {};
    let chargeCounts = {}; // 各支払い方法のチャージ回数をカウント
    let currentPage = 1;
    const baseUrl = window.location.href.split('?')[0];

    while (true) {
        const doc = await fetchHTML(`${baseUrl}?page=${currentPage}`);
        const { charges, methods } = await getChargesFromPage(doc);

        charges.forEach((charge, index) => {
            const method = methods[index];
            if (method) {
                if (!totalCharges[method]) {
                    totalCharges[method] = 0;
                    chargeCounts[method] = 0; // 初期化
                }
                totalCharges[method] += charge;
                chargeCounts[method] += 1;
            }
        });

        const pageTotal = charges.reduce((sum, point) => sum + point, 0);
        console.log(`ページ ${currentPage} の合計: ${formatNumber(pageTotal)} ポイント`);
        console.log(`ページ ${currentPage} の支払い方法別合計: `, 
            Object.fromEntries(
                Object.entries(totalCharges).map(([method, total]) => [method, formatNumber(total)])
            )
        );

        const nextPageLi = doc.querySelector('li.now + li a');
        if (nextPageLi) {
            currentPage++;
        } else {
            break;
        }
    }

    const grandTotal = Object.values(totalCharges).reduce((sum, point) => sum + point, 0);
    const resultMessage = `総課金額: ${formatNumber(grandTotal)} ポイント\n` +
                          `全ページの支払い方法別合計:\n` +
                          Object.entries(totalCharges)
                                .map(([method, total]) => 
                                    `${method}: ${formatNumber(total)} ポイント (回数: ${chargeCounts[method]})`
                                )
                                .join('\n');

    console.log(resultMessage);
    alert(resultMessage); // ポップアップで結果を表示
}

// 計算を開始
calculateTotalCharges();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?