javascript:(function(){
	console.log('bookmarklet "tables" was called');
	document.querySelectorAll('.bookmarklet_notice_tables').forEach(el => el.remove());
	
	let color_green = '#186839';
	let bg_color_green = '#97e7b9';
	
	let color_red = '#71180e';
	let bg_color_red = '#f4aaa4';
	
	function run_bm_tables() {
		let i;
		let tables = document.querySelectorAll('table');
		tables.forEach(table => {
			table.querySelectorAll('caption').forEach(caption => {
				caption.style.backgroundColor = 'aqua';
			});
			
			let hasCaption = table.querySelector('caption');
			let hasAriaLabel = table.hasAttribute('aria-label');
			let hasAriaLabelledby = table.hasAttribute('aria-labelledby');
			let captionInfo = document.createElement('div');
			
			captionInfo.className = 'bookmarklet_notice_tables';
			captionInfo.style.fontSize = '0.9em';
			captionInfo.style.fontWeight = 'bold';
			captionInfo.style.padding = '4px';
			
			if (!hasCaption && !hasAriaLabel && !hasAriaLabelledby) {
				captionInfo.style.color = color_red;
				captionInfo.style.backgroundColor = bg_color_red;
				captionInfo.textContent = `🡣 This table is not entitled.`;
			} else {
				captionInfo.style.color = color_green;
				captionInfo.style.backgroundColor = bg_color_green;
				
				if (hasCaption) {
					captionInfo.textContent = `🡣 Caption is provided: "${hasCaption.innerHTML.trim()}".`;
				} else if (hasAriaLabel) {
					captionInfo.textContent = `🡣 ARIA label is provided: "${table.getAttribute('aria-label')}".`;
				} else if (hasAriaLabelledby) {
					let labelledbyId = table.getAttribute('aria-labelledby');
					let labelledbyElement = document.getElementById(labelledbyId);
					if (labelledbyElement) {
						captionInfo.textContent = `🡣 ARIA labelledby points to: "${labelledbyElement.innerHTML.trim()}".`;
					} else {
						captionInfo.style.color = color_red;
						captionInfo.style.backgroundColor = bg_color_red;
						captionInfo.textContent = `🡣 ARIA labelledby attribute is set but no element with ID "${labelledbyId}" was found.`;
					}
				}
			}
			
			table.parentNode.insertBefore(captionInfo, table);
			
			table.querySelectorAll('td').forEach(td => {
				td.style.backgroundColor = '#f7e187';
				td.style.border = '2px solid #f0c30f';
			});
			
			let ths = table.querySelectorAll('th');
			
			ths.forEach(th => {
				th.style.backgroundColor = '#59d98e';
				th.style.border = '3px dashed '+color_green;
			});
			
			if (ths.length == 0) {
				let thInfo = document.createElement('div');
				thInfo.className = 'bookmarklet_notice_tables';
				thInfo.style.fontSize = '0.9em';
				thInfo.style.fontWeight = 'bold';
				thInfo.style.padding = '4px';
				thInfo.style.color = color_red;
				thInfo.style.backgroundColor = bg_color_red;
				thInfo.textContent = `🡣 This table has no th elements.`;
				table.parentNode.insertBefore(thInfo, table);
			}
		});
		if (tables.length === 0) {
			alert('OK: the page has no tables.');
		}
	}
	
	setTimeout(function(){
		run_bm_tables();
	}, 200);
})();