🏠 Home 

预览链接中的图片

在本页浏览其他网页链接中的图片


Install this script?
  1. // ==UserScript==
  2. // @name 预览链接中的图片
  3. // @namespace http://tampermonkey
  4. // @version 1
  5. // @description 在本页浏览其他网页链接中的图片
  6. // @match *://*/*
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // @grant GM_registerMenuCommand
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13. // 获取当前页面的域名
  14. const currentDomain = window.location.hostname;
  15. // 检查用户是否已经选择了禁用脚本
  16. let isScriptDisabled = GM_getValue(`disableScript_${currentDomain}`, false);
  17. // 添加启用/禁用功能的按钮
  18. GM_registerMenuCommand(isScriptDisabled ? '启用预览链接中的图片' : '禁用预览链接中的图片', function() {
  19. isScriptDisabled = !isScriptDisabled;
  20. GM_setValue(`disableScript_${currentDomain}`, isScriptDisabled);
  21. location.reload(); // 刷新页面使设置生效
  22. });
  23. // 如果脚本被禁用,直接返回,不执行脚本代码
  24. if (isScriptDisabled) {
  25. return;
  26. }
  27. const links = document.getElementsByTagName('a');
  28. for (let i = 0; i < links.length; i++) {
  29. const link = links[i];
  30. if (link.href.match(/\.(jpg|jpeg|png|gif)$/i)) {
  31. const img = document.createElement('img');
  32. img.src = link.href;
  33. img.style.maxWidth = '100%';
  34. img.style.maxHeight = '100%';
  35. img.style.display = 'block';
  36. img.style.margin = '0 auto';
  37. link.parentNode.insertBefore(img, link.nextSibling);
  38. }
  39. }
  40. })();
  41. // 注册一个菜单命令,让用户可以禁用脚本
  42. GM_registerMenuCommand('禁用预览链接中的图片', () => {
  43. // 获取当前页面的域名
  44. const currentDomain = window.location.hostname;
  45. // 将禁用标志设置为true,同时考虑域名
  46. GM_setValue(`disableScript_${currentDomain}`, true);
  47. alert('脚本已禁用,请刷新页面以生效。');
  48. });