Jump to content

User:DTLHS/exclude.js

From Wiktionary, the free dictionary

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • 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.


$(function () {
	var location = window.location.toString();
	if (!location.toString().includes("User:DTLHS/word_tracking")) {
		return;
	}

	var wordStatus = {};
	
	function modifyText(text, wordStatus) {
		var result = [];
		text.split("\n").forEach(function(item) {
			if (item.startsWith("# {{User:DTLHS/tracking_line|")) {
				var parts = item.split("|");
				var word = parts[2];
				if (word in wordStatus) {
					parts[3] = "excluded=" + wordStatus[word] + "}}";
					result.push(parts.join("|"))
				} else {
					result.push(item);
				}
			} else {
				result.push(item);
			}
		});
		return result.join("\n");
	}

	function include_exclude_click(event) {
		event.preventDefault();

		var el = event.target;
		var inner = el.innerText;
		if (inner == "exclude") {
			el.innerText = "include";
			var i = el.parentElement.parentElement.getElementsByTagName("i")[0];

			var str = document.createElement("s");
			i.parentNode.insertBefore(str, i);
			str.appendChild(i);
			var word = i.innerText;

			wordStatus[word] = "False";
		} else {
			el.innerText = "exclude";

			var i = el.parentElement.parentElement.getElementsByTagName("i")[0];
			var str = i.parentElement;
			str.outerHTML = str.innerHTML;

			var word = i.innerText;

			
		}
		editor.addEdit({
	        'edit': function(text) {
	        	wordStatus[word] = "True";
	        	return modifyText(text, wordStatus);
	        },
	        'redo': function () {
			},
			'undo': function () {
			},
	        'summary': 'update excluded words'
	    });

	}

	$('.toggle_include_exclude').each(function () {
		var link = document.createElement('a');

		this.parentNode.insertBefore(link, this);
		link.appendChild(this);

		this.addEventListener("click", include_exclude_click);
	});
	
	var editor = new Editor();

});