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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// AUTOMATIC ARTICLE LEAD IMAGE//// Description: Adds a lead image to biographies that don't have lead images.$(()=>{// Check if ran.if(window.autoLeadImage){return;}// Set running.window.autoLeadImage=true;// This API key is IP-restricted, so this script won't work for other people.constgoogleApiKey='AIzaSyBuXFJn7vNI1JYqe1GQx2i-JhH4sI7JGKk';// eslint-disable-next-line complexityfunctioninit(){constenableAutomaticLeadImage=()=>{if(window.mw.config.get('wgCanonicalNamespace')===''&&window.mw.config.get('wgAction')==='view'&&window.mw.util.getParamValue('disable')!=='leadimage'){returntrue;}returnfalse;};constisPrintable=Boolean(window.___location.href.match(/&printable=yes/));if(!enableAutomaticLeadImage()||isPrintable){returnfalse;}letisAPerson=false;// eslint-disable-next-line no-restricted-syntaxfor(constcategoryofwindow.mw.config.get('wgCategories')){constcat=category.replace(/_/g,' ');if(cat.indexOf('Living people')===0||cat.indexOf(' births')!==-1||cat.indexOf(' deaths')!==-1){isAPerson=true;break;}}if(!isAPerson||findLeadImages()===1){returnfalse;}constpersonName=window.mw.config.get('wgTitle');consturl=`https://www.googleapis.com/customsearch/v1?q=${personName}`+'&cx=004227430873773175363:j7_jwozfw80&imgType=face&num=1&'+`searchType=image&key=${googleApiKey}`;// Get the search results.return$.get(url,(json)=>{// Get the first item's info.constimageTitle=json.items[0].title;constimageLink=json.items[0].link;constimageThumbnail=json.items[0].image.thumbnailLink;// If image does not exist, then show the Google-cached thumbnail instead.return$.ajax({url:imageLink,error:()=>imageResults([imageTitle,imageThumbnail]),success:()=>imageResults([imageTitle,imageLink]),});// We failed to get an image, so just show a placeholder.}).fail(()=>imageResults(['Example','https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png',]),);}// eslint-disable-next-line complexity, max-statementsfunctionimageResults(results){letaddAfter;letcolSpan;letjumpToNav;letnewNode;letnewNodeDiv;letparentNode;constdefaultLeadImageSize=200;// The title of the infobox, if there is one.constfn=$('.fn').first();constinfobox=findLeadImages();constimageUrl=decodeURI(results[1]);// there is no infobox and no lead imageif(infobox===-1){// insert an image after the "difference" boxif($('#difference').length){jumpToNav=$('#diffHeading').next();// insert an image into the lead}else{jumpToNav=$('.mw-content-ltr:eq(0)').children().first();// eslint-disable-next-line max-depthif(jumpToNav[0].nodeName!=='P'){jumpToNav=jumpToNav.nextAll('p').first();}}// everything else after here has parentNode as a TR// in the infobox, there is no name formatted with microformats}elseif(!fn.length){parentNode=infobox.children().first().children().first();colSpan=parentNode.children().first().attr('colspan');addAfter=parentNode;// there is a name formatted with microformats, in either TH or TD}elseif(fn[0].nodeName==='TH'||fn[0].nodeName==='TD'){parentNode=fn.parent();colSpan=fn.attr('colspan');addAfter=parentNode;// there is a name formatted with microformats, in a CAPTION}elseif(fn[0].nodeName==='CAPTION'){parentNode=fn.siblings().last().children().first();colSpan=parentNode.children().first().attr('colspan');addAfter=parentNode.prev();if(colSpan<=1){colSpan=2;}// similar to the above, but it is the parent node}elseif(fn.parent()[0].nodeName==='CAPTION'){consttable=fn.closest('table');parentNode=table.find('tr').first();colSpan=table.find('th').first().attr('colspan');addAfter=$();// standard infobox with no microformats}else{parentNode=fn.parent().parent();if(parentNode[0].nodeName!=='TR'){parentNode=parentNode.parent();}colSpan=fn.parent().attr('colspan');addAfter=parentNode;}constnewNodeImage=$(`<a class="image" href="${imageUrl}"><img alt="${results[0]}" src="${imageUrl}" style="width: ${defaultLeadImageSize}px;" /></a>`,);constnewNodePreLink=$('<span>From </span>');constnewNodeLink=$(`<a href="http://images.google.com/images?q=${window.mw.config.get('wgTitle',)}">Google Images</a>`,);// there is no infobox and no lead imageif(infobox===-1){constimagePath='http://bits.wikimedia.org/skins-1.5/common/images/magnify-clip.png';constmagnify=$('<div class="magnify"><a title="" class="internal" '+`href="${imageUrl}"><img width="15" height="11" alt="" `+`src="${imagePath}"></a></div>`,);constnewNodeCaption=$('<div class="thumbcaption"></div>').append(magnify).append(newNodePreLink).append(newNodeLink);newNodeImage.addClass('thumbimage');newNodeDiv=$(`<div class="thumbinner" style="width: ${defaultLeadImageSize+2}px;"></div>`,).append(newNodeImage).append(newNodeCaption);newNode=$('<div class="thumb tright"></div>').append(newNodeDiv);returnjumpToNav.before(newNode);// there is an infobox with no image}constnewNodeSpan=$('<span style="font-size: 95%;"></span>').append(newNodePreLink).append(newNodeLink);newNodeDiv=$('<div></div>').append(newNodeImage).append('<br />').append(newNodeSpan);constnewNodeChild=$(`<td colspan="${colSpan}" style="text-align: center;"></td>`,).append(newNodeDiv);newNode=$('<tr></tr>').append(newNodeChild);if(addAfter.length){returnaddAfter.after(newNode);}returnparentNode.before(newNode);}// eslint-disable-next-line complexityfunctionfindLeadImages(){constdefaultImageNames=['File:Replace_this_image_male.svg','File:Replace_this_image_female.svg',];constinfoboxes=$('.infobox');consttocColors=$('.toccolours');letinfoboxImages=false;// get images in infoboxesif(infoboxes.length){const$images=$('.image',infoboxes.first());// Remove default images.$images.each((index,element)=>{const$image=$(element);if(defaultImageNames.indexOf($image.attr('href'))>-1){$image.remove();}});// Running this again because we may have removed some images.infoboxImages=$('.image',infoboxes.first());}elseif(tocColors.length){infoboxImages=$('.image',tocColors.first());}// there is more than one image in infoboxesif(infoboxImages.length){return1;}// there is an infobox with no imageif(infoboxImages.length===0){returninfoboxes.first();}// There are no infoboxes. Check if we are viewing a diff firstletnode;if($('table.diff').length){node=$('h2.diff-currentversion-title:eq(0)').next();}else{node=$('.mw-content-ltr:eq(0)').children().first();}while(node.length&&node.attr('id')!=='section-1'&&!node.hasClass('printfooter')){// there is a lead imageif((node[0].nodeName==='P'||node[0].nodeName==='DIV')&&(node.children().first().hasClass('image')||(node.children().eq(1)&&node.children().first().hasClass('thumbinner')))){return1;}node=node.next();}// there are no lead imagesreturn-1;}init();});