User:Polygnotus/Scripts/XC2.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/XC2.
// ExtendedConfirmedChecker.js // Adds indicators next to usernames on talk pages showing extended confirmed status // License: copyleft classExtendedConfirmedChecker{ constructor($,mw,window){ this.$=$; this.mw=mw; this.window=window; this.processedLinks=newSet(); this.userStatuses=null; // Define status indicators this.statusInfo={ extended:{ symbol:'✔', color:'#00a000', title:'Extended confirmed user' }, error:{ symbol:'?', color:'#666666', title:'Error checking status' }, blocked:{ symbol:'🚫', color:'#cc0000', title:'Blocked user' }, missing:{ symbol:'!', color:'#666666', title:'User not found' }, normal:{ symbol:'✘', color:'#cc0000', title:'Not extended confirmed' } }; // Advanced groups that imply extended confirmed status this.advancedGroups=newSet([ 'sysop',// Administrators 'bot',// Bots 'checkuser',// CheckUsers 'oversight',// Oversighters 'founder',// Founders 'steward',// Stewards 'staff',// Wikimedia staff 'bureaucrat',// Bureaucrats 'extendedconfirmed'// Explicitly extended confirmed ]); } asyncexecute(){ // Only run on talk pages constnamespace=this.mw.config.get('wgNamespaceNumber'); if(namespace%2!==1){ return; } // Load user statuses from cache first awaitthis.loadUserStatuses(); // Process links awaitthis.processPage(); } asyncgetWikitextFromCache(title){ constapi=newthis.mw.ForeignApi('https://en.wikipedia.org/w/api.php'); try{ constresponse=awaitapi.get({ action:'query', prop:'revisions', titles:title, rvslots:'*', rvprop:'content', formatversion:'2', uselang:'content', smaxage:'86400',// cache for 1 day maxage:'86400'// cache for 1 day }); returnresponse.query.pages[0].revisions[0].slots.main.content; }catch(error){ console.error('Error fetching wikitext:',error); returnnull; } } asyncloadUserStatuses(){ if(this.userStatuses){ return;// Already loaded } try{ // Try to fetch from NovemBot's user list first constdataString=awaitthis.getWikitextFromCache('User:NovemBot/userlist.js'); if(dataString){ constdataJSON=JSON.parse(dataString); this.userStatuses=newMap(); // Combine all advanced groups into 'extended' status constadvancedUsers={ ...dataJSON.sysop, ...dataJSON.bot, ...dataJSON.checkuser, ...dataJSON.steward, ...dataJSON.staff, ...dataJSON.bureaucrat, ...dataJSON.extendedconfirmed }; // Set status for all known users Object.keys(advancedUsers).forEach(username=>{ this.userStatuses.set(username,'extended'); }); // Cache the results this.saveToLocalStorage(this.userStatuses); }else{ // Fall back to localStorage if API fetch fails this.userStatuses=this.loadFromLocalStorage(); } }catch(error){ console.error('Error loading user statuses:',error); this.userStatuses=this.loadFromLocalStorage(); } } loadFromLocalStorage(){ try{ constcache=localStorage.getItem('ec-status-cache'); if(cache){ const{data,timestamp}=JSON.parse(cache); if(Date.now()-timestamp<24*60*60*1000){ returnnewMap(Object.entries(data)); } } }catch(error){ console.error('Error loading from localStorage:',error); } returnnewMap(); } saveToLocalStorage(statusMap){ try{ constcacheData={ data:Object.fromEntries(statusMap), timestamp:Date.now() }; localStorage.setItem('ec-status-cache',JSON.stringify(cacheData)); }catch(error){ console.error('Error saving to localStorage:',error); } } isSubpage(path){ constdecodedPath=decodeURIComponent(path); constcleanPath=decodedPath.split(/[?#]/)[0]; return/User:[^/]+\//.test(cleanPath); } findUserLinks(){ returnthis.$('#content a').filter((_,link)=>{ const$link=this.$(link); consthref=$link.attr('href'); // Basic checks if(!href||(!href.startsWith('/wiki/User:')&&!href.startsWith('/w/index.php?title=User:'))){ returnfalse; } // Skip already processed links if(this.processedLinks.has(link)){ returnfalse; } // Exclude talk pages and subpages if(href.includes('talk')||this.isSubpage(href)){ returnfalse; } returntrue; }); } getUsernameFromLink(link){ consthref=this.$(link).attr('href'); letmatch; if(href.startsWith('/wiki/')){ match=decodeURIComponent(href).match(/User:([^/?&#]+)/); }else{ consturl=newURL(href,window.location.origin); consttitle=url.searchParams.get('title'); if(title){ match=decodeURIComponent(title).match(/User:([^/?&#]+)/); } } if(match){ returnmatch[1].split('/')[0].replace(/_/g,' '); } returnnull; } addStatusIndicator(link,status){ const$link=this.$(link); // Remove any existing indicators $link.siblings('.ec-status-indicator').remove(); conststatusData=this.statusInfo[status]||this.statusInfo.normal; constindicator=this.$('<span>') .addClass('ec-status-indicator') .css({ 'margin-left':'4px', 'font-size':'0.85em', 'color':statusData.color, 'cursor':'help' }) .attr('title',statusData.title) .text(statusData.symbol); $link.after(indicator); this.processedLinks.add(link); } asyncprocessPage(){ constuserLinks=this.findUserLinks(); userLinks.each((_,link)=>{ constusername=this.getUsernameFromLink(link); if(username){ conststatus=this.userStatuses.get(username)||'normal'; this.addStatusIndicator(link,status); } }); } } // Initialize and run the checker $(()=>{ constchecker=newExtendedConfirmedChecker($,mw,window); // Run on page load and when new content is added checker.execute(); mw.hook('wikipage.content').add(()=>checker.execute()); });