לדלג לתוכן

משתמש:Idoiz/RandomSubCategory.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
// Define the category you want to get a random subcategory from
var category = 'קטגוריה:ויקינתונים - השוואת ערכים: חסרה תווית עברית';

// Make the API request to get subcategories
$.getJSON('https://he.wikipedia.org/w/api.php?action=query&format=json&origin=*&list=categorymembers&cmtitle=' + encodeURIComponent(category) + '&cmtype=subcat&cmlimit=max', function(data) {
    var subcategories = data.query.categorymembers;

    // Check if there are subcategories
    if (subcategories.length > 0) {
        // Get the total number of subcategories
        var totalSubcategories = subcategories.length;
        
        // Get a random subcategory
        var randomIndex = Math.floor(Math.random() * totalSubcategories);
        var randomSubcategory = subcategories[randomIndex];

        // Create the link to the subcategory, encoding only the spaces
        var link = 'https://he.wikipedia.org/wiki/' + randomSubcategory.title.replace(/ /g, '%20');
        
        // Print the information in the console
        console.log('Total Subcategories:', totalSubcategories);
        console.log('Chosen Subcategory Serial Number:', randomIndex + 1);
        console.log('Random Subcategory:', randomSubcategory.title);
        console.log('Link:', link);
    } else {
        console.log('No subcategories found under this category.');
    }
});