Google, Middle Click Search

Opens search results in new tab when you middle click

目前為 2016-08-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google, Middle Click Search
// @version      1.0.1
// @description  Opens search results in new tab when you middle click
// @author       Adrien Pyke
// @namespace    https://greasyfork.org/users/649
// @include      /^https?:\/\/www\.google\.[a-zA-Z]+\/?$/
// @include      /^https?:\/\/www\.google\.[a-zA-Z]+\/search\?.*\/?$/
// @require      https://greasyfork.org/scripts/5679-wait-for-elements/code/Wait%20For%20Elements.js?version=122976
// @grant        GM_openInTab
// ==/UserScript==

(function() {
	'use strict';

	var updateQueryString = function(key, value, url) {
		if (!url) url = window.location.href;
		var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
			hash;

		if (re.test(url)) {
			if (typeof value !== 'undefined' && value !== null)
				return url.replace(re, '$1' + key + "=" + value + '$2$3');
			else {
				hash = url.split('#');
				url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
				if (typeof hash[1] !== 'undefined' && hash[1] !== null)
					url += '#' + hash[1];
				return url;
			}
		}
		else {
			if (typeof value !== 'undefined' && value !== null) {
				var separator = url.indexOf('?') !== -1 ? '&' : '?';
				hash = url.split('#');
				url = hash[0] + separator + key + '=' + value;
				if (typeof hash[1] !== 'undefined' && hash[1] !== null)
					url += '#' + hash[1];
				return url;
			}
			else
				return url;
		}
	};

	var getUrl = function(value) {
		if (window.location.href.match(/^https?:\/\/www\.google\.[a-zA-Z]+\/search\?.*\/?$/)) {
			return updateQueryString('q', encodeURIComponent(value), window.location.href);
		} else {
			return location.protocol + '//' + location.host + '/search?q=' + encodeURIComponent(value);
		}
	};

	waitForElems('#sblsbb > button', function(btn) {
		var input = document.querySelector('#lst-ib');

		btn.onmousedown = function(e) {
			if (e.button === 1) {
				e.preventDefault();
			}
		};

		btn.onclick = function(e) {
			if (e.button === 1 && input.value.trim()) {
				e.preventDefault();
				e.stopImmediatePropagation();
				var url = getUrl(input.value);
				GM_openInTab(url, true);
				return false;
			}
		};
	});
	waitForElems('.sbsb_b li .sbqs_c, .sbsb_b li .sbpqs_d', function(elem) {
		elem.onclick = function(e) {
			if (e.button === 1) {
				e.preventDefault();
				e.stopImmediatePropagation();
				var text = elem.classList.contains('sbpqs_d') ? elem.querySelector('span').textContent : elem.textContent;
				var url = getUrl(text);
				GM_openInTab(url, true);
				return false;
			}
		};
	});
})();