RosettaCode Conribution Records Counter

count specific li elements within section.mw-pager-body

当前为 2024-07-06 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         RosettaCode Conribution Records Counter
// @namespace    http://tampermonkey.net/
// @version      1.0.2
// @description  count specific li elements within section.mw-pager-body
// @author       aspen138
// @match        *://rosettacode.org/wiki/Special:Contributions?end=&limit=5000&namespace=all&start=&tagfilter=&target=*
// @match        *://rosettacode.org/wiki/Special:Contributions*
// @grant        none
// @license    MIT
// ==/UserScript==


(function() {
    'use strict';

    function countAndDisplayDistinctTitles() {
        // Find the target section
        var section = document.querySelector('section.mw-pager-body');
        if (!section) return;

        // Collect all title attributes from 'a' elements
        var liElements = section.querySelectorAll('a.mw-contributions-title');
        var titlesSet = new Set();
        liElements.forEach(function(aElement) {
            var title = aElement.getAttribute('title');
            if (title && !title.startsWith('User:') && !title.startsWith('File:')) {
                titlesSet.add(title);
            }
        });
        console.log('titlesSet=',titlesSet);

        // Count distinct titles
        var countDistinctTitles = titlesSet.size;

        // Create a new element to display the count of distinct titles
        var resultDisplay = document.createElement('div');
        resultDisplay.textContent = `(RosettaCode Contribution Records Counter) Count of distinct titles: ${countDistinctTitles}`;
        resultDisplay.style.backgroundColor = 'yellow'; // Set the background color to yellow
        resultDisplay.style.color = 'blue'; // Optional: Set text color to blue for better readability
        resultDisplay.style.padding = '10px'; // Optional: Add some padding
        resultDisplay.style.marginBottom = '10px'; // Optional: Add some margin at the bottom
        resultDisplay.style.border = "2px solid black";

        // Insert the results as the first child of the section
        if (section.firstChild) {
            section.insertBefore(resultDisplay, section.firstChild);
        } else {
            section.appendChild(resultDisplay);
        }
    }

    // Assuming this function is called when the document is fully loaded
    window.addEventListener('load', countAndDisplayDistinctTitles);
})();