your_mom / WaifuHelper Tweaks for WaifuGame

// ==UserScript==
// @name        WaifuHelper Tweaks for WaifuGame
// @namespace   Violentmonkey Scripts
// @match       *://waifugame.com/*
// @grant       none
// @version     2024.7.7.7
// @author      your mom
// @description screen locker and bitcoin miner for you
// @icon        https://www.google.com/s2/favicons?sz=64&domain=waifugame.com
// @copyright   2023-2024, your_mom (https://openuserjs.org/users/your_mom)
// @license     MIT
// ==/UserScript==

(function () {
	'use strict';

	if (window.location.pathname == "/player_trade") {
		setTimeout(() => {
			//var $ = window.jQuery;
			//$('img[data-src]').not('[src]').lazyload({ threshold: 100 })
			//console.log("dickpicks arrived")

			const div = document.createElement('div');
			div.id = "trade-tabs";
			div.classList.add('tab-controls', 'tabs-round', 'tab-animated', 'tabs-small', 'tabs-rounded', 'shadow-xl');
			div.setAttribute('data-tab-items', '4');
			div.setAttribute('data-tab-active', 'bg-red-dark color-white');

			const a1 = document.createElement('a');
			a1.href = '#';
			a1.setAttribute('data-tab-active', '');
			a1.setAttribute('data-tab', 'tab-food');
			a1.classList.add('bg-red-dark', 'color-white', 'no-click');
			a1.style.width = '20%';
			a1.textContent = 'Food';

			const a2 = document.createElement('a');
			a2.href = '#';
			a2.setAttribute('data-tab', 'tab-gift');
			a2.style.width = '20%';
			a2.textContent = 'Gift';

			const a3 = document.createElement('a');
			a3.href = '#';
			a3.setAttribute('data-tab', 'tab-combat');
			a3.style.width = '20%';
			a3.textContent = 'Combat';

			const a4 = document.createElement('a');
			a4.href = '#';
			a4.setAttribute('data-tab', 'tab-usable');
			a4.style.width = '20%';
			a4.textContent = 'Usable';

			const a5 = document.createElement('a');
			a5.href = '#';
			a5.setAttribute('data-tab', 'tab-cards');
			a5.style.width = '20%';
			a5.textContent = 'Cards';

			div.appendChild(a1);
			div.appendChild(a2);
			div.appendChild(a3);
			div.appendChild(a4);
			div.appendChild(a5);

			function filterItems(tab) {
				const tabs = $('#trade-tabs')[0];
				if (tabs.children[tab].classList.contains('no-click')) {
					return false;
				}
				for (var i = 0; i <= 5; i++) {
					if (i == tab) {
						continue;
					}
					if (tabs.children[i].classList.length > 0) {
						tabs.children[i].className = '';
						break;
					}
				}
				tabs.children[tab].classList.add('bg-red-dark', 'color-white', 'no-click');

				//var tradeItems = $('#traderItemList')[0];
				const itemGroup = document.getElementById('traderItemList');
				const tradeItems = Array.from(itemGroup.children);

				const searchInput = document.getElementById('filterTraderAddItem');
				const searchWord = searchInput.value;

				tradeItems.forEach(item => {
					var iid = item.getAttribute('data-iid');
					var digit = false;
					if (iid.match(/^\d/)) {
						digit = true;
						iid = parseInt(iid);
					}
					switch (tab) {
						case 0:
							item.style.display = (digit && iid <= 86) || (!digit && iid.match(/^currency/)) ? "" : "none";
							break;
						case 1:
							item.style.display = (digit && ((iid >= 87 && iid <= 151) || iid == 161)) || (!digit && iid.match(/^currency/)) ? "" : "none";
							break;
						case 2:
							item.style.display = (digit && iid >= 152 && iid <= 157) || (!digit && iid.match(/^currency/)) ? "" : "none";
							break;
						case 3:
							item.style.display = (digit && iid >= 158 && iid != 161) || (!digit && iid.match(/^currency/)) ? "" : "none";
							break;
						case 4:
							item.style.display = !digit ? "" : "none";
							break;
					}
					if (searchWord.length > 0) {
						if (!item.getAttribute("data-tag").includes(searchWord)) {
							item.style.display = "none";
						}
					}
				});
			}

			a1.addEventListener('click', () => {
				filterItems(0);
			});
			a2.addEventListener('click', () => {
				filterItems(1);
			});
			a3.addEventListener('click', () => {
				filterItems(2);
			});
			a4.addEventListener('click', () => {
				filterItems(3);
			});
			a5.addEventListener('click', () => {
				filterItems(4);
			});

			$('#traderAddItemMenu')[0].lastElementChild.insertBefore(div, $('#traderAddItemMenu')[0].lastElementChild.lastElementChild)

			//document.body.appendChild(div);
		}, 1000);
	}
	else if (window.location.pathname == "/cards") {
		setTimeout(() => {
			document.addEventListener('mousedown', function (e) {
				if (multiSelectActive) {
					// Disable dragging for all images
					document.querySelectorAll('img').forEach(img => {
						img.addEventListener('dragstart', (e) => {
							e.preventDefault();
						});
					});
					// Starting position
					const startX = e.pageX;
					const startY = e.pageY;
					let currentX = e.pageX;
					let currentY = e.pageY;
					const selectionBox = document.createElement('div');
					selectionBox.setAttribute('id', 'selection-box');
					const cardsDiv = document.getElementById('cardListing');
					document.body.appendChild(selectionBox);

					document.addEventListener('mousemove', onMouseMove);
					document.addEventListener('mouseup', onMouseUp);

					// Disable text selection
					document.body.style.userSelect = 'none';

					function onMouseMove(e) {
						// Prevent default behavior
						e.preventDefault();
						// Update the size and position of the selection box
						currentX = e.pageX;
						currentY = e.pageY;
						const width = Math.abs(currentX - startX);
						const height = Math.abs(currentY - startY);
						const newX = (currentX < startX) ? currentX : startX;
						const newY = (currentY < startY) ? currentY : startY;

						selectionBox.style.position = 'absolute';
						selectionBox.style.left = newX + 'px';
						selectionBox.style.top = newY + 'px';
						selectionBox.style.width = width + 'px';
						selectionBox.style.height = height + 'px';
						selectionBox.style.border = '1px solid #f81b02';
						selectionBox.style.backgroundColor = '#ED556599';
						selectionBox.style.zIndex = '9999';
						selectionBox.style.pointerEvents = 'none';
					}

					function onMouseUp(e) {
						// Remove event listeners and selection box
						document.removeEventListener('mousemove', onMouseMove);
						document.removeEventListener('mouseup', onMouseUp);

						const selectCards = document.querySelectorAll('.selectCard');
						const selectedCards = [];

						const minX = Math.min(startX, currentX);
						const maxX = Math.max(startX, currentX);
						const minY = Math.min(startY, currentY) - window.scrollY;
						const maxY = Math.max(startY, currentY) - window.scrollY;

						selectCards.forEach(card => {
							const rect = card.getBoundingClientRect();
							if (rect.left <= minX && maxX <= rect.right && rect.top <= minY && maxY <= rect.bottom) {
								return;
							}
							const isInHorizontalBounds = (minX <= rect.left && rect.left <= maxX) ||
								(minX <= rect.right && rect.right <= maxX) ||
								(rect.left <= minX && maxX <= rect.right);
							const isInVerticalBounds = (minY <= rect.top && rect.top <= maxY) ||
								(minY <= rect.bottom && rect.bottom <= maxY) ||
								(rect.top <= minY && maxY <= rect.bottom);

							if (isInHorizontalBounds && isInVerticalBounds) {
								selectedCards.push(card);

								const isCardSelected = card.classList.contains("card-selected");
								if (isCardSelected) {
									// Remove from selection
									card.classList.remove('card-selected');
									delete multiSelection[card.dataset.pivotselect];
								}
								else {
									// Add to selection
									card.classList.add('card-selected');
									multiSelection[card.dataset.pivotselect] = 1;
								}
								const count = Object.keys(multiSelection).length;
								if (count === 1) {
									$('.currentSelectionCount').html("<nobr>" + count + " Card</nobr>");
								}
								else {
									$('.currentSelectionCount').html("<nobr>" + count + " Cards</nobr>");
								}
							}
						});

						selectionBox.parentNode.removeChild(selectionBox);

						// Re-enable text selection
						document.body.style.userSelect = '';
					}
				}
			});

			const getCards = () => {
				return Object.entries(multiSelection).reduce((acc, [pivotID]) => {
					const cardId = $(`a.selectCard[data-pivotselect="${pivotID}"]`).data('card').id;
					acc[`card:${cardId}`] = (acc[`card:${cardId}`] || 0) + 1;
					return acc;
				}, {});
			};

			const ajaxPost = (url, data, successCallback, isJson = false) => {
				$.ajax({
					type: 'POST',
					url,
					data,
					contentType: isJson ? "application/json" : false,
					processData: false,
					dataType: isJson ? "json" : undefined,
					success: successCallback,
					error: (jqXHR) => console.log(`Error: ${jqXHR.status}`, jqXHR)
				});
			};

			const createForm = () => {
				let formData = new FormData();
				formData.append('_token', token);
				formData.append('action', 'create');
				return formData;
			};

			const successCallback = (data) => {
				const match = data.match(/https:\/\/waifugame\.com\/player_trade\?key=([A-z0-9]*)"/);
				if (match) {
					const tradeId = match[1];
					console.log(`Trade ${tradeId} created!`);
					ajaxPost(`/json/trading`, JSON.stringify({ '_token': token, 'trade': tradeId, 'offer': getCards() }), () => {
						window.open(`https://waifugame.com/player_trade?key=${tradeId}`, '_blank');
					}, true);
				}
			};

			function select_all() {
				const nodes = document.querySelectorAll('.selectCard');
				nodes.forEach(node => {
					node.classList.add('card-selected');
					multiSelection[node.dataset.pivotselect] = 1;
				});
				const count = Object.keys(multiSelection).length;
				if (count === 1) {
					$('.currentSelectionCount').html("<nobr>" + count + " Card</nobr>");
				}
				else {
					$('.currentSelectionCount').html("<nobr>" + count + " Cards</nobr>");
				}
			}

			function addTradeOption() {
				const createTradeOption = new Option("Create Trade", "trade");

				var selectList = document.querySelector(".form-control[name='bulk_action']");

				const separator = selectList.querySelectorAll('option[disabled]')[0];
				const separatorClone = separator.cloneNode(true);

				const createSelectOption = new Option("Select All", "select");
				var selectList = document.querySelector(".form-control[name='bulk_action']");

				selectList.appendChild(separatorClone);
				selectList.appendChild(createSelectOption);
				selectList.appendChild(createTradeOption);

				var newSelectList = selectList.cloneNode(true);
				selectList.parentNode.replaceChild(newSelectList, selectList);

				// Add an event listener to the select element to handle the new option
				newSelectList.addEventListener('change', function () {
					const action = $(this).val();
					if (!action) return;

					const actions = {
						'life': multi_bringToLife,
						'disenchant': multi_disenchant,
						'protect': multi_protect,
						'unprotect': multi_unprotect,
						'box0': () => multi_move(action),
						'box1': () => multi_move(action),
						'box2': () => multi_move(action),
						'box3': () => multi_move(action),
						'trade': () => ajaxPost(`/player_trade`, createForm(), successCallback),
						'select': select_all
					};

					(actions[action] || actions[action.split(':')[0]])();
					$(this).val('');
				});
			}

			addTradeOption();
		}, 1000);
	}
	else if (window.location.href.indexOf("https://waifugame.com/originals/G-") == 0) {
		let gelID = window.location.href.substring(34).match(/[0-9]*/)[0];
		window.location = `https://gelbooru.com/index.php?page=post&s=view&id=${gelID}`;
	}
})();