Google cache viewer

Automatically adds a cache link to Google Search results. / Google搜索结果中添加缓存按钮

目前为 2024-07-12 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Google cache viewer
  3. // @namespace http://hhtjim.com/
  4. // @version 1.0.1
  5. // @description Automatically adds a cache link to Google Search results. / Google搜索结果中添加缓存按钮
  6. // @author Hootrix
  7. // @include https://www.google.tld/search?*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. window.addEventListener('load', function() {
  16. // select containers `cite[role="text"]`
  17. const containers = document.querySelectorAll('.g.Ww4FFb.vt6azd.tF2Cxc.asEBEc');
  18.  
  19. containers.forEach(container => {
  20. //const cite = container.querySelector('cite[role="text"]');
  21. let cites = container.querySelectorAll('cite[role="text"]');
  22. // last item
  23. let cite = cites[cites.length - 1];
  24. const link = container.querySelector('a[data-ved]');
  25. if (cite && cite.textContent.startsWith('http')) {
  26. //const url = cite.textContent;
  27. const url = link.href
  28. const cacheUrl = `https://webcache.googleusercontent.com/search?q=cache:${url}`;
  29.  
  30. const cacheDiv = document.createElement('div');
  31. cacheDiv.className = ''; // class name eFM0qc
  32. cacheDiv.innerHTML = `<a href="${cacheUrl}" target="_blank" style="visibility:visible;color: blue; margin-left: 10px;">[Cache]</a>`;
  33.  
  34. if (cite.parentElement) {
  35. cite.parentElement.appendChild(cacheDiv);
  36. }
  37. }
  38. });
  39. });
  40. })();