User:Polygnotus/Scripts/ColonCategory.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/ColonCategory.
// Wikipedia search script for articles containing [[:Category: // This uses the MediaWiki API to search and save results to a file (asyncfunction(){ constsearchQuery='insource:/\\[\\[:Category:/'; constallResults=[]; letcontinueToken=null; console.log('Starting search for articles containing [[:Category:'); // Function to perform API search with continuation asyncfunctionsearchWikipedia(sroffset=0){ constparams=newURLSearchParams({ action:'query', list:'search', srsearch:searchQuery, srnamespace:'0',// Main namespace srlimit:'500',// Maximum allowed per request format:'json', origin:'*' }); if(sroffset>0){ params.append('sroffset',sroffset); } consturl=`https://en.wikipedia.org/w/api.php?${params.toString()}`; try{ constresponse=awaitfetch(url,{ headers:{ 'User-Agent':'ColonCategory.js/1.0)' } }); constdata=awaitresponse.json(); returndata; }catch(error){ console.error('API request failed:',error); returnnull; } } // Paginate through all results letoffset=0; lettotalFetched=0; while(true){ constdata=awaitsearchWikipedia(offset); if(!data||!data.query||!data.query.search){ console.log('No more results or error occurred'); break; } constresults=data.query.search; allResults.push(...results.map(r=>r.title)); totalFetched+=results.length; console.log(`Fetched ${totalFetched} articles so far...`); // Check if there are more results if(data.continue&&data.continue.sroffset){ offset=data.continue.sroffset; // Add delay to avoid hammering the API awaitnewPromise(resolve=>setTimeout(resolve,1000)); }else{ break; } } console.log(`Total articles found: ${allResults.length}`); // Create text file content constfileContent=allResults.join('\n'); // Create and download the file constblob=newBlob([fileContent],{type:'text/plain'}); consturl=URL.createObjectURL(blob); consta=document.createElement('a'); a.href=url; a.download='wikipedia-category-links-'+newDate().toISOString().split('T')[0]+'.txt'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); console.log('File download initiated'); console.log(`Saved ${allResults.length} article titles to file`); })();