Greasy Fork is available in English.

Custom Word Lists

Creates a drop-down list with word lists from words.sketchful.io

Tính đến 2020-07-31 09:40:25 UTC. Xem phiên bản mới nhất.

// ==UserScript==
// @name        Custom Word Lists
// @namespace   https://greasyfork.org/users/281093
// @match       https://sketchful.io/*
// @grant       GM.xmlHttpRequest
// @version     1.0
// @author      Bell
// @description Creates a drop-down list with word lists from words.sketchful.io
// ==/UserScript==
/* jshint esversion: 6 */
/* eslint-disable no-undef */

const siteURL = 'https://words.sketchful.io/';
// const siteURL = 'http://www.sw-guild.com/';

const categories = new Map();
const categoryURL = {
	color: {
		url: 'color-5.html',
		name: 'Colors'
	},
	famous: {
		url: 'famous-5.html',
		name: 'Famous'
	},
	game: {
		url: 'game-6.html',
		name: 'Games'
	},
	other: {
		url: 'other-5.html',
		name: 'Other'
	}
};

(function addDroplist() {
	const formGroup = document.createElement('div');
	const label = document.createElement('label');
	const select = document.createElement('select');
	const wordsTextbox = document.querySelector('#gameSettingsWordsTextArea');
	const settingsCol = document.querySelector(
		'#gameSettings > div.row.align-items-end > div:nth-child(2)'
	);

	formGroup.setAttribute('class', 'form-group');
	label.setAttribute('for', 'gameWordlists');
	label.textContent = 'Word Lists';
	select.setAttribute('class', 'form-control');
	select.id = 'gameWordlists';
	select.onchange = () => {
		wordsTextbox.value = select.value;
	};
	formGroup.append(label, select);
	settingsCol.insertBefore(formGroup, settingsCol.firstChild);
})();

async function loadWordlists() {
	try {
		await Promise.all([
			getHTML(categoryURL.color), getHTML(categoryURL.famous),
			getHTML(categoryURL.game), getHTML(categoryURL.other)
		]);

		addOptions(categories);
	}
	catch (e) {
		console.error(e);
	}
}

loadWordlists();

function getHTML(category) {
	return new Promise((resolve, reject) => {
		GM.xmlHttpRequest({
			method: 'GET',
			url: siteURL + category.url,
			onload: (res) => {
				categories.set(category.name, findWordlists(res));
				resolve(categories);
			},
			onerror: reject
		});
	});
}

function addOptions() {
	const selectList = document.querySelector('#gameWordlists');

	for (const [category, lists] of categories) {
		const categoryName = document.createElement('optgroup');
		categoryName.label = category;
		selectList.appendChild(categoryName);

		for (const [listname, words] of lists) {
			const option = document.createElement('option');
			option.value = words;
			option.textContent = listname;
			categoryName.appendChild(option);
		}
	}
}

function findWordlists(res) {
	const wordlists = new Map();

	const html = document.createElement('html');
	html.innerHTML = res.responseText;
	const lists = html.querySelectorAll('.text-placement');
	const listNames = html.querySelectorAll('.delete-title');

	for (let i = 0; i < lists.length; i++) {
		wordlists.set(
			listNames[i].textContent.trim(),
			lists[i].textContent.trim()
		);
	}

	return wordlists;
}