Ratio Calculator

Calculate ratio as a percentage between two HTML elements and display the result.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Ratio Calculator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Calculate ratio as a percentage between two HTML elements and display the result.
// @author       Bolt Again
// @match        https://www.pixiv.net/dashboard/works*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function calculateRatio() {
        var elements = document.querySelectorAll('.sc-a16hc8-4.jBjeDE');

        for (var i = 0; i < elements.length; i++) {
            var parentElement = elements[i];
            var bookmarkElement = parentElement.querySelector('a[title="Bookmarks"]');
            var viewsElement = parentElement.querySelector('a[title="Views"]');

            var bookmarkValue = bookmarkElement.querySelector('span.sc-1tt9fda-1.dkkGqt').textContent;
            var viewsValue = viewsElement.querySelector('span.sc-1tt9fda-1.dkkGqt').textContent;

            var ratio = Math.round((parseNumber(bookmarkValue) / parseNumber(viewsValue)) * 100);

            var ratioElement = document.createElement('a');
            ratioElement.title = 'ratio';
            ratioElement.className = bookmarkElement.className;
            ratioElement.href = bookmarkElement.href;

            var innerDiv = document.createElement('div');
            innerDiv.className = bookmarkElement.querySelector('div.sc-bQCEYZ.irmCui').className;

            var innerDivContent = `
                <div width="16" class="${bookmarkElement.querySelector('div.sc-eEVmNe.ggcxgL').className}">
                    <div class="${bookmarkElement.querySelector('div.sc-fmdNqN.hyACbC').className}">
                        <pixiv-icon name="${bookmarkElement.querySelector('pixiv-icon').getAttribute('name')}" class="${bookmarkElement.querySelector('pixiv-icon').className}"></pixiv-icon>
                    </div>
                </div>
                <div class="${bookmarkElement.querySelector('div.sc-fXgAZx.fwvUqi').className}">
                    <span class="${bookmarkElement.querySelector('span.sc-1tt9fda-1.dkkGqt').className}">${ratio}%</span>
                </div>`;

            innerDiv.innerHTML = innerDivContent;
            ratioElement.appendChild(innerDiv);

            parentElement.appendChild(ratioElement);
        }
    }

    function parseNumber(value) {
        return parseFloat(value.replace(/,/g, ''));
    }

    // Apply custom styles
    GM_addStyle(`
        .sc-a16hc8-4.jBjeDE a[title="ratio"] span {
            color: green !important;
            font-weight: bold !important;
        }
    `);

    function waitForElementToLoad() {
        var targetElement = document.querySelector('nav.sc-xhhh7v-0.kYtoqc');

        if (targetElement) {
            setTimeout(function() {
                calculateRatio();
            }, 1000);
        } else {
            setTimeout(waitForElementToLoad, 100);
        }
    }

    waitForElementToLoad();
})();