Google Webspam Report

Open the webspam reporting form for that page from the context menu (right-click menu) of the link of Google search.

// ==UserScript==
// @name        Google Webspam Report
// @name:ja     Google Webスパム報告
// @description Open the webspam reporting form for that page from the context menu (right-click menu) of the link of Google search.
// @description:ja Google検索のリンクのコンテキストメニュー (右クリックメニュー) から、そのページを報告するためのWebスパム報告フォームを開きます。
// @version     1.0.2
// @namespace   https://greasyfork.org/users/137
// @match       https://www.google.com/webmasters/tools/spamreportform
// @match       https://www.google.com/webmasters/tools/spamreportform?*
// @match       https://www.google.com/webmasters/tools/spamreportform#*
// @include     https://www.google.*
// @require     https://gitcdn.xyz/cdn/greasemonkey/gm4-polyfill/a834d46afcc7d6f6297829876423f58bb14a0d97/gm4-polyfill.js
// @require     https://greasyfork.org/scripts/19616/code/utilities.js?version=868689
// @license     MPL-2.0
// @contributionURL https://www.amazon.co.jp/registry/wishlist/E7PJ5C3K7AM2
// @compatible  Edge
// @compatible  Firefox Firefoxを推奨 / Firefox is recommended
// @compatible  Opera
// @compatible  Chrome
// @grant       GM.openInTab
// @grant       GM_openInTab
// @noframes
// @run-at      document-end
// @icon        https://www.google.com/favicon.ico
// @author      100の人
// @homepageURL https://greasyfork.org/scripts/410143
// ==/UserScript==

'use strict';

const DETAILS_TEMPLATES = [
	'This site was automatically generated by machine translation.',
	'This page is the search result.',
];

Gettext.setLocalizedTexts({
	/*eslint-disable quote-props, max-len */
	'ja': {
		'Spam report': 'スパム報告',
		'Clear': 'クリア',
		'This site was automatically generated by machine translation.': '機械翻訳により自動生成されたサイトです。',
		'This page is the search result.': '検索結果ページです。',
	},
	/*eslint-enable quote-props, max-len */
});
Gettext.originalLocale = 'en';
Gettext.setLocale(navigator.language);

if (location.origin === 'https://www.google.com' && location.pathname === '/webmasters/tools/spamreportform') {
	const comments = document.getElementById('comments');
	const parent = comments.closest('.input-field');
	parent.insertAdjacentHTML('beforebegin', '<div class="text"><select></select></div>');
	const select = parent.previousElementSibling.firstElementChild;
	select.add(new Option(_('Clear'), ''));
	for (const details of DETAILS_TEMPLATES) {
		select.add(new Option(_(details), details));
	}
	select.value = comments.value = DETAILS_TEMPLATES[0];
	parent.previousElementSibling.firstElementChild.addEventListener('change', function (event) {
		comments.value = event.target.value;
	});
} else if (/^www\.google(\.com?)?\.[a-z]+$/.test(location.host)) {
	let actionMenuParentTemplate, itemTemplate;

	new MutationObserver(function () {
		const query = new URLSearchParams(location.search).get('q');
		for (const parent
			of document.querySelectorAll('.g:not([data-has-report-button]) a + div > div:nth-of-type(2)')) {
			const root = parent.closest('.g');
			root.dataset.hasReportButton = '';

			const spamurl = root.getElementsByTagName('a')[0].href;
			if (spamurl.startsWith('https://books.google.')) {
				// Googleブック
				continue;
			}

			let panel = parent.getElementsByClassName('action-menu-panel')[0];
			if (!panel) {
				if (!actionMenuParentTemplate) {
					actionMenuParentTemplate
						= document.getElementsByClassName('action-menu')[0].parentElement.cloneNode(true);
					const items = actionMenuParentTemplate.getElementsByClassName('action-menu-item');
					while (items[0]) {
						items[0].remove();
					}
				}
				panel = parent.appendChild(actionMenuParentTemplate.cloneNode(true))
					.getElementsByClassName('action-menu-panel')[0];
			}

			if (!itemTemplate) {
				itemTemplate = document.getElementsByClassName('action-menu-item')[0].cloneNode(true);
				itemTemplate.getElementsByTagName('a')[0].target = '_blank';
				itemTemplate.getElementsByTagName('span')[0].textContent = _('Spam report');
			}
			const item = itemTemplate.cloneNode(true);
			item.getElementsByTagName('a')[0].href
				= 'https://www.google.com/webmasters/tools/spamreportform?' + new URLSearchParams({ spamurl, query });
			panel.append(item);
		}
	}).observe(document, { childList: true, subtree: true });
}