User:Dixtosa/skipToPost.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// Description  : Makes links from 'User contributions' and 'Watchlist' pages that are posts in one of the discussion pages (GP, TR, etc.)
//                go directly to the actual post
// Requirements : Timezone should be set correctly in prefs
// Limitations  : Does not work for the posts that have more than 1 minute difference between
//                signature time and official timestamp for the edit
// Todo         : identify posts by section name too
// Dependencies : ES6, momentjs module

/* jshint maxerr:1048576, strict:true, undef:true, latedef:true, sub:true */
/* global mw, $, moment */

// <nowiki>
if (mw.config.values.wgAction == 'view')
$(function(){
	//let enabledOnPagesRegex = /^Wiktionary:/;

	if (['Contributions'].indexOf(mw.config.values.wgCanonicalSpecialPageName) > -1)
	mw.loader.using(["moment","user.options"], function () {
		let userDateFormat = {
			"dmy":"HH:mm, D MMMM YYYY",
			"mdy":"HH:mm, MMMM D, YYYY",
			"ymd":"HH:mm, YYYY MMMM D",
			"ISO 8601":"YYYY-MM-DDTHH:mm:ss",
			"default":"HH:mm, D MMMM YYYY" //XXX: what if default changes?
		}[mw.user.options.values.date];

		$(".mw-contributions-list li a.mw-contributions-title").on("click auxclick", function(){
			let editTimestamp = moment($(this).parent().find("a.mw-changeslist-date").text(), userDateFormat);
			let signature = editTimestamp.utc().format("HH:mm, D MMMM YYYY");
			let signature_secondary = editTimestamp.subtract(1, "minutes").utc().format("HH:mm, D MMMM YYYY");
			localStorage["WT:skipToPost.js:signature-primary"] = signature;
			localStorage["WT:skipToPost.js:signature-secondary"] = signature_secondary;
		});
	});

	if (['Watchlist'].indexOf(mw.config.values.wgCanonicalSpecialPageName) > -1)
	mw.loader.using("moment", function(){
		$(".mw-changeslist li .mw-title a").on("click auxclick", function(){
			let elem = $(this);
			let timestamp = elem.closest("li.mw-changeslist-line").data("mw-ts");
			let editTimestamp = moment.utc(timestamp, "YYYYMMDDHHmmss");// "20170515172157";
			let signature = editTimestamp.format("HH:mm, D MMMM YYYY");
			let signature_secondary = editTimestamp.subtract(1, "minutes").format("HH:mm, D MMMM YYYY");
			localStorage["WT:skipToPost.js:signature-primary"] = signature;
			localStorage["WT:skipToPost.js:signature-secondary"] = signature_secondary;
		});
	});


	//is talk or Wiktionary
	if (!(mw.config.values.wgNamespaceNumber == 4 || mw.config.values.wgNamespaceNumber % 2 == 1)) return;
	var timestampToSearch = localStorage["WT:skipToPost.js:signature-primary"];
	var timestampToSearch_secondary = localStorage["WT:skipToPost.js:signature-secondary"];
	if (timestampToSearch)
	{
		var jquery_selectors = [
			`p:contains('${timestampToSearch}'),li:contains('${timestampToSearch}'),dd:contains('${timestampToSearch}')`,
			`p:contains('${timestampToSearch_secondary}'),li:contains('${timestampToSearch_secondary}'),dd:contains('${timestampToSearch_secondary}')`,
			`span[title='${timestampToSearch} (UTC)'].localcomments`,
			`span[title='${timestampToSearch_secondary} (UTC)'].localcomments`
		];
		
		let post = [];
		for (let i = 0; i < jquery_selectors.length; i++) {
			post = $(jquery_selectors[i]).last(); //XXX:sloppy
			if (post.length !== 0)
			{
				if(post.is('span.localcomments')) post = post.closest("p,li,dd"); //if the UTC changing gadget is turned on
				post.contents()
					//.filter((_, elem) => !$(elem).find(":contains(' (UTC)')").length > 0)
					.filter((_, elem) => !$(elem).is("dl")) //much faster also works with the gadget
					.wrap("<span class='lamedixtosa'>");
				
				//post.prevUntil("dd:has(dl)")
				//	.wrap("<span class='lamerdixtosa'></span>");
				// also need to check if contains UTC or title={UTC} for a gadget-aware version
				
				mw.util.addCSS(".SkipToPostJs-highlighted-post-part{color: green; border: lime 1px solid; border-radius: 10px;}");
				$(".lamedixtosa").wrapAll("<div id='SkipToPostJs-highlighted-post' class='SkipToPostJs-highlighted-post-part'/>");
				$(".lamerdixtosa").wrapAll("<div class='SkipToPostJs-highlighted-post-part' />");
				window.location.hash="#SkipToPostJs-highlighted-post";
				break;
			}
		}
		if (post.length === 0)
			mw.notify(`skipToPost.js: timestamp not found on the page (${timestampToSearch})`);
		delete localStorage["WT:skipToPost.js:signature-primary"];
		delete localStorage["WT:skipToPost.js:signature-secondary"];
	}
});
// </nowiki>