Simkl inline tagging and memo taking

Adds inline-memos to Simkl for quick tagging

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Simkl inline tagging and memo taking
// @namespace    http://dannywhittaker.com
// @version      0.1
// @description  Adds inline-memos to Simkl for quick tagging
// @author       You
// @match        https://simkl.com/*
// @grant        none
// ==/UserScript==

$(document).ready(function () {
	var userId, loaded = false, userData = {};
	
	function refreshData() {
		var animeUrl;
		
		userId = /\/(\d+)\//.exec($('#SimklHeaderMenuProfile').find('a').attr('href'))[1];
		
		if (userId) {
			animeUrl = '/' + userId + '/anime/';
			
			$.ajax({
				url: animeUrl
			}).done(function (obj) {
				var doc = $(obj), results, i, mapped = {};
				
				results = doc.find('.SimklTVMyTableTR').map(function () {
					return {
						id: /\/(\d+)\//.exec($(this).find('.SimklTVMyTableTRTitle a').attr('href'))[1],
						memo: $(this).find('.SimklTVMyTableTRMemo').text().replace(/\+ Add/, '')
					};
				});
				
				for (i = 0; i < results.length; i++) {
					mapped[results[i].id] = results[i];
				}
				
				userData = mapped;
				loaded = true
			});
		}
	}
	
	function refreshUi() {
		var currentAnimeId, userAnime, originalDiv, newObj;
	
		function setMemo() {
			var val = $(this).val();
			
			$.ajax({
				url: '/ajax/full/my_list.php',
				type: 'POST',
				data: {
					action: 'editTag',
					id: currentAnimeId,
					text: val
				}
			}).fail(function () {
				console.error(arguments);
				alert('Failed to save memo');
			});
		}
	
		if (loaded) {
			currentAnimeId = /\/anime\/(\d+)\//.exec(window.location.href)[1];
			
			if (currentAnimeId) {
				userAnime = userData[currentAnimeId];
				
				if (userAnime) {
					originalDiv = $('.SimklTVBtnBigAddToWatchListDiv').parent();
					
					if (originalDiv && originalDiv.length && !originalDiv.next('.js-zallist-inline-memo-wrap').length) {
						newObj = $('<textarea style="display: block; width: 100%; height: 80px; padding: 0; border: none; box-sizing: border-box; margin-top: 1em; background-color: rgba(255, 255, 255, 0.8); resize: vertical;" class="js-zallist-inline-memo-wrap"></textarea>');
						newObj.val(userAnime.memo);
						newObj.change(setMemo);
						newObj.insertAfter(originalDiv);
					}
				}
			}
		}
	}
	
	window.setInterval(refreshData, 60 * 1000);
	window.setInterval(refreshUi, 1 * 1000);
	
	refreshData();
});