Greasy Fork is available in English.

跳过抖音广告

关闭抖音弹窗登录,跳过抖音广告和直播,观看直播默认原画

As of 05/06/2023. See the latest version.

// ==UserScript==
// @name         跳过抖音广告
// @namespace    https://greasyfork.org/zh-CN/users/1034730-%E9%9A%8F%E7%BC%98%E7%8E%A9%E5%AE%B6?locale_override=1
// @version      0.23
// @description  关闭抖音弹窗登录,跳过抖音广告和直播,观看直播默认原画
// @author       随缘玩家
// @license      随缘玩家
// @match        https://www.douyin.com/
// @match        https://live.douyin.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  let url = window.location.host;
  if (url == "www.douyin.com") {
    let next;
    let configxgIcon = [
      { name: "图片", option: true, type: "tupian" },
      { name: "清屏", option: false, type: "qingping" },
      { name: "评论区", option: false, type: "comment" },
      { name: "直播", option: true, type: "live" },
      { name: "右侧", option: true, type: "right" },
      { name: "底部", option: true, type: "bottom" },
      { name: "购物", option: true, type: "shopping" },
      { name: "提示", option: true, type: "tips" },
      { name: "回退", option: true, type: "rollback" },
    ]
    if (!localStorage.getItem("xg-icon")) {
      localStorage.setItem("xg-icon", JSON.stringify(configxgIcon))

    } else if (JSON.parse(localStorage.getItem("xg-icon")).length != configxgIcon.length) {
      localStorage.setItem("xg-icon", JSON.stringify(configxgIcon))
    } else {
      configxgIcon = JSON.parse(localStorage.getItem("xg-icon"))
    }
    let observer = new MutationObserver((e) => {

      e.forEach((i) => {
        if (i.addedNodes.length > 0 && i.addedNodes[0].className) {
          let getClass = i.addedNodes[0].classList[0]
          if (getClass != undefined) {
            if (getClass == "OFZHdvpl" && document.querySelector('[data-e2e="feed-active-video"] ._QjzkgP3.Vw_S4MTA.isDark') != null && configxgIcon[2].option) {
              let commentsOff = document.querySelector('[data-e2e="feed-active-video"] ._QjzkgP3.Vw_S4MTA.isDark.z_zS3jcn.dLCldFlr') || document.querySelector('[data-e2e="feed-active-video"] .aDQw7OWI._628BWQES.JFWzESlW.LookModalFrameFast')
              if (commentsOff) {
                setTimeout(() => {
                  i.addedNodes[0].querySelector(".pBxTZJeH.Qz1xVpFH.aLzJ7lUV").click()
                }, 200);
              }
            }
            if (getClass == "gear") {
              let clear = i.addedNodes[0].querySelectorAll(".virtual .item")
              if (clear[0] != null && clear[0].classList.length <= 1 && clear[0].querySelectorAll("span")[1] == undefined) {
                clear[0].click()
              }
              if (clear[0].querySelectorAll("span")[1] != undefined && clear[2].classList.length <= 1) {
                clear[2].click()
              }
              if (document.querySelector("[data-e2e='feed-active-video']") && !document.querySelector("[data-e2e='feed-active-video'] [data-peizhi]")) {
                let config = document.querySelector("[data-e2e='feed-active-video'] .xg-right-grid")
                let xgIcon = document.createElement("xg-icon")
                xgIcon.className = "xgplayer-autoplay-setting automatic-continuous"
                xgIcon.innerHTML = `<div class="xgplayer-icon" data-e2e="video-player-auto-play" data-e2e-state="video-player-auto-playing"><div class="xgplayer-setting-label"><span class="xg-switch-inner"></span></button><span class="xgplayer-setting-title" data-peizhi>配置</span></div></div><div class="xgTips">
 
   </div>`
                for (const item of configxgIcon) {
                  let icon = `<div class="xgplayer-icon" data-e2e="video-player-auto-play" data-e2e-state="video-player-auto-playing"><div class="xgplayer-setting-label"><button data-type="${item.type}" aria-checked="${item.option}" class="${item.option ? "xg-switch-checked" : ""} xg-switch" aria-labelledby="xg-switch-pip" type="button"><span class="xg-switch-inner"></span></button><span class="xgplayer-setting-title">${item.name}</span></div></div>`
                  let range = document.createRange()
                  let iconitem = range.createContextualFragment(icon)
                  let button = iconitem.querySelector("button");
                  let xgTips = xgIcon.querySelector(".xgTips")
                  xgTips.style.display = "none"
                  xgTips.style.visibility = "visible"
                  xgTips.style.bottom = 31 + "px"
                  button.onclick = () => {
                    if (item.option == true) {
                      item.option = false
                      button.classList.remove("xg-switch-checked")
                      button.setAttribute("aria-checked", "false")
                      localStorage.setItem("xg-icon", JSON.stringify(configxgIcon))
                    } else {
                      item.option = true
                      button.classList.add("xg-switch-checked")
                      button.setAttribute("aria-checked", "true")
                      localStorage.setItem("xg-icon", JSON.stringify(configxgIcon))
                    }
                  }
                  xgIcon.onmouseover = () => {
                    xgTips.style.display = "block"
                  }
                  xgIcon.onmouseleave = () => {
                    xgTips.style.display = "none"
                  }
                  xgTips.appendChild(iconitem)
                }
                config?.appendChild(xgIcon)
              }
            }
            if (getClass == "mPWahmAI") {
              let loginOff = i.addedNodes[0].querySelector(".box-align-center .dy-account-close")
              if (loginOff != null) {
                loginOff.click()
              }

            }
            if (getClass == "xgplayer-playswitch") {
              i.addedNodes[0].style.opacity = 0
            }
          }
        }
      })
    })
    observer.observe(document, { childList: true, subtree: true })
    //广告
    let body;
    let time;
    let VideoNumber = []
    let bodyTime
    function Tips(tipsText) {
      let RootVideo = document.querySelector(".YwClj8rK.fullscreen_capture_feedback")
      let TipsBox = document.createElement("div")
      TipsBox.style = `
          position: absolute;
          left: 0;
          right: 0;
          bottom: 90px;
          margin: 0 auto;
          text-align: center;
          width: 300px;
          height: 30px;
          line-height: 30px;
          background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
          cursor: pointer;
          z-index: 99999999999999999999999999;
          opacity:.9;
          transition: all 1s
          `
      TipsBox.innerText = "已为您跳过了" + tipsText
      RootVideo.appendChild(TipsBox)
      setTimeout(() => { TipsBox.style.opacity = 0 }, 500);
      setTimeout(() => { TipsBox.remove() }, 1500);
    }

    function skip(item, index) {
      clearInterval(time)
      if (configxgIcon[8].option && VideoNumber.includes(index)) return
      if (VideoNumber.length > 20) VideoNumber = []
      if (index && !VideoNumber.includes(index)) VideoNumber.push(index)
      let videos = document.querySelector("[data-e2e='feed-active-video'] .slider-video")
      if (videos != null) {
        if (!configxgIcon[8].option) videos.style.display = "none"
        if (videos.querySelector("video") != null) videos.querySelector("video").pause()
      }
      let zhibonode = document.querySelector("[data-e2e='feed-live'] video")
      if (zhibonode != null && zhibonode.style.display == "") {
        if (!configxgIcon[8].option) zhibonode.style.display = "none"
        zhibonode.pause()
      }
      if (configxgIcon[7].option) {
        Tips(item)
      }
      time = setInterval(() => {
        next.click()
      }, 3);

    }
    bodyTime = setInterval(() => {
      body = document.querySelector(".u0N5WOYm.Srjrb_Q0") || document.querySelector(".swiper-wrapper")
      next = document.querySelectorAll(".xgplayer-playswitch-next")[0]
      if (body && next) {
        clearInterval(bodyTime)
        let observers = new MutationObserver((e) => {
          e.forEach((i) => {
            if (i.target.attributes[0].nodeValue == 'feed-active-video') {
              clearInterval(time)
              let guanggao = i.target.querySelector(".video-info-detail .SlSbcMqT.FxjGh3L8.alznRtIZ")
              let tupian = i.target.querySelector(".video-info-detail .ysRqqDJY")
              let gouwu = i.target.querySelector(".tSXOCvQc .xgplayer-shop-anchor")
              if (tupian != null && configxgIcon[0].option) {
                skip("图片", i.target.dataset.e2eVid)
              }
              if (guanggao != null) {
                skip("广告", i.target.dataset.e2eVid)
              }
              if (gouwu != null && configxgIcon[6].option) {
                skip("购物广告", i.target.dataset.e2eVid)
              }
              if (i.target.querySelector(".tSXOCvQc") != null && configxgIcon[1].option) {
                i.target.querySelector(".tSXOCvQc").style.display = "none"
              }

              if (i.target.querySelector(".tSXOCvQc") != null && configxgIcon[4].option) {
                let rightHide = i.target.querySelector(".L1TH4HdO.d6KxRih3.positionBox") || i.target.querySelector(".L1TH4HdO.positionBox")
                rightHide.style.opacity = 0
              }
              if (i.target.querySelector(".Xu8BXy_K") != null && configxgIcon[5].option) {
                let imgBackground = i.target.querySelector(".Xu8BXy_K")
                imgBackground.style.display = "none"
              }

              if (i.target.querySelector(".xg-video-container") != null && configxgIcon[5].option) {
                let time
                let video = i.target.querySelector(".xg-video-container")
                let qingping = i.target.querySelector(".tSXOCvQc")
                let xgplayer = document.querySelector("[data-e2e='feed-active-video'] .xgplayer-controls")
                let bottomItem = xgplayer.querySelector(".xg-inner-controls.xg-pos")
                video.style.height = '99%'
                video.style.cursor = "none"
                video.style.bottom = 0
                video.style.margin = "auto 0"
                qingping.style.bottom = "6px"
                bottomItem.style.bottom = -bottomItem.offsetHeight + "px"
                bottomItem.style.transition = "all 0.3s"
                xgplayer.style.backgroundImage = "none"
                if (qingping.querySelector(".video-info-mask")) {
                  qingping.querySelector(".video-info-mask").remove()
                }
                let CommentItem = i.target.querySelectorAll(".UXyEyqbq.UdkDK3ea.DZKZZklc .otZjcQr3")
                if (CommentItem) {
                  CommentItem.forEach((i) => {
                    i.remove()
                  })
                }
                xgplayer.onmouseover = () => {
                  clearTimeout(time)
                  bottomItem.style.bottom = 0 + "px"
                  document.querySelector("[data-e2e='feed-active-video'] .tSXOCvQc").style.display = "none"
                }
                xgplayer.onmouseleave = () => {
                  clearTimeout(time)
                  time = setTimeout(() => {
                    bottomItem.style.bottom = -bottomItem.offsetHeight + "px"
                    if (!configxgIcon[1].option) {
                      document.querySelector("[data-e2e='feed-active-video'] .tSXOCvQc").style.display = ""
                    }
                  }, 1000);
                }
              }

            }
            if (document.querySelector("[data-e2e='feed-active-video']") == null && configxgIcon[3].option) {
              skip("直播", document.querySelector("[data-e2e='feed-live'] .Nu66P_ba").innerText)
            }
          })
        })
        observers.observe(body, { attributeOldValue: true, attributes: true, subtree: true, attributeFilter: ['data-e2e'] })
      }
      else {
        return
      }
    }, 500);
  }
  if (url == "live.douyin.com") {
    window.onload = function () {
      let getDom = setInterval(() => {
        let getDomTrue = document.querySelector(".xg-right-grid").innerHTML.includes("配置")
        if (getDomTrue) {
          clearInterval(getDom)
          return
        }
        let liwu = document.querySelector('.ruqvqPsH')
        if (liwu) {
          liwu.style.display = 'none'
        }
        let inall = document.querySelectorAll('.fHknbHHl').length > 0 ? document.querySelectorAll('.fHknbHHl') : document.querySelectorAll('.tY7dfAwO')
        let quanping = inall[1]?.querySelectorAll('div')[1]
        let pbliwu = inall[3]?.querySelectorAll('div')[1]
        quanping.click()
        let size = document.querySelector('.FKQqfehj')
        size.style.height = '99.8%'
        let danmus = document.querySelector('.basicPlayer.xgplayer .xgplayer-danmu')
        danmus.style.top = '0px'
        let vid = document.querySelector('.Zs4Pv2bD')
        let time;
        vid.onmousemove = function () {
          clearInterval(time)
          vid.style.cursor = 'auto'
          time = setInterval(function () {
            vid.style.cursor = 'none'
          }, 2000)
        }
        let huazhi = document.querySelectorAll('.J1hZCcks .LH4RopWg')[0] || document.querySelectorAll('.iDyiVE4p.bWLQ7URi .LgIvTpnw')[0]
        huazhi.click()

        let config = [
          { name: "弹幕", option: true },
          { name: "特效", option: true },
        ]
        if (!localStorage.getItem("xg-icon")) {
          localStorage.setItem("xg-icon", JSON.stringify(config))
        }
        else if (JSON.parse(localStorage.getItem("xg-icon")).length === config.length) {
          config = JSON.parse(localStorage.getItem("xg-icon"));
        } else {
          localStorage.setItem("xg-icon", JSON.stringify(config))
        }
        let bottom = document.querySelector(".xgplayer-controls.control_autohide.xgplayer-controls-initshow .xg-right-grid")
        let danmu = bottom.querySelector(".danmu-icon")
        let div = document.createElement("xg-icon")
        div.dataset.index = 1
        div.classList.add("tGBLU2eM")
        div.innerHTML = `
        <div class="tY7dfAwO">
        <div class="xuUeCQbT Gv4jhKug">
 
        </div>
        <div>配置</div>
        </div>`
        for (const item of config) {
          let label = document.createElement("label")
          let input = document.createElement("input")
          let span = document.createElement("span")
          input.type = "checkbox"
          input.checked = item.option
          span.innerText = item.name
          label.appendChild(input)
          label.appendChild(span)
          div.querySelector(".xuUeCQbT.Gv4jhKug").appendChild(label)
          div.querySelector(".xuUeCQbT.Gv4jhKug").style.bottom = "26px"
          label.onclick = function () {
            item.option = input.checked
            localStorage.setItem("xg-icon", JSON.stringify(config))
            window.location.reload()
          }
        }
        div.onmousemove = () => {
          div.querySelector(".xuUeCQbT.Gv4jhKug").style.display = "block"
        }
        div.onmouseleave = () => {
          div.querySelector(".xuUeCQbT.Gv4jhKug").style.display = "none"
        }
        bottom.appendChild(div)

        let liveVoice = document.querySelector(".xgplayer-volume").dataset.state
        if (liveVoice == "mute") {
          document.querySelector(".xgplayer-volume .xgplayer-icon").click()
        }
        if (config[0].option) {
          danmu.click()
        }
        if (config[1].option) {
          pbliwu.click()
        }
        if (document.querySelector(".xgplayer-play").dataset.state == "pause") {
          document.querySelector(".xgplayer-play").click()
        }

      }, 3200);
    }
  }
})();