HideImg

隐藏图片

目前為 2023-09-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @license MIT
// @name        HideImg
// @namespace   ojer
// @match       *://*/*
// @version     1.0
// @author      ojer
// @description 隐藏图片
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// @grant       GM_unregisterMenuCommand
// @grant       GM_addValueChangeListener
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// ==/UserScript==

const { register } = VM.shortcut

const styleHide = 'visibility: hidden !important;'

const dataKey = 'HI_IS_HIDE'
const captionHideKey = 'HideImg (Ctrl+Shift+I)'
const captionShowKey = 'ShowImg (Ctrl+Shift+I)'

const hide = () => {
  regMenu(true)
  document.querySelectorAll('img,video').forEach((item) => {
    item.style = item.getAttribute('style') + ';' + styleHide
  })
}

const show = () => {
  regMenu(false)
  document.querySelectorAll('img,video').forEach((item) => {
    let style = item.getAttribute('style')
    if (style) {
      item.style = style.replaceAll(styleHide, '')
    }
  })
}

const listenerId = GM_addValueChangeListener(dataKey, (name, oldValue, newValue, remote) => {
  console.log(oldValue, newValue)
  if (oldValue && !newValue) {
    show()
  } else if (!oldValue && newValue) {
    hide()
  }
})
const regMenu = (isShow) => {
  GM_unregisterMenuCommand(isShow ? captionHideKey : captionShowKey)
  GM_registerMenuCommand(isShow ? captionShowKey : captionHideKey, () => {
    GM_setValue(dataKey, !isShow)
  })
}

register('c-s-i', () => {
  GM_setValue(dataKey, !GM_getValue(dataKey))
})

const main = () => {
  if (GM_getValue(dataKey, undefined) === undefined) {
    GM_setValue(dataKey, true)
  }
  if (GM_getValue(dataKey)) {
    hide()
  } else {
    show()
  }
}
main()