DisableScrollScript

禁止滚动

当前为 2024-11-17 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         DisableScrollScript
// @namespace    http://tampermonkey.net/
// @version      2024.11.18
// @description  禁止滚动
// @author       荷塘月色
// @include      *://*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
  const wrapper = document.createElement('div')
  document.body.appendChild(wrapper)
  wrapper.classList.add('custom-wrapper')
  const dragEl = document.createElement('div')
  dragEl.classList.add('custom-drag-element')
  const defaultStyleClass = document.createElement('style')
  const hiddenStyleClass = document.createElement('style')
  const button = document.createElement('button')
  wrapper.appendChild(button)
  wrapper.appendChild(dragEl)
  document.head.appendChild(defaultStyleClass)
  button.innerText = '锁定'
  button.classList.add('custom-button')

  const offset = { x: 0, y: 0 }
  let isDragging = false

  dragEl.addEventListener('touchstart', e => {
    isDragging = true
    const touch = e.touches[0] // 获取第一个触摸点
    offset.x = touch.clientX - wrapper.getBoundingClientRect().left
    offset.y = touch.clientY - wrapper.getBoundingClientRect().top
    e.preventDefault()
  })

  dragEl.addEventListener('mousedown', (ev) => {
    isDragging = true
    offset.x = ev.clientX - wrapper.getBoundingClientRect().left
    offset.y = ev.clientY - wrapper.getBoundingClientRect().top
  })

  document.addEventListener('mousemove', function(e) {
    if (isDragging) {
      wrapper.style.left = (e.clientX - offset.x) + 'px'
      wrapper.style.top = (e.clientY - offset.y) + 'px'
    }
  })

  document.addEventListener('mouseup', function() {
    isDragging = false
  })

  document.addEventListener('touchmove', function(e) {
    if (isDragging) {
      const touch = e.touches[0] // 获取第一个触摸点
      wrapper.style.left = (touch.clientX - offset.x) + 'px'
      wrapper.style.top = (touch.clientY - offset.y) + 'px'
    }
  })

  document.addEventListener('touchend', function() {
    isDragging = false
  })

  button.addEventListener('click', () => {
    if (document.head.contains(hiddenStyleClass)) {
      document.head.removeChild(hiddenStyleClass)
      button.innerText = '锁定'
    } else {
      document.head.appendChild(hiddenStyleClass)
      button.innerText = '解锁'
    }
  })

  function Camel2Kebab(string) {
    return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
  }

  /* 通用 JSON 转 CSS 方法 */
  function Json2Css(complexStyleObject) {
    let cssString = ''

    for (const selector in complexStyleObject) {
      if (selector.startsWith('@')) { /* 处理媒体查询和其他@规则 */
        cssString += `${selector} { `
        for (const innerSelector in complexStyleObject[selector]) {
          cssString += `${innerSelector} { `
          const styles = complexStyleObject[selector][innerSelector]
          for (const prop in styles) {
            cssString += `${Camel2Kebab(prop)}: ${styles[prop]}; `
          }
          cssString += '} '
        }
        cssString += '} '
      } else {
        cssString += `${selector} { `
        const styles = complexStyleObject[selector]
        for (const prop in styles) {
          cssString += `${Camel2Kebab(prop)}: ${styles[prop]}; `
        }
        cssString += '} '
      }
    }

    return cssString
  }

  const hiddenStylesJSON = {
    '*': {
      // 'overflow': 'hidden !important',
      'touch-action': 'none'
    }
  }
  hiddenStyleClass.innerHTML = `${Json2Css(hiddenStylesJSON)}`

  const defaultStylesJSON = {
    '.custom-button': {
      'padding': '0.25em 1em',
      'border-radius': '4px',
      'border': '1px solid #000',
      'cursor': 'pointer'
    },
    '.custom-wrapper': {
      'zIndex': '10000',
      'position': 'fixed',
      'top': '10px',
      'left': '10px',
      'padding': '4px',
      'display': 'flex',
      'flexDirection': 'column',
      'gap': '4px',
      'background': 'rgb(255,255,255)',
      'border-radius': '4px',
      'border': '1px solid #000'
    },
    '.custom-drag-element': {
      'width': '100%',
      'height': '4px',
      'background': 'rgba(0,0,0,0.2)',
      'border-radius': '4px',
      'cursor': 'pointer'
    }
  }
  defaultStyleClass.innerHTML = `${Json2Css(defaultStylesJSON)}`

  // 确保按钮能在页面里
  setInterval(() => {
    if (!document.body.contains(wrapper)) {
      document.body.appendChild(wrapper)
    }
  }, 100)
})()