DisableScrollScript

禁止滚动

目前為 2024-11-17 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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)
})()