Google 圖片搜尋直開

Google 圖片搜尋點擊圖片直接懸浮原圖開啟圖片,而不是進入網站

// ==UserScript==
// @name         Google 圖片搜尋直開
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Google 圖片搜尋點擊圖片直接懸浮原圖開啟圖片,而不是進入網站
// @author       shanlan(ChatGPT o3-mini)
// @match        https://www.google.com/search*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function(){
  'use strict';
  document.head.appendChild(Object.assign(document.createElement('style'), {
    textContent: "#tm-image-modal{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.8);display:flex;align-items:center;justify-content:center;z-index:9999}#tm-image-modal img{max-width:90%;max-height:90%;border:2px solid #fff}"
  }));
  const getVisibleSrc = a => {
    for (const img of a.querySelectorAll("img")) {
      const s = getComputedStyle(img);
      if (s.visibility !== "hidden" && img.offsetWidth && img.offsetHeight) return img.src;
    }
  }, openModal = a => {
    const m = document.createElement("div");
    m.id = "tm-image-modal";
    const i = document.createElement("img");
    i.src = getVisibleSrc(a);
    m.appendChild(i);
    document.body.appendChild(m);
    const o = new MutationObserver(() => {
      const n = getVisibleSrc(a);
      if (n && n !== i.src) i.src = n;
    });
    o.observe(a, {attributes: true, childList: true, subtree: true});
    m.onclick = () => { o.disconnect(); m.remove() };
  };
  document.addEventListener("click", e => {
    const a = e.target.closest("a.YsLeY");
    if (a && getVisibleSrc(a)) { e.preventDefault(); openModal(a) }
  });
})();