// ==UserScript== // @name 一键隐藏图片 // @version 0.3.3 // @author SoyaDokio // @icon https://cdn.jsdelivr.net/gh/sdokio/UserScript/assets/images/hide_pictures_on_page.jpg // @description 摸鱼时页面显示与工作不相关的图片未免有些明目张胆,这时候就需要一键隐藏全图了。 // @match *://*/* // @grant GM_addStyle // @grant GM_getValue // @grant GM_setValue // @require https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js // @run-at document-start // @license MIT // @namespace https://greasyfork.org/scripts/420682 // @supportURL https://github.com/sdokio/UserScript // @homepageURL https://github.com/sdokio/UserScript // @downloadURL none // ==/UserScript== (function () { "use strict"; Array.prototype.indexOf = function (val) { for (var i = 0; i < this.length; i++) if (this[i] == val) return i; return -1; } Array.prototype.remove = function (val) { while (true) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } else { break; } } } var hpop_config_custom; var hpop_config_default = { "version": "0.3.3", "sitesHide": [], "position": { "top": window.innerHeight / 2 - 14 + "", "left": "0", "right": "auto" } } const STYLE_RAW = "" + ".hpop-panel{" + "position:fixed;" + "width:64px;" + "height:20px;" + "font-size:12px;" + "font-weight: 500;" + "font-family:Verdana, Arial, '宋体';" + "background:#f1f1f1;" + "z-index:2147483647;" + "margin: 0;" + "opacity:0.4;" + "transition:0.3s;" + "overflow:hidden;" + "user-select:none;" + "text-align:left;" + "white-space:nowrap;" + "line-height:20px;" + "padding:3px 6px;" + "border:1px solid #ccc;" + "box-sizing: content-box;" + "}" + ".hpop-panel-left{" + "transform:translate(-63px,0);" + "border-width:1px 1px 1px 0;" + "border-top-right-radius: 14px;" + "border-bottom-right-radius: 14px;" + "}" + ".hpop-panel-right{" + "transform:translate(63px,0);" + "border-width:1px 0 1px 1px;" + "border-top-left-radius: 14px;" + "border-bottom-left-radius: 14px;" + "padding-left: 10px;" + "padding-right: 0;" + "}" + ".hpop-panel input{" + "margin: 0;" + "padding: 0;" + "vertical-align:middle;" + "-webkit-appearance:checkbox;" + "-moz-appearance:checkbox;" + "position: static;" + "clip: auto;" + "opacity: 1;" + "cursor: pointer;" + "}" + ".hpop-panel.hpop-panel-active{" + "width:70px;" + "opacity: 0.9;" + "}" + ".hpop-panel.hpop-panel-left-active{" + "left: 0px;" + "transform:translate(0,0);" + "}" + ".hpop-panel.hpop-panel-right-active{" + "right: 0px;" + "transform:translate(0,0);" + "}" + ".hpop-panel label{" + "margin:0;" + "padding:0 0 0 3px;" + "font-weight:500;" + "}" + ".hpop-panel-move{" + "border-width:1px 1px 1px 0;" + "border-radius: 14px;" + "}" + " "; main(); function main() { // 取出本地缓存配置 hpop_config_custom = GM_getValue("hpop_config"); if (!hpop_config_custom) { hpop_config_custom = hpop_config_default; } // 将数据结构的变更保存到本地缓存配置 var updFlag = false; for (var _key in hpop_config_default) { if (!hpop_config_custom.hasOwnProperty(_key)) { hpop_config_custom._key = hpop_config_default._key; updFlag = true; } } if (updFlag) { // 保存当前配置到本地缓存 GM_setValue("hpop_config", hpop_config_custom); } generateControlPanel(); } // 生成控制面板(浮动元素) function generateControlPanel() { // 新建控制面板元素 var node = document.createElement("hide-pictures-on-page"); node.id = "hpop-panel"; if (hpop_config_custom.position.left == 0) { node.className = "hpop-panel hpop-panel-left"; } if (hpop_config_custom.position.right == 0) { node.className = "hpop-panel hpop-panel-right"; } node.style.cssText = "position:fixed;top:" + hpop_config_custom.position.top + "px;" + "left:" + hpop_config_custom.position.left + "px;" + "right:" + hpop_config_custom.position.right + "px;"; node.innerHTML = "" + ""; // 仅在顶层窗口添加控制面板 if (window.self === window.top) { if (document.querySelector("body")) { document.body.appendChild(node); } else { document.documentElement.appendChild(node); } } // 根据记忆状态(显示/隐藏)初始化该网站 if (hpop_config_custom.sitesHide.indexOf(document.location.hostname) > -1) { document.querySelector("#hpop-switch").checked = true; $(document).ready(function() { $("img").hide(); }); } // 添加控制面板所需样式 var _style = document.createElement("style"); _style.type = "text/css"; _style.innerHTML = STYLE_RAW; if (document.querySelector("#hpop-panel")) { document.querySelector("#hpop-panel").appendChild(_style); } else { GM_addStyle(STYLE_RAW); } // 给控制面板添加鼠标滑入/滑出时展开/吸附效果 node.addEventListener("mouseover", function () { node.classList.add("hpop-panel-active"); if (hpop_config_custom.position.left == 0) { node.classList.add("hpop-panel-left-active"); } if (hpop_config_custom.position.right == 0) { node.classList.add("hpop-panel-right-active"); } }); node.addEventListener("mouseleave", function () { setTimeout(function () { node.classList.remove("hpop-panel-active"); node.classList.remove("hpop-panel-left-active"); node.classList.remove("hpop-panel-right-active"); }, 300); }); // 给控制面板添加拖拽效果 node.addEventListener("mousedown", function (event) { node.style.transition = "null"; var dispX = event.clientX - node.offsetLeft; var dispY = event.clientY - node.offsetTop; var move = function (event) { node.classList.add("hpop-panel-move"); node.style.left = event.clientX - dispX + "px"; node.style.top = event.clientY - dispY + "px"; } document.addEventListener("mousemove", move); document.addEventListener("mouseup", function () { node.classList.remove("hpop-panel-move"); node.style.transition = "0.3s"; document.removeEventListener("mousemove", move); var bodyWidth = document.body.clientWidth; var hpop_nodeWidth = node.offsetLeft + node.offsetWidth / 2; if (hpop_nodeWidth > bodyWidth / 2) { node.style.left = "auto"; node.style.right = 0; node.classList.remove("hpop-panel-left"); node.classList.add("hpop-panel-right"); hpop_config_custom.position.left = "auto"; hpop_config_custom.position.right = "0"; } else { node.style.left = hpop_config_custom.position.left = 0; node.style.right = hpop_config_custom.position.right = "auto"; node.classList.add("hpop-panel-left"); node.classList.remove("hpop-panel-right"); } hpop_config_custom.position.top = node.offsetTop; // 保存当前配置到本地缓存 GM_setValue("hpop_config", hpop_config_custom); }); }); // 给控制面板添加点击显示/隐藏效果 var toggleShowHide = function () { if (document.querySelector("#hpop-switch").checked) { document.querySelector("#hpop-switch").checked = false; $("img").show("500"); // 取消记忆本网站下次默认隐藏图片 hpop_config_custom.sitesHide.remove(document.location.hostname); } else { document.querySelector("#hpop-switch").checked = true; $("img").hide("500"); // 记忆本网站下次默认隐藏图片 hpop_config_custom.sitesHide.push(document.location.hostname); } // 保存当前配置到本地缓存 GM_setValue("hpop_config", hpop_config_custom); } node.addEventListener("click", toggleShowHide); node.querySelector("#hpop-switch").addEventListener("click", toggleShowHide); } })();