User:Aizag/common.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// ==UserScript==
// @name        Multi-language Ref Count using XTools
// @description Display reference count for each language version of the page using XTools
// @author      YourName
// @version     1.0
// @include     https://*.wikipedia.org/*
// ==/UserScript==

(function() {
    'use strict';

    // Get the language links from the page
    var languageLinks = document.querySelectorAll('#p-lang li.interlanguage-link > a');

    if (languageLinks.length === 0) {
        return;  // Exit if there are no language links
    }

    languageLinks.forEach(function(link) {
        // Get the page title and language code for each language
        var langCode = link.getAttribute('lang');
        var pageTitle = link.getAttribute('title').split(' – ')[0];  // Get the page title

        // Encode the page title for use in a URL
        var encodedPageTitle = encodeURIComponent(pageTitle);

        // Generate the XTools URL for this language version
        var xtoolsUrl = 'https://xtools.wmcloud.org/articleinfo/' + langCode + '.wikipedia.org/' + encodedPageTitle;

        // Fetch the XTools page and extract the reference count
        fetch(xtoolsUrl)
            .then(response => response.text())  // Get the response as text (HTML)
            .then(html => {
                var parser = new DOMParser();
                var doc = parser.parseFromString(html, 'text/html');
                
                // Extract the reference count from the XTools page
                var refCountElement = doc.querySelector('th:contains("References") + td');
                if (refCountElement) {
                    var refCount = refCountElement.textContent.trim();

                    // Create a <span> to display the reference count and append it to the language link
                    var refCountSpan = document.createElement('span');
                    refCountSpan.style.color = '#555';  // Set color for reference count
                    refCountSpan.style.marginLeft = '5px';  // Add some margin
                    refCountSpan.textContent = '(' + refCount + ')';
                    link.appendChild(refCountSpan);
                }
            })
            .catch(error => console.error('Failed to fetch reference count from XTools:', error));
    });
})();