User:Polygnotus/Scripts/SourceTable2.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/SourceTable2.
// Only run on AfD edit pages if(mw.config.get('wgPageName').startsWith('Wikipedia:Articles_for_deletion/')&&mw.config.get('wgAction')==='edit'){ $(function(){ console.log('AfD script running, attempting to add button...'); // Try to find the exact structure we see in your HTML // h1#firstHeading with a span#firstHeadingTitle inside if($('#firstHeading').length){ $('<button>') .attr('id','generateSourceTableButton') .text('Generate Source Table') .css({ 'margin-left':'10px', 'margin-bottom':'5px', 'display':'inline-block', 'vertical-align':'middle', 'font-size':'15px', 'padding':'4px 8px' }) .click(function(e){ e.preventDefault(); generateSourceTable(); }) .appendTo('#firstHeading'); console.log('Button added to #firstHeading'); } // Fallback - try adding it to the edit form area elseif($('#editform').length){ $('<button>') .attr('id','generateSourceTableButton') .text('Generate Source Table') .css({ 'margin':'10px 0', 'display':'block' }) .click(function(e){ e.preventDefault(); generateSourceTable(); }) .prependTo('#editform'); console.log('Button added to #editform as fallback'); }else{ console.error('Could not find any suitable element to attach the button to'); } }); } functiongenerateSourceTable(){ // Extract article name from AfD page title varafdPageName=mw.config.get('wgPageName'); vararticleName=extractArticleName(afdPageName); console.log('Generating source table for article:',articleName); // Fetch article content newmw.Api().get({ action:'query', prop:'revisions', rvprop:'content', titles:articleName, formatversion:2 }).done(function(data){ try{ if(!data.query||!data.query.pages||data.query.pages.length===0){ mw.notify('Error: Could not retrieve article data.',{type:'error'}); console.error('API response did not contain expected data:',data); return; } varpage=data.query.pages[0]; if(page.missing){ mw.notify('The article "'+articleName+'" could not be found.',{type:'error'}); console.error('Article not found:',articleName); return; } if(!page.revisions||page.revisions.length===0){ mw.notify('No revisions found for the article.',{type:'error'}); console.error('No revisions found for:',articleName); return; } varpageContent=page.revisions[0].content; // Extract references varreferences=extractReferences(pageContent); if(references.length===0){ mw.notify('No references found in the article.',{type:'warning'}); console.warn('No references found in:',articleName); return; } vartableWikicode='\n\n{{User:Polygnotus/Templates/SourceAssessTable\n'; references.forEach(function(ref,index){ varnum=index+1; tableWikicode+='|src'+num+'='+ref+'\n'; tableWikicode+='|ind'+num+'=\n'; tableWikicode+='|rel'+num+'=\n'; tableWikicode+='|sig'+num+'=\n'; tableWikicode+='|comments'+num+'=\n'; }); tableWikicode+='}}'; // Get the edit textarea vareditTextarea=document.getElementById('wpTextbox1'); if(editTextarea){ editTextarea.value+=tableWikicode; $(editTextarea).trigger('change'); mw.notify('Source table added to the edit area. Please review and save your changes when ready.',{type:'success'}); console.log('Source table successfully added to edit area'); }else{ mw.notify('Failed to find the edit textarea.',{type:'error'}); console.error('Could not find edit textarea #wpTextbox1'); } }catch(error){ mw.notify('An error occurred: '+error.message,{type:'error'}); console.error('Error processing article data:',error); } }).fail(function(error){ mw.notify('Failed to fetch article content. Please try again.',{type:'error'}); console.error('API request failed:',error); }); } functionextractArticleName(afdPageName){ vararticleName=afdPageName.replace('Wikipedia:Articles_for_deletion/',''); // Handle nomination numbers (1st, 2nd, etc.) varnominationMatch=articleName.match(/(.*) \((\d+)(st|nd|rd|th) nomination\)$/); if(nominationMatch){ articleName=nominationMatch[1]; } console.log('Extracted article name:',articleName); returnarticleName; } functionextractReferences(content){ varreferences=[]; varindex=0; while(true){ varstartIndex=content.indexOf('<ref',index); if(startIndex===-1)break; varendIndex=content.indexOf('>',startIndex); if(endIndex===-1)break; vartagContent=content.substring(startIndex,endIndex+1); if(tagContent.endsWith('/>')){ index=endIndex+1; continue; } varrefEndIndex=content.indexOf('</ref>',endIndex); if(refEndIndex===-1)break; varrefContent=content.substring(endIndex+1,refEndIndex).trim(); // Replace {{|}} with | refContent=refContent.replace(/\{\{\!\}\}/g,'|'); references.push(refContent); index=refEndIndex+6; } console.log('Extracted',references.length,'references'); returnreferences; }