User:Shizhao/hashtagslink.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, 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 / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//Generate a hashtags link to https://hashtags.wmflabs.org/ in the edit summary
//ref https://github.com/WikipediaLibrary/hashtags/blob/master/scripts/common.py
$(function(){
	$('span.comment').each(function(){
		if (/(?:^|\s)[##]{1}(\w+)/.test($(this).text()) ) {
			matchs = $(this).text().matchAll(/(?:^|\s)[##]{1}(\w+)/g);
			Array.from(matchs, (res) =>  hashtag(this,res));
		}
	});
	function hashtag(arg,tag) {
		taghtml = '<a style="color:#1EAEDB;" href="//hashtags.wmflabs.org/?query='+tag[1]+'">'+tag[0]+'</a>';
		txt = $(arg).html().toString().replace(tag[0],taghtml);
		$(arg).html(txt);
	}
});