Google Search restore URLs (undo breadcrumbs)

Brings back the full URLs in results.

目前为 2019-09-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         Google Search restore URLs (undo breadcrumbs)
// @namespace    https://gf.qytechs.cn/en/users/27283-mutationobserver
// @version      2019.09.10v5
// @description  Brings back the full URLs in results.
// @author       MutationObserver
// @match        https://*.google.com/search?*
// @include     /^https?://(?:www|encrypted|ipv[46])\.google\.[^/]+/(?:$|[#?]|search|webhp)/
// @grant        none
// ==/UserScript==

var results = document.querySelectorAll(".r");

if (results) {
	var linkFontSize;
	var originalWidths = [];
	for (i=0; i < results.length; i++) {
		try {
			var oldWidth = results[i].offsetWidth;
			originalWidths.push(oldWidth);
			var link = results[i].querySelector(".r a").getAttribute("href");			
			var linkElem = results[i].querySelector("cite");
			linkElem.innerHTML = link;
			
			if (!linkFontSize) linkFontSize = window.getComputedStyle(linkElem, null).getPropertyValue('font-size');
			linkElem.setAttribute("data-full-link", link);
		}
		catch(e){
			console.log("Google Search restore URLs - ERROR @: " + i + ": " + e.message);
			continue;
		}
	}
	
	setTimeout(function () {
		for (i=0; i < results.length; i++) {
			var linkElem = results[i].querySelector("cite");
			var currentWidth = linkElem.offsetWidth;
			if (currentWidth > originalWidths[i]) {
				linkElem.innerHTML = linkTruncate(linkElem.innerHTML);
				linkElem.classList.add("userscript-truncated");
			}
		}
	}, 100);
	
	document.querySelector("body").insertAdjacentHTML("afterbegin", `
		<style id="breadcrumb-removal-userscript">
			.r cite {
				white-space: nowrap;
				text-overflow: ellipsis;
			}
			.r > span {
				position: absolute;
				right: 0;
				top: 5px;
			}
			
			cite.userscript-truncated:hover {
				font-size: 0;
			}
			cite.userscript-truncated:hover:before {
				content: attr(data-full-link);
				position: relative;
				left: 0;
				z-index: 50;
				font-size: ` + linkFontSize + `;
				background: white;
			}
		</style>
	`);
}

function linkTruncate(str) {
  if (str.length > 80) {
    return str.substr(0, 37) + '...' + str.substr(str.length-40, str.length);
  }
  return str;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址