Nico User Ads Hider

ニコニ広告による動画への装飾を削除

As of 2017-11-06. See the latest version.

// ==UserScript==
// @name        Nico User Ads Hider
// @namespace   http://userscripts.org/users/121129
// @description ニコニ広告による動画への装飾を削除
// @match       *://www.nicovideo.jp/video_top*
// @match       *://www.nicovideo.jp/tag/*
// @match       *://www.nicovideo.jp/search/*
// @match       *://www.nicovideo.jp/ranking*
// @version     8
// @grant       none
// ==/UserScript==

;(function() {
  'use strict'
  var handler = function(handleForEachMutation) {
    return function(mutations) {
      for (var m of mutations) handleForEachMutation(m)
    }
  }
  var containsAllClass = function(elem, classTokens) {
    return classTokens.every(function(t) {
      return elem.classList.contains(t)
    })
  }
  var nicovideoObj = function() {
    var isUadVideoItem = function(target) {
      return target.hasAttribute('data-uad-video-item')
    }
    var isUadLevelClassAdded = function(mutation) {
      return mutation.attributeName === 'class'
          && mutation.target.className.includes('uadlevel')
    }
    var restoreIfUadLevelClassAdded = function(mutation) {
      if (!isUadVideoItem(mutation.target) && isUadLevelClassAdded(mutation)) {
        mutation.target.className = mutation.target.className.split(/\s+/).filter(function(v) {
          return !v.startsWith('uadlevel')
        }).join(' ')
      }
    }
    var isBalloonShown = function(mutation) {
      var e = mutation.target
      return mutation.attributeName === 'style'
          && e.style.display !== 'none'
          && containsAllClass(e, ['balloon', 'recent', 'active'])
    }
    var hideIfBalloonShown = function(mutation) {
      if (isBalloonShown(mutation)) mutation.target.style.display = 'none'
    }
    var isListRankingPage = function() {
      var p = location.pathname
      return p.startsWith('/ranking/') && p !== '/ranking/'
    }
    var isVideoCommentsShown = function(elem) {
      return elem.hasAttribute('data-video-comments')
          && elem.style.display !== 'none'
    }
    var hideIfVideoCommentsShown = function(mutation) {
      var e = mutation.target
      if (isVideoCommentsShown(e)) e.style.display = 'none'
    }
    var fs = [restoreIfUadLevelClassAdded, hideIfBalloonShown]
    if (isListRankingPage()) fs.push(hideIfVideoCommentsShown)
    return {
      handler: handler(function(mutation) {
        for (var f of fs) f(mutation)
      }),
      options: {
        attributes: true,
        subtree: true,
        attributeOldValue: true,
        attributeFilter: ['class', 'style'],
      },
    }
  }
  var observe = function(obj) {
    new MutationObserver(obj.handler).observe(document.body, obj.options)
  }
  observe(nicovideoObj())
})()