// ==UserScript== // @name 预览链接中的图片 // @namespace http://tampermonkey // @version 1 // @description 在本页浏览其他网页链接中的图片 // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 获取当前页面的域名 const currentDomain = window.location.hostname; // 检查用户是否已经选择了禁用脚本 const isScriptDisabled = GM_getValue(`disableScript_${currentDomain}`, false); // 如果用户选择禁用脚本,则不执行脚本代码 if (isScriptDisabled) { return; } const links = document.getElementsByTagName('a'); for (let i = 0; i < links.length; i++) { const link = links[i]; if (link.href.match(/\.(jpg|jpeg|png|gif)$/i)) { const img = document.createElement('img'); img.src = link.href; img.style.maxWidth = '100%'; img.style.maxHeight = '100%'; img.style.display = 'block'; img.style.margin = '0 auto'; link.parentNode.insertBefore(img, link.nextSibling); } } })(); // 注册一个菜单命令,让用户可以禁用脚本 GM_registerMenuCommand('禁用脚本', () => { // 获取当前页面的域名 const currentDomain = window.location.hostname; // 将禁用标志设置为true,同时考虑域名 GM_setValue(`disableScript_${currentDomain}`, true); alert('脚本已禁用,请刷新页面以生效。'); });