User:Narmdo/common.js

From A Township Tale Wiki
Jump to navigation Jump to search

In other languages: Deutsch • Español • Français • Русский


CSS and Javascript changes must comply with the wiki design rules.


Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
var pageContent = mw.hook('wikipage.content');

function map($class, $function)
{
	pageContent.add(function($content) {
	    var $items = $content.find($class);
	    if (!$items.length) return;
	
	    $items.each(function() {
	    	var $this = $(this);
	    	
	    	$function($this);
	    });
	});
}

map('.att-news', function($div)
{
	var blogApi = "https://strapi.townshiptale.dev/graphql?query={posts(limit:7,%20sort:%22date:desc%22){title,date,slug}}&format=txt&format=txt&origin=*";

	$div = $div.children(":first");
	    		
	fetch(blogApi)
	    .then(function(response) {return response.json();})
	    .then(function(response) {
	    	var posts = response.data.posts;
	    	
	    	$div.children('.rss_item').remove();
	    	
	    	posts.forEach(function($post)
	    	{
	    		var itemHtml = '<div class="rss_item"><div class="plainlinks"><a target="_blank" rel="nofollow noreferrer noopener" class="external text" href="https://townshiptale.com/news/' +
	    		$post.slug + 
	    		'">' + 
	    		$post.title + 
	    		'</a></div><div class="date_epoch">' + 
	    		$post.date + 
	    		'</div></div></div>';
	    		
	    		$div.append(itemHtml);
	    	});
	     });
});