پرش به محتوا

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

از دانشنامه علوم انسانی
imported>Jeeputer
سازگاری با ظاهر جدید ویژه‌صفحهٔ صفحه‌های تازه؛ دستور کوتاه کردن خلاصه ویرایش را هم حذف کردم چون برای حفظ پیوندهای درون متن خلاصه لازم است کل عنصر خلاصه به‌طور کامل چسبانده شود (درجا)
imported>Jeeputer
افزودن قابلیت واکشی اطلاعات ساعن (درجا)
خط ۸۶: خط ۸۶:
            });
            });
    });
    });
}
function oresize(id) {
var ORESapi = 'https://ores.wikimedia.org/v3/scores/fawiki/?models=damaging&revids=' + id;
        return fetch(ORESapi).then(function(response) {
return response.json();
}).then(function(data) {
var isDamaging = data.fawiki.scores[id].damaging.score.prediction;
                if (isDamaging) {
                    $('#npf-ores-' + id).show();
                }
});
}
}
خط ۱۰۸: خط ۱۲۰:
    .attr('id', 'npf-header-' + ingredients.revid)
    .attr('id', 'npf-header-' + ingredients.revid)
    .addClass('npf-flex-row')
    .addClass('npf-flex-row')
        .append(
                .append($('<span>')
            $('<a>')
                    .addClass('npf-row-content-right')
                .attr({
                    .css('display', 'inline-flex')
                'href': mw.util.getUrl(ingredients.title),
                    .append($('<span>')
                'title': ingredients.title
                        .css('display', 'inline-flex')
                })
                        .append(
                .addClass('npf-header-title')
                    $('<a>')
                .text(ingredients.title)
                        .attr({
        )
                        'href': mw.util.getUrl(ingredients.title),
                .append(
                        'title': ingredients.title
                $('<span>')
                        })
                .addClass('npf-hist-link')
                        .addClass('npf-header-title')
                .append('(')
                        .text(ingredients.title)
                .append(
                )
                $('<a>')
                        .append($('<span>')
                .attr({
                            .addClass('npf-ores-indicator npf-blinking')
                'href': mw.util.getUrl('Special:PageHistory/' + ingredients.title),
                            .attr({
                'title': 'تاریخچهٔ صفحهٔ ' + ingredients.title
                                'id': 'npf-ores-' + ingredients.firstrevid,
            })
                                'title': 'بالقوه خرابکارانه'
                .text('تاریخچه')
                            })
            )
                            .css('display', 'none')
            .append(')')
                            .text('⚠')
                        )
                    )
                    .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', 'حجم صفحه')
                    )
                 )
                 )
        .append($('<span>')
        .text(' · ' + ingredients.bytes + ' بایت')
        .attr('title', 'حجم صفحه')
        .addClass('npf-row-content-right')
        )
        .append($('<span>')
        .append($('<span>')
        .addClass('npf-row-content-left')
        .addClass('npf-row-content-left')
        .append($('<a>')
        .append($('<a>')
        .attr({
        .attr({
        'href': mw.util.getUrl('Special:Permalink/' + ingredients.permalink),
        'href': mw.util.getUrl('Special:Permalink/' + ingredients.firstrevid),
        'title': 'پیوند دائمی به نخستین نسخهٔ صفحه'
        'title': 'پیوند دائمی به نخستین نسخهٔ صفحه'
        })
        })
خط ۲۲۱: خط ۲۴۸:
        date: $(listItems[i])[0].children[1].innerText,
        date: $(listItems[i])[0].children[1].innerText,
        revid: $($(listItems[i])[0]).attr('data-mw-revid'), // Latest revision's ID is available as "data-mw-revid" attribute for each list item
        revid: $($(listItems[i])[0]).attr('data-mw-revid'), // Latest revision's ID is available as "data-mw-revid" attribute for each list item
        permalink: $(listItems[i])[0].children[1].href.match(/\oldid\=(\d*)/)[1], // First revision's ID
        firstrevid: $(listItems[i])[0].children[1].href.match(/\oldid\=(\d*)/)[1], // First revision's ID
                 bytes: ($(listItems[i])[0].children[5].innerText).replace(/\[(.*) بایت\]/, '$1'),
                 bytes: ($(listItems[i])[0].children[5].innerText).replace(/\[(.*) بایت\]/, '$1'),
        creator: $(listItems[i])[0].children[6].innerText,
        creator: $(listItems[i])[0].children[6].innerText,
خط ۲۴۷: خط ۲۷۴:
    createBox(boxIngredients[i]); // Create boxes
    createBox(boxIngredients[i]); // Create boxes
    parsePage(boxIngredients[i].revid); // Update categories and langlinks after we have the api response
    parsePage(boxIngredients[i].revid); // Update categories and langlinks after we have the api response
    oresize(boxIngredients[i].firstrevid);
}
}
});
});

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

$.when(mw.loader.using(['mediawiki.api', 'mediawiki.util']), $.ready)
	.then(function() {
		
		if (mw.config.get('wgCanonicalSpecialPageName') !== "Newpages") return;
		
		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;
		        for (var c = 0; c < cats.length; c++) {
		        	var catName = cats[c]['*'];
		        	if (catName === 'مقاله‌های_نامزد_حذف_سریع') {
		        		var $csdElement = $('<span>')
		        				.attr('title', 'این صفحه نامزد حذف سریع شده است')
		        				.css({
                                    'display': 'none',
                                    'width': '150px'
                                })
		        				.addClass('npf-row-content-left npf-csd')
		        				.text('نامزد حذف سریع');
		        		$('div#npf-comment-' + id).append($csdElement);
		        		$csdElement.toggle(2000);
		        	} else if (catName === 'صفحه‌های_حذف_زمان‌دار') {
		        		var $prodElement = $('<span>')
		        				.attr('title', 'این صفحه نامزد حذف زمان‌دار شده است')
		        				.css('display', 'none')
		        				.addClass('npf-row-content-left npf-csd')
		        				.text('نامزد حذف زمان‌دار');
		        		$('div#npf-comment-' + id).append($prodElement);
		        		$prodElement.toggle(2000);
		        	} else if (catName === 'مقاله‌های_نامزد_حذف') {
		    			var afdUrl = $(data.parse.text['*']).find('a:contains("نظرخواهی مربوط به این صفحه")')[0].href;
		        		var $afdElement = $('<a>')
		        				.attr({
		        					'title': 'این صفحه در نظرخواهی برای حذف نامزد است',
		        					'href': afdUrl
		        				})
		        				.css('display', 'none')
		        				.addClass('npf-row-content-left npf-afd')
		        				.text('نظرخواهی برای حذف');
		        		$('div#npf-comment-' + id).append($afdElement);
		        		$afdElement.toggle(2000);
		        	}
		        }
		        var contentCats = cats.filter(function(cat) {
		        	return typeof(cat.hidden) !== "string";
		        });
		        var langlinks = data.parse.langlinks || 0;
                var enLangLink = false;
                if (langlinks) {
	                for (var li = 0; li < langlinks.length; li++) {
	                    if (langlinks[li].lang === 'en') {
	                    	// [ url, English title ]
	                        enLangLink = [langlinks[li].url, langlinks[li]['*']];
	                        break;
	                    }
	                }
                }
		        var categoriesCount = contentCats.length ? mw.language.convertNumber(contentCats.length) : 'فاقد';
		        var langLinksCount = langlinks.length ? mw.language.convertNumber(langlinks.length) : 'فاقد';
		        // 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 + ' میان‌ویکی' +
	                            // Append English language link if exists
	                            (enLangLink
	                            	? ' (<a href="' + enLangLink[0] + '" title="' + enLangLink[1] + '">en</a>)'
	                            	: ''
                            	)
                        	);
                        // Font color = red if we have no categories
                        if (contentCats.length < 1) $('#npf-cat-count-' + id).addClass('npf-nocat');
                        // Show updated content
		                $('#cat-iw-container-' + id).fadeIn(1000);
		            });
		    });
		}
		
		function oresize(id) {
			var ORESapi = 'https://ores.wikimedia.org/v3/scores/fawiki/?models=damaging&revids=' + id;
	        return fetch(ORESapi).then(function(response) {
				return response.json();
			}).then(function(data) {
				var isDamaging = data.fawiki.scores[id].damaging.score.prediction;
                if (isDamaging) {
                    $('#npf-ores-' + id).show();
                }
			});
		}
		
		// 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($('<span>')
                    .addClass('npf-row-content-right')
                    .css('display', 'inline-flex')
                    .append($('<span>')
                        .css('display', 'inline-flex')
                        .append(
        		            $('<a>')
        	                .attr({
        	                	'href': mw.util.getUrl(ingredients.title),
        	                	'title': ingredients.title
        	                })
        	                .addClass('npf-header-title')
        	                .text(ingredients.title)
        		        )
                        .append($('<span>')
                            .addClass('npf-ores-indicator npf-blinking')
                            .attr({
                                'id': 'npf-ores-' + ingredients.firstrevid,
                                'title': 'بالقوه خرابکارانه'
                            })
                            .css('display', 'none')
                            .text('⚠')
                        )
                    )
                    .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', 'حجم صفحه')
                    )
                )
		        .append($('<span>')
		        	.addClass('npf-row-content-left')
		        	.append($('<a>')
		        		.attr({
		        			'href': mw.util.getUrl('Special:Permalink/' + ingredients.firstrevid),
		        			'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 $comment = $('<div>')
		    	.attr('id', 'npf-comment-' + ingredients.revid)
		    	.addClass('npf-flex-row')
		        .append($('<span>')
		        	.addClass('npf-row-content-right')
		        	.attr('title', 'خلاصهٔ ویرایش در زمان ایجاد صفحه')
		            .append('خلاصهٔ ویرایش: ')
		            .append($('<span>')
		                .addClass('npf-italic')
		                .append($(ingredients.comment))
                    )
		        );
		    
		    $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 > section.mw-pager-body > 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[9];						// ...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[1].innerText,
		        revid: $($(listItems[i])[0]).attr('data-mw-revid'),				// Latest revision's ID is available as "data-mw-revid" attribute for each list item
		        firstrevid: $(listItems[i])[0].children[1].href.match(/\oldid\=(\d*)/)[1], // First revision's ID
                bytes: ($(listItems[i])[0].children[5].innerText).replace(/\[(.*) بایت\]/, '$1'),
		        creator: $(listItems[i])[0].children[6].innerText,
		        userTools: $(listItems[i])[0].children[7],
		        comment: ($(listItems[i])[0].children[8])
		    };
		    if (hasTags) { // We have some tags
		        boxIngredients[i].tags = [];
		        for (var ei = 0; ei < $(listItems[i])[0].children[9].children.length; ei++) {
		            var $element = $($(listItems[i])[0].children[9].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[6]).hasClass('new')) {
		    	boxIngredients[i].newUser = true;
		    }
		}
		$('#mw-content-text section.mw-pager-body > ul').remove();
        $('#mw-content-text section.mw-pager-body').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
		    oresize(boxIngredients[i].firstrevid);
		}
});