MediaWiki:Common.js/RedditWidget.js

(function {   "use-strict";    mw.hook('wikipage.content').add(function($elem) { var enableWidget = document.getElementById("reddit-widget"); if (enableWidget) { fetch('https://www.reddit.com/r/thepromisedneverland.json') .then(function(res) {                   return res.json;                }).then(function(json) {                    var post = json.data.children;                    var config = {                        subreddit: post[0].data.subreddit                    };                    //BIGWRAPPER                    var $embed_wrap = $('');

//HEADER var $embed_header = $(' ').append([                       $('', { style: 'margin:5px;', href: 'https://www.reddit.com/r/' + config.subreddit + '/', html: '' }),                       "links from ",                        $('', { style: 'text-decoration:none;color:#336699', href: 'https://www.reddit.com/r/' + config.subreddit + '/', text: config.subreddit + '.reddit.com' })                   ]);

//CONTENTS var $content_wrap = $(' ');

//Set up left and right column as defined by reddit var $content_inner = [ $(' '),                       $(' ')                    ];

//Set up class names var class_half = ["first-half", "second-half"]; var even_odd = ["even", "odd"];

//Loop through all entries for (var i = 0; i < post.length; i++) { //Change focus to e(lement) var e = post[i].data; //Check to see if left or right side var inner_select = 0; if (i > post.length / 2) { inner_select = 1; }                       //Make Block var div = $(' ', {                           class: [                                "reddit-link",                                even_odd[i % 2],                                class_half[inner_select],                                "thing",                                "id-" + e.name                            ].join(' '),                            html: [                                $('', { href: e.url, class: "reddit-voting-arrows", target: "_blank", style: "float:left; display:block;", html: '' }),                               $(' ', {                                    class: 'reddit-entry entry unvoted', style: "margin-left: 28px; min-height:32px;", html: [ '' + e.link_flair_text + ' ', '' + e.title + '', '  ' + e.score + ' points | ' + e.num_comments + ' comments ', ' '                                   ]                                })                            ]                        });                        //Add Block to column $content_inner[inner_select].append(div); }                   //Add final clear $content_inner[1].append(' ');

//Add HEADER and CONTENTS to BIGWRAPPER with a rembeddit-content wrap $embed_wrap.append([                       $embed_header,                        $(' ')                            .append($content_wrap.append($content_inner))                    ]); $(enableWidget).append($embed_wrap); }).catch(console.error);       }    }); });