A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

Use the ‘Today in History’ line on my website

yolinfl

Hi there, I would like to use the ‘Today in History’ block which is shown on the main Wikipedia page, on my genealogy website. Is this possible? If so, what code do I use?

Tgr

See the feed/onthisday API.

yolinfl

Thank you Tgr, but unfortunately I don’t have the knowledge to create this. I thought it could be done with a more simple code/script in my HTML.
Thank you for your effort!

Tgr

It is pretty simple, e.g.

var date = new Date().toISOString();
$target = $( 'div#target' );
$.get( 'https://en.wikipedia.org/api/rest_v1/feed/onthisday/selected/' + date.substr( 5, 2 ) + '/' + date.substr( 8, 2 ) ).then( function ( data ) {
    $.each( data.selected, function ( _, event ) {
        var $linkedText = $( '<a>' ).text( event.text );
        $.each( event.pages, function( _, page ) {
            if ( page.isTopic ) {
                $linkedText.attr( {
                    title: page.displaytitle,
                    href: page.content_urls.desktop.page
                } );
                return false;
            }
        } );
        $target.append( event.year + ': ',  $linkedText, '<br />' );
    } );
} );
yolinfl

Thank you for that code, Tgr!

But (ahum) … where… do I put it?

Tgr

Probably something like

<html>
<head>
    ...
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script>
       ...your code here...
    </script>
</head>
<body>
    <div id="target"></div>
    ...your webpage here...
</body>
</html>

but it really depends on what you are using for, what tooling you are using for your site etc.

If you were looking for some kind of preformatted embeddable content, like an iframe, such a thing does not exist (although there are long-term plans to provide it).

yolinfl

Thank you. I’ll try it a.s.a.p.!