# nlフィルタ定義(文字コード判定用なのでこの行は削除しないこと)
# 17/02/02

#-----------------------------------------------------------------------------------------
[Replace]
Name = キャッシュ済動画のリンク色変更(watch)
URL = (?:www|news)\.nicovideo\.jp/watch/
Multi = TRUE
idGroup = 2
Match<
(href="(?:|/|http://www\.nicovideo\.jp/)watch/([a-z]{2}\d+)[^"]*"[^>]*)(>\s*)(?!<img|(?:一般|プレミアム)回線で視聴)
>
Replace<
$1 style="color:#<C00000$C08000>;"$3
>

#-----------------------------------------------------------------------------------------
# watchページのURLをリンク化
[Replace]
Name = watchURLtoLink
URL = www\.nicovideo\.jp/watch/
Multi = TRUE
Match<
(?i)h?(ttps?://[a-z0-9:./?!&\-~#=_%;+]+)(?![^<>]*>|[^<>]*</(?:a|textarea|script)>)
>
Replace<
<a href="h$1" target="_blank">$0</a>
>

#----------------------------------------------------------------------------------------
[Style]
Name = キャッシュアイコン位置(watchページ HTML5)
URL = www\.nicovideo\.jp/watch/
Require = "js-initial-watch-data"
Append<
	.PlaylistItemContent .cacheIcon { bottom:4px;}
>

#-----------------------------------------------------------------------------------------
[Script]
Name = javascript埋め込み(watchページ HTML5)
URL = www\.nicovideo\.jp/watch/
Require = "js-initial-watch-data"
Append<
document.addEventListener("DOMContentLoaded", function(){
 "use strict";

/* watchページ 再生リスト のキャッシュアイコンの表示＆色変え */
const
  $XHR = function(aUrl, resolve) {
    var httpObj = NicoCache_nl.xhr();
    httpObj.open('GET', aUrl, true);
    httpObj.onload = function() { resolve(httpObj); };
    // httpObj.onerror = function() { reject(httpObj.error); };
    httpObj.send();
  }
  ,init = function() {
    var PlaylistCardList = document.getElementsByClassName('PlaylistCardList');
    if(!PlaylistCardList.item(0)) return requestAnimationFrame(init);
    
    var
      RouterLink = Array.from(PlaylistCardList[0].getElementsByClassName('RouterLink'))
      ,smids = RouterLink.map(function(E) { return E.getAttribute('href').split('/')[2] })
      ,F = function (aElem, json) {
        var
          smid = aElem.getAttribute('href').split('/')[2]
          ,smidData = (JSON.parse(json.responseText))[smid];
        if (!smidData) return;
        var cacheData = smidData.preferredHTML5 ? smidData.caches[smidData.preferredHTML5] : null;
        if (!cacheData) return;
       
        var
          cache = ''
          ,color = '';
        if (true /*!cacheData.dmc*/) {
          if (!cacheData.economy) {
            cache = 'cache';
            color = '#C00000';
          } else {
            cache = 'economy';
            color = '#C08000';
          }
        } else {
          if (!cacheData.economy) {
            cache = 'dmcCache';
            color = '#008000';
          } else {
            cache = 'dmcEconomy';
            color = '#808000';
          }
        }
        
        aElem.getElementsByClassName('PlaylistItemContent-title')[0].style.color = color;
        aElem.getElementsByClassName('PlaylistItemContent-thumbnail')[0].insertAdjacentHTML("afterEnd",
          '<div class="cacheIcon ' + cache + 'IconImgMin"></div>');
      };
    
    $XHR('/cache/info/v2?' + smids.join(','), function(json) {
      RouterLink.map(function(i) { return F(i, json) });
    });
  };

init();

},false);
>

#-----------------------------------------------------------------------------------------
[Replace]
Name = キャッシュアイコンを追加(GINZA)
URL = www\.nicovideo\.jp/watch/
Require = res\.nimg\.jp/css/watch_zero/watch\.css\?
Multi = TRUE
Match<
(\s*)<img [^>]*class="(?:thumbnail|itemThumbnail)"\s*/?>
>
Replace<
$0$1<div class="cacheIcon"></div>
>

#-----------------------------------------------------------------------------------------
[Style]
Name = キャッシュアイコンを定義(GINZA)
URL = www\.nicovideo\.jp/watch/
Require = res\.nimg\.jp/css/watch_zero/watch
Append<
	#playlistContainer .playlistItem .thumbContainer .cacheIcon,
	#videoExplorer .cacheIcon {
		position:absolute; top:auto; bottom:0px; left:0px; z-index:1000; display:none;
	}
	#playlistContainer .playlistItem .thumbContainer .cacheInfo {
		position:absolute; bottom:-8px; padding:0 4px 0 4px; background-color:rgba(0,0,0,.6); color:white;
	}
>

#-----------------------------------------------------------------------------------------
[Script]
Name = キャッシュアイコンを表示(GINZA)
URL = www\.nicovideo\.jp/watch/
Require = res\.nimg\.jp/js/watch/require
Append<
require([
	'jquery',
	'watchapp/init/PlaylistInitializer',
	'watchapp/init/VideoExplorerInitializer',
	'WatchJsApi'
], function($, PlaylistInitializer, VideoExplorerInitializer) {
	WatchJsApi.onInitialized(function() {
		var playlist = PlaylistInitializer.playlist;
		var playlistData = JSON.parse($('#playlistDataContainer').text());
		if (playlistData) {
			$(playlistData.playlist).each(function(index, data) {
				if (data.has_data) {
					var item = playlist.getItemByWatchID(data.id);
					if (item && item.id) {
						if (data.cache) {
							item.cache = data.cache
						} else if (item.id.match(/^[a-z]{2}[0-9]+$/)) {
							item.cache = 'none';
						}
					}
				}
			});
		}
		VideoExplorerInitializer.videoExplorer.addEventListener('refreshEnd', function (e) {
			setTimeout(function() {
				$(e.content.getItems()).each(function(index, item) {
					showCacheIcon(item._id, item._seed.cache);
				});
			}, 500);
		});
		WatchJsApi.player.addEventListener('onVideoInitialized', function() {
			var item = playlist.getPlayingItem();
			checkCached(item.id, function(cache) {
				if (cache != 'none') {
					item.cache = showCacheIcon(item.id, cache);
				} else {
					var info = $('<p class="cacheInfo"></p>');
					var index = playlist.getPlayingIndex();
					if (index >= 0) {
						var thumb = $('#playlistContainer ul.playlistItemList>li').eq(index)
								.find('.thumbContainer');
						thumb.find('.cacheIcon').hide();
						thumb.append(info);
					}
					var npc = PlayerApp.ns.player.Nicoplayer.getInstance()._nicoplayerConnector;
					var timer = setInterval(function() {
						var ratio = npc.getVideoBufferedRatio();
						info.text(Math.floor(ratio * 100) + '%');
						if (ratio == 1) {
							clearInterval(timer);
							setTimeout(function() {
								checkCached(item.id, function(cache) {
									info.remove();
									item.cache = showCacheIcon(item.id, cache);
								});
							}, 1000);
						}
					}, 1000);
				}
			});
		});
		$('#playlistContainer ul.playlistItemList').on('DOMNodeInserted', function(e) {
			var target = $(e.target);
			if (target.hasClass('playlistItem')) {
				var timer = setInterval(function() {
					var id = target.attr('data-nicowatch-video-id');
					if (id) {
						clearInterval(timer);
						var item = playlist.getItemByWatchID(id);
						if (item) {
							if (item.cache) {
								showCacheIcon(target, item.cache);
							}
						}
					}
				}, 50);
			}
		});
		function checkCached(id, callback) {
			var cache = 'none';
			$.get('/cache/info/v2?' + id, function(data) {
				var info = data[id];
				if (info == null || info.preferredFlash == null) return;
				var cacheInfo = info.caches[info.preferredFlash];
				if (cacheInfo != null) {
					cache = (cacheInfo.economy ? 'economy' : 'cache');
				}
				callback(cache);
			});
			
		}
	});
	function showCacheIcon(target, cache) {
		if (target) {
			if (typeof target === 'string') {
				target = $('a[href*="watch/' + target + '"]');
			}
			if (target.length > 0 && cache !== "none") {
				target.find('.cacheIcon').addClass(cache + "IconImgMin").show();
				target.find('span.title').css('color',
				    cache == 'cache' ? '#C00000':
				    cache == 'economy' ? '#C08000' : '#C00000');
			}
		}
		return cache;
	}
});
>
