বিষয়বস্তুতে চলুন

ব্যবহারকারী:R1F4T/das.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন।
  • অপেরা: Ctrl-F5 টিপুন।
// Function to debounce input events (wait until user stops typing)
function debounce(func, wait) {
    var timeout;
    return function () {
        var context = this, args = arguments;
        clearTimeout(timeout);
        timeout = setTimeout(function () {
            func.apply(context, args);
        }, wait);
    };
}

// Function to copy text to clipboard
function copyToClipboard(text) {
    var tempInput = document.createElement("textarea");
    tempInput.style.position = "absolute";
    tempInput.style.left = "-9999px";
    tempInput.value = text;
    document.body.appendChild(tempInput);
    tempInput.select();
    document.execCommand("copy");
    document.body.removeChild(tempInput);
    alert('Copied to clipboard!');  // Alert the user that the text was copied
}

// Function to convert Arabic numbers to Bengali numbers
function arabicToBengaliNumber(num) {
    const bengaliDigits = ['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯'];
    return num.toString().split('').map(digit => bengaliDigits[digit]).join('');
}

// Function to fetch category members from the main namespace using the MediaWiki API
function fetchCategoryMembers(category) {
    return new Promise(function (resolve, reject) {
        var apiUrl = "https://bn.wikipedia.org/w/api.php";
        var params = {
            action: "query",
            list: "categorymembers",
            cmtitle: "Category:" + category,
            cmlimit: "max",
            cmnamespace: "14",  // Only pages in the main namespace (namespace 0)
            format: "json",
            origin: "*"
        };

        $.ajax({
            url: apiUrl,
            data: params,
            dataType: "json",
            success: function (data) {
                var pages = data.query.categorymembers.map(function (page) {
                    return page.title;
                });
                resolve(pages);  // Resolve the promise with the list of pages
            },
            error: function (err) {
                console.error("Error fetching category pages for:", category, err);
                reject(err);
            }
        });
    });
}

// Function to find common pages in multiple categories
function findCommonPages(categories) {
    var categoryPromises = categories.map(fetchCategoryMembers); // Fetch pages for each category

    // Use Promise.all to wait for all category fetches to complete
    Promise.all(categoryPromises).then(function (results) {
        // Find the common pages across all categories
        var commonPages = results.reduce(function (accumulator, current) {
            // If it's the first iteration, return the first category's pages
            if (!accumulator) return current;
            // Filter the pages that exist in both the accumulator and the current category
            return accumulator.filter(function (page) {
                return current.includes(page);
            });
        }, null);  // Start accumulator as null, so the first iteration uses the first category's pages

        // Generate and display the wikitable with the common pages
        var tableCode = '{| class="wikitable"\n! ক্রমিক !! পৃষ্ঠার নাম\n';
        if (commonPages && commonPages.length > 0) {
            commonPages.forEach(function (pageTitle, index) {
                tableCode += '|-\n| ' + arabicToBengaliNumber(index + 1) + ' || [[' + pageTitle + ']]\n';
            });
        } else {
            tableCode += '|-\n| colspan="2" | কোনো সাধারণ পৃষ্ঠাগুলি পাওয়া যায়নি\n';  // No common pages found
        }
        tableCode += '|}';
        tableTextInput.setValue(tableCode);  // Display the wikitable code
    }).catch(function (error) {
        console.error("Error fetching common pages:", error);
    });
}

// Function to create and show the OOUI modal with category input box, optimized for mobile and full-screen
function createOOUIModal() {
    // Create a simple text input box for entering categories (comma-separated)
    var categoryInput = new OO.ui.MultilineTextInputWidget({
        placeholder: 'শ্রেণীগুলির নাম কমা দ্বারা পৃথক করে লিখুন', // Placeholder in Bengali
        value: '',
        classes: ['mobile-friendly-input']  // Add custom class for mobile styling
    });

    // Create textarea for displaying the wikitable code
    tableTextInput = new OO.ui.MultilineTextInputWidget({
        rows: 15, // Height of the text area
        placeholder: 'উইকিটেবিল কোড এখানে দেখানো হবে', // Placeholder in Bengali
        readonly: true,
        autosize: true,
        classes: ['mobile-friendly-textarea']  // Add custom class for mobile styling
    });

    // Create submit button to find and display common pages in the categories
    var fetchButton = new OO.ui.ButtonWidget({
        label: 'সাধারণ পৃষ্ঠাগুলি পান', // "Get Common Pages" in Bengali
        flags: ['progressive'],
        classes: ['mobile-friendly-button']  // Add custom class for mobile styling
    });

    // Create copy button to copy the wikitable code to the clipboard
    var copyButton = new OO.ui.ButtonWidget({
        label: 'কপি করুন', // "Copy" in Bengali
        flags: ['primary'],
        classes: ['mobile-friendly-button']  // Add custom class for mobile styling
    });

    // Create close button
    var closeButton = new OO.ui.ButtonWidget({
        label: 'বন্ধ করুন', // "Close" in Bengali
        flags: ['destructive'],
        classes: ['mobile-friendly-button']  // Add custom class for mobile styling
    });

    // Create a field layout for input fields
    var fieldLayout = new OO.ui.FieldLayout(categoryInput, {
        label: 'শ্রেণীগুলির নাম (কমা দ্বারা পৃথক):',
        align: 'top'
    });

    // Create a panel to hold the buttons and input fields
    var panel = new OO.ui.PanelLayout({
        padded: true,
        expanded: false,
        classes: ['mobile-friendly-panel', 'full-screen-panel']  // Custom class for full-screen styling
    });

    panel.$element.append(
        fieldLayout.$element,
        fetchButton.$element,
        copyButton.$element,  // Add the copy button here
        tableTextInput.$element,
        closeButton.$element
    );

    // Create a dialog window using OOUI
    var modalWindow = new OO.ui.MessageDialog();
    var windowManager = new OO.ui.WindowManager({
        classes: ['full-screen-window']  // Custom class to make the modal full-screen
    });

    $('body').append(windowManager.$element);
    windowManager.addWindows([modalWindow]);

    // When the fetch button is clicked, find and display common pages
    fetchButton.on('click', function () {
        var categories = categoryInput.getValue().split(',').map(function (category) {
            return category.trim();  // Split the input by commas and trim whitespace
        });
        if (categories.length > 0) {
            findCommonPages(categories);  // Find common pages in the categories
        }
    });

    // When the copy button is clicked, copy the content of the wikitable to the clipboard
    copyButton.on('click', function () {
        var textToCopy = tableTextInput.getValue();  // Get the content of the wikitable
        if (textToCopy) {
            copyToClipboard(textToCopy);  // Copy the content to the clipboard
        }
    });

    // When the close button is clicked, close the modal
    closeButton.on('click', function () {
        windowManager.closeWindow(modalWindow);
    });

    // Open the modal when the link is clicked, with full-screen layout
    windowManager.openWindow(modalWindow, {
        title: 'সাধারণ পৃষ্ঠাসমূহের তালিকা', // Modal title in Bengali
        message: panel.$element
    });
}

// Add portlet link to Wikipedia's personal tools menu
function addPortletLinkWithOOUI() {
    mw.util.addPortletLink(
        'p-personal', // Where to place the link, e.g., 'p-personal' for the personal tools menu
        '#',          // The URL, in this case '#' since we don't navigate anywhere
        'সাধারণ পৃষ্ঠাসমূহের তালিকা', // Text for the link in Bengali
        'pt-category-pages',   // ID for the link element
        'শ্রেণীগুলিতে সাধারণ পৃষ্ঠাগুলি তালিকাভুক্ত করুন', // Tooltip text in Bengali
        null,          // Access key
        null           // Position of the link
    );

    // Bind the click event to open the modal when the link is clicked
    $('#pt-category-pages').on('click', function (e) {
        e.preventDefault();
        createOOUIModal(); // Create and open the OOUI modal with category input
    });
}

// Run the code when the page is loaded
$(document).ready(function () {
    addPortletLinkWithOOUI(); // Add the portlet link with OOUI modal
});