پرش به محتوا

مدیاویکی:Gadget-NewPagesFeed.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Jeeputer
انتقال مجدد نوار ناوبری پایین صفحه به جای خودش
imported>Jeeputer
تمیزکاری، کلاس‌بندی و افزودن توضیحات به متن کد
خط ۱: خط ۱:
$.when(mw.loader.using(['mediawiki.api', 'mediawiki.util']), $.ready)
$.when(mw.loader.using(['mediawiki.api', 'mediawiki.util']), $.ready)
.then(function() {
.then(function() {
mw.loader.load('https://fa.wikipedia.org/w/index.php?title=User:Jeeputer/newPagesFeed.css&action=raw&ctype=text/css', 'text/css');
var api = new mw.Api();
var api = new mw.Api();
function parsePage(id) {
function parsePage(id) {
خط ۱۰: خط ۱۱:
        var cats = data.parse.categories.length;
        var cats = data.parse.categories.length;
        var langlinks = data.parse.langlinks.length;
        var langlinks = data.parse.langlinks.length;
        var catColor = cats ? '' : '#b12727';
        var categoriesCount = (cats) ? mw.language.convertNumber(cats) : 'فاقد';
        var categoriesCount = (cats > 0) ? mw.language.convertNumber(cats) : 'صفر';
        var langLinksCount = (langlinks) ? mw.language.convertNumber(langlinks) : 'فاقد';
        var langLinksCount = (langlinks > 0) ? mw.language.convertNumber(langlinks) : 'صفر';
        // Replace "در حال تجزیهٔ صفحه…" with categories and langlinks count
        $('#cat-iw-container-' + id).fadeOut(
        $('#cat-iw-container-' + id).fadeOut(
            1000,
            1000,
            function() {
            function() {
                $('#cat-iw-container-' + id).empty();
                $('#cat-iw-container-' + id).empty();
                $('#cat-iw-container-' + id).append(
                $('#cat-iw-container-' + id)
                            '<span style="color: ' + catColor + '">' + categoriesCount + ' رده</span> • ' +
.attr('title', 'اطلاعات رده‌ها و میان‌ویکی‌ها')
                            langLinksCount + ' میان‌ویکی');
                .append(
                            '<span id="npf-cat-count-' + id + '">' + categoriesCount + ' رده</span> • ' +
                            langLinksCount + ' میان‌ویکی');
                        // Font color = red if we have no categories
                        if (cats < 1) $('#npf-cat-count-' + id).addClass('npf-nocat');
                        // Show updated content
                $('#cat-iw-container-' + id).fadeIn(1000);
                $('#cat-iw-container-' + id).fadeIn(1000);
            });
            });
    });
    });
}
}
// Utility function to create container div for each row
    function createRow(content) {
        var $row = $('<div>').addClass('npf-row').append(content);
        return $row;
    }
   
function createBox(ingredients) {
function createBox(ingredients) {
    function createRow(content) {
    var htmlID = 'npf-box-' + ingredients.revid; // Unique ID (e.g. npf-header-39298943) for each article box
        var $row = $('<div>').css('margin', '12px 16px 0').append(content);
        return $row;
    }
    var htmlID = 'npf-' + ingredients.title;
    var bgcolor = ingredients.notPatrolled ? '#ededed' : '';
    var $box = $('<div>')
    var $box = $('<div>')
        .css({
        .attr('id', htmlID)
            'margin': '12px 0px 16px',
        .addClass('npf-article-box');
            'border': '1px solid #a2a9b1',
       
            'padding-bottom': '12px',
        if (ingredients.notPatrolled) { // Gray background for non-patrolled pages
            'background': bgcolor
        $box.addClass('npf-notpatrolled')
        })
        .attr('title', 'مقالهٔ گشت نخورده');
        .attr('id', htmlID);
        }
    var $header = $('<div id="npf-header-' + ingredients.revid + '" style="display: flex;">')
       
    var $header = $('<div>')
    .attr('id', 'npf-header-' + ingredients.revid)
    .addClass('npf-flex-row')
        .append(
        .append(
            $('<a>')
            $('<a>')
                .attr({'href': mw.util.getUrl(ingredients.title)})
                .attr({
                .css({
                'href': mw.util.getUrl(ingredients.title),
                    'margin-left': '5px',
                'title': ingredients.title
                    'font-weight': 'bold',
                })
                    'diplay': 'block'
                .addClass('npf-header-title')
                })
                .text(ingredients.title)
                .text(ingredients.title)
        )
        )
                 .append(
                 .append(
                    '<a href="' + mw.util.getUrl('Special:PageHistory/' + ingredients.title) +
                $('<span>')
                    '" style="font-size:90%; margin-left: 5px;">(تاریخچه)</a>'
                .addClass('npf-hist-link')
                .append('(')
                .append(
                $('<a>')
                .attr({
                'href': mw.util.getUrl('Special:PageHistory/' + ingredients.title),
                'title': 'تاریخچهٔ صفحهٔ ' + ingredients.title
            })
                .text('تاریخچه')
            )
            .append(')')
                 )
                 )
        .append('<span style="margin-left: 8px; display: block;"> · ' + ingredients.length + ' بایت</span>')
        .append($('<span>')
        .append('<span style="text-align: left; margin-right: auto; display: block;"><a href="' +
        .text(' · ' + ingredients.bytes + ' بایت')
        mw.util.getUrl('Special:Permalink/' + ingredients.revid) + '">' + ingredients.date + '</a></span>'
        .attr('title', 'حجم صفحه')
        .addClass('npf-row-content-right'))
        .append($('<span>')
        .addClass('npf-row-content-left')
        .append($('<a>')
        .attr({
        'href': mw.util.getUrl('Special:Permalink/' + ingredients.revid),
        'title': 'پیوند دائمی به نخستین نسخهٔ صفحه'
        })
        .text(ingredients.date)
        )
    );
    );
    var $details = $('<div id="npf-details-' + ingredients.revid + '" style="display: flex;">')
    var $details = $('<div>')
        .append($('<span style="margin-left: 8px; display: block;">')
    .attr('id', 'npf-details-' + ingredients.revid)
        .append($('<span>').append(
    .addClass('npf-flex-row')
        'ایجاد شده توسط <a style="display: inline; padding: 0.2em" ' +
        .append($('<span>')
        (ingredients.newUser ? 'class="new"': '') + ' href="' + mw.util.getUrl('کاربر:' +
        .attr('title', 'ایجاد کنندهٔ صفحه')
        ingredients.creator) + '">' + ingredients.creator + '</a>'
        .addClass('npf-row-content-right')
        .append($('<span>')
        .append('ایجاد شده توسط ')
        .append($('<a>')
        .addClass('npf-creator' + (ingredients.newUser ? ' new' : ''))
        .attr('href', mw.util.getUrl('کاربر:' + ingredients.creator))
        .text(ingredients.creator)
        )
        )
    )
    )
        .append($(ingredients.userTools))
        .append($(ingredients.userTools))
    )
    )
        .append('<span id="cat-iw-container-' + ingredients.revid + '" style="text-align: left; margin-right: auto; display: block;">در حال تجزیهٔ صفحه…');
        .append($('<span>')
        .attr({
        'id': 'cat-iw-container-' + ingredients.revid,
        'title': 'لطفاً صبر کنید…'
        })
        .addClass('npf-row-content-left')
        .text('در حال تجزیهٔ صفحه…')
        );
    var commentText = (ingredients.comment.length > 100) ? ingredients.comment.substring(0, 100) + '…' : ingredients.comment;
    var commentText = (ingredients.comment.length > 100)
    var $comment = $('<div id="npf-comment-' + ingredients.revid + '" style="display: flex;">')
    ? ingredients.comment.substring(0, 100) + '…'
    : ingredients.comment;
   
    var $comment = $('<div>')
    .attr('id', 'npf-comment-' + ingredients.revid)
    .addClass('npf-flex-row')
        .append($('<span>')
        .append($('<span>')
        .attr('title', 'خلاصهٔ ویرایش در زمان ایجاد صفحه')
            .append('خلاصهٔ ویرایش: ')
            .append('خلاصهٔ ویرایش: ')
            .append($('<span>')
            .append($('<span>')
                .css('font-style', 'italic')
                .addClass('npf-italic')
                .text(commentText)
                .text(commentText)
            )
            )
خط ۸۵: خط ۱۳۴:
    $box.append(createRow($comment));
    $box.append(createRow($comment));
     
     
    // Append tag container only if we have some tags
    if (ingredients.tags && ingredients.tags.length > 0) {
    if (ingredients.tags && ingredients.tags.length > 0) {
        var $tagElements = $('<span>');
        var $tagElements = $('<span>');
خط ۹۲: خط ۱۴۲:
        var $tags = $('<div>')
        var $tags = $('<div>')
            .attr('id', 'npf-tags-' + ingredients.revid)
            .attr('id', 'npf-tags-' + ingredients.revid)
            .css({'display': 'flex', 'font-size': '85%'})
            .addClass('npf-tags-container')
            .append($('<span>').css('display', 'block')
            .append($('<span>')
            .attr('title', 'برچسب‌های نصب شده بر نخستین ویرایش')
                .append('برچسب‌ها: ')
                .append('برچسب‌ها: ')
                .append($tagElements)
                .append($tagElements)
خط ۹۹: خط ۱۵۰:
        $box.append(createRow($tags));
        $box.append(createRow($tags));
    }
    }
   
    $('#npf-parent-container').append($box);
    $('#npf-parent-container').append($box);
    $('.mw-pager-navigation-bar').eq(1).insertAfter($box);
    $('.mw-pager-navigation-bar').eq(1).insertAfter($box); // Move navigation bar to its initial location
              
              
}
}
var listItems = $('#mw-content-text > ul > li'),  
var listItems = $('#mw-content-text > ul > li'),  
    boxIngredients = [],
    boxIngredients = [], // Will be populated by information from list items without any API calls
    i;
    i;
for (i = 0; i < listItems.length; i++) {
for (i = 0; i < listItems.length; i++) {
    var hasTags = $(listItems[i])[0].children[8];
    var hasTags = $(listItems[i])[0].children[8]; // ...children[8] is always for edit tags
    boxIngredients[i] = {
    boxIngredients[i] = {
        notPatrolled: !!$(listItems[i]).hasClass('not-patrolled'),
        notPatrolled: !!$(listItems[i]).hasClass('not-patrolled'),
خط ۱۱۳: خط ۱۶۵:
        url: $(listItems[i])[0].children[1].href,
        url: $(listItems[i])[0].children[1].href,
        date: $(listItems[i])[0].children[0].innerText,
        date: $(listItems[i])[0].children[0].innerText,
        revid: $($(listItems[i])[0]).attr('data-mw-revid'),
        revid: $($(listItems[i])[0]).attr('data-mw-revid'), // Revision ID is available as "data-mw-revid" attribute for each list item
        length: ($(listItems[i])[0].children[4].innerText).replace(/\[(.*) بایت\]/, '$1'),
        bytes: ($(listItems[i])[0].children[4].innerText).replace(/\[(.*) بایت\]/, '$1'),
        creator: $(listItems[i])[0].children[5].innerText,
        creator: $(listItems[i])[0].children[5].innerText,
        userTools: $(listItems[i])[0].children[6],
        userTools: $(listItems[i])[0].children[6],
خط ۱۲۳: خط ۱۷۵:
        for (var ei = 0; ei < $(listItems[i])[0].children[8].children.length; ei++) {
        for (var ei = 0; ei < $(listItems[i])[0].children[8].children.length; ei++) {
            var $element = $($(listItems[i])[0].children[8].children[ei]);
            var $element = $($(listItems[i])[0].children[8].children[ei]);
            if ($element.hasClass('mw-tag-marker')) {
            if ($element.hasClass('mw-tag-marker')) { // Prevent pushing "برچسب‌ها" to array
            // Store tags as jquery object so we can add
            // them to the box with without modifications
                boxIngredients[i].tags.push($element);
                boxIngredients[i].tags.push($element);
            }
            }
خط ۱۳۳: خط ۱۸۷:
}
}
$('#mw-content-text > ul').remove();
$('#mw-content-text > ul').remove();
         $('#mw-content-text').append('<div id="npf-parent-container"></div>');
         $('#mw-content-text').append('<div id="npf-parent-container"></div>'); // Append main parent container
for (i = 0; i < boxIngredients.length; i++) {
for (i = 0; i < boxIngredients.length; i++) {
    createBox(boxIngredients[i]);
    createBox(boxIngredients[i]); // Create boxes
    parsePage(boxIngredients[i].revid);
    parsePage(boxIngredients[i].revid); // Update categories and langlinks after we have the api response
}
}
});
});

نسخهٔ ۱۵ آوریل ۲۰۲۴، ساعت ۱۷:۳۲

$.when(mw.loader.using(['mediawiki.api', 'mediawiki.util']), $.ready)
	.then(function() {
		mw.loader.load('https://fa.wikipedia.org/w/index.php?title=User:Jeeputer/newPagesFeed.css&action=raw&ctype=text/css', 'text/css');
		var api = new mw.Api();
		function parsePage(id) {
		    return api.get({
		    	action: 'parse',
		    	oldid: id,
		    	format: 'json'
		    }).done(function(data) {
		        var cats = data.parse.categories.length;
		        var langlinks = data.parse.langlinks.length;
		        var categoriesCount = (cats) ? mw.language.convertNumber(cats) : 'فاقد';
		        var langLinksCount = (langlinks) ? mw.language.convertNumber(langlinks) : 'فاقد';
		        // Replace "در حال تجزیهٔ صفحه…" with categories and langlinks count
		        $('#cat-iw-container-' + id).fadeOut(
		            1000,
		            function() {
		                $('#cat-iw-container-' + id).empty();
		                $('#cat-iw-container-' + id)
							.attr('title', 'اطلاعات رده‌ها و میان‌ویکی‌ها')
			                .append(
	                            '<span id="npf-cat-count-' + id + '">' + categoriesCount + ' رده</span> • ' +
	                            langLinksCount + ' میان‌ویکی');
                        // Font color = red if we have no categories
                        if (cats < 1) $('#npf-cat-count-' + id).addClass('npf-nocat');
                        // Show updated content
		                $('#cat-iw-container-' + id).fadeIn(1000);
		            });
		    });
		}
		
		// Utility function to create container div for each row
	    function createRow(content) {
	        var $row = $('<div>').addClass('npf-row').append(content);
	        return $row;
	    }
	    
		function createBox(ingredients) {
		    var htmlID = 'npf-box-' + ingredients.revid;						// Unique ID (e.g. npf-header-39298943) for each article box
		    var $box = $('<div>')
		        .attr('id', htmlID)
		        .addClass('npf-article-box');
		        
	        if (ingredients.notPatrolled) {										// Gray background for non-patrolled pages
	        	$box.addClass('npf-notpatrolled')
	        	.attr('title', 'مقالهٔ گشت نخورده');
	        }
	        
		    var $header = $('<div>')
		    	.attr('id', 'npf-header-' + ingredients.revid)
		    	.addClass('npf-flex-row')
		        .append(
		            $('<a>')
	                .attr({
	                	'href': mw.util.getUrl(ingredients.title),
	                	'title': ingredients.title
	                })
	                .addClass('npf-header-title')
	                .text(ingredients.title)
		        )
                .append(
                	$('<span>')
                	.addClass('npf-hist-link')
                	.append('(')
                	.append(
	                	$('<a>')
                		.attr({
                			'href': mw.util.getUrl('Special:PageHistory/' + ingredients.title),
                			'title': 'تاریخچهٔ صفحهٔ ' + ingredients.title
            			})
                		.text('تاریخچه')
            		)
            		.append(')')
                )
		        .append($('<span>')
		        	.text(' · ' + ingredients.bytes + ' بایت')
		        	.attr('title', 'حجم صفحه')
		        	.addClass('npf-row-content-right'))
		        .append($('<span>')
		        	.addClass('npf-row-content-left')
		        	.append($('<a>')
		        		.attr({
		        			'href': mw.util.getUrl('Special:Permalink/' + ingredients.revid),
		        			'title': 'پیوند دائمی به نخستین نسخهٔ صفحه'
		        		})
		        		.text(ingredients.date)
	        		)
		    	);
		
		    var $details = $('<div>')
		    	.attr('id', 'npf-details-' + ingredients.revid)
		    	.addClass('npf-flex-row')
		        .append($('<span>')
		        	.attr('title', 'ایجاد کنندهٔ صفحه')
		        	.addClass('npf-row-content-right')
			        .append($('<span>')
			        	.append('ایجاد شده توسط ')
			        	.append($('<a>')
			        		.addClass('npf-creator' + (ingredients.newUser ? ' new' : ''))
				        	.attr('href', mw.util.getUrl('کاربر:' + ingredients.creator))
				        	.text(ingredients.creator)
			        	)
			    	)
		        	.append($(ingredients.userTools))
		    	)
		        .append($('<span>')
		        	.attr({
		        		'id': 'cat-iw-container-' + ingredients.revid,
		        		'title': 'لطفاً صبر کنید…'
		        	})
		        	.addClass('npf-row-content-left')
		        	.text('در حال تجزیهٔ صفحه…')
	        	);
		
		    var commentText = (ingredients.comment.length > 100)
		    	? ingredients.comment.substring(0, 100) + '…'
		    	: ingredients.comment;
		    	
		    var $comment = $('<div>')
		    	.attr('id', 'npf-comment-' + ingredients.revid)
		    	.addClass('npf-flex-row')
		        .append($('<span>')
		        	.attr('title', 'خلاصهٔ ویرایش در زمان ایجاد صفحه')
		            .append('خلاصهٔ ویرایش: ')
		            .append($('<span>')
		                .addClass('npf-italic')
		                .text(commentText)
		            )
		        );
		    
		    $box.append(createRow($header));
		    $box.append(createRow($details));
		    $box.append(createRow($comment));
		    
		    // Append tag container only if we have some tags
		    if (ingredients.tags && ingredients.tags.length > 0) {
		        var $tagElements = $('<span>');
		        for (var t = 0; t < ingredients.tags.length; t++) {
		            $tagElements.append($(ingredients.tags[t]));
		        }
		        var $tags = $('<div>')
		            .attr('id', 'npf-tags-' + ingredients.revid)
		            .addClass('npf-tags-container')
		            .append($('<span>')
		            	.attr('title', 'برچسب‌های نصب شده بر نخستین ویرایش')
		                .append('برچسب‌ها: ')
		                .append($tagElements)
		            );
		        $box.append(createRow($tags));
		    }
		    
		    $('#npf-parent-container').append($box);
		    $('.mw-pager-navigation-bar').eq(1).insertAfter($box);				// Move navigation bar to its initial location
            
		}
		var listItems = $('#mw-content-text > ul > li'), 
		    boxIngredients = [], // Will be populated by information from list items without any API calls
		    i;
		for (i = 0; i < listItems.length; i++) {
		    var hasTags = $(listItems[i])[0].children[8];						// ...children[8] is always for edit tags
		    boxIngredients[i] = {
		        notPatrolled: !!$(listItems[i]).hasClass('not-patrolled'),
		        title: $(listItems[i])[0].children[1].title,
		        url: $(listItems[i])[0].children[1].href,
		        date: $(listItems[i])[0].children[0].innerText,
		        revid: $($(listItems[i])[0]).attr('data-mw-revid'),				// Revision ID is available as "data-mw-revid" attribute for each list item
		        bytes: ($(listItems[i])[0].children[4].innerText).replace(/\[(.*) بایت\]/, '$1'),
		        creator: $(listItems[i])[0].children[5].innerText,
		        userTools: $(listItems[i])[0].children[6],
		        comment: ($(listItems[i])[0].children[7].innerText)
		    };
		    if (hasTags) { // We have some tags
		        boxIngredients[i].tags = [];
		        for (var ei = 0; ei < $(listItems[i])[0].children[8].children.length; ei++) {
		            var $element = $($(listItems[i])[0].children[8].children[ei]);
		            if ($element.hasClass('mw-tag-marker')) {					// Prevent pushing "برچسب‌ها" to array
		            	// Store tags as jquery object so we can add
		            	// them to the box with without modifications
		                boxIngredients[i].tags.push($element);
		            }
		        }
		    }
		    if ($($(listItems[i])[0].children[5]).hasClass('new')) {
		    	boxIngredients[i].newUser = true;
		    }
		}
		$('#mw-content-text > ul').remove();
        $('#mw-content-text').append('<div id="npf-parent-container"></div>');	// Append main parent container
		for (i = 0; i < boxIngredients.length; i++) {
		    createBox(boxIngredients[i]);										// Create boxes
		    parsePage(boxIngredients[i].revid);									// Update categories and langlinks after we have the api response
		}
});