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

[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);
							} else {
								var a = $('#videoExplorer a[href*="watch/' + id + '"]');
								var icon = a.find('.cacheIcon[src]').get(0);
								if (icon) {
									item.cache = showCacheIcon(target, icon.src);
								} else {
									checkCached(id, function(cache) {
										item.cache = showCacheIcon(target, cache);
									});
								}
							}
						}
					}
				}, 50);
			}
		});
		function checkCached(id, callback) {
			var cache = 'none';
			$.get('/cache/info?' + id, function(data) {
				var info = data[id];
				if (info != null && !info['caching'] && !info['normalSize']) {
					cache = info['economy'] ? 'economy' : 'icon';
				}
				callback(cache);
			});
			
		}
	});
	function showCacheIcon(target, src) {
		var cache = 'none';
		if (target && typeof src === 'string' && src != 'none') {
			if (typeof target === 'string') {
				target = $('a[href*="watch/' + target + '"]');
			}
			if (target.length > 0) {
				if (src == 'icon' || src == 'economy') {
					cache = src;
					src = 'http://www.nicovideo.jp/local/cache' + src + '.gif';
				} else if (src.indexOf('icon') >= 0) {
					cache = 'icon';
				} else if (src.indexOf('economy') >= 0) {
					cache = 'economy';
				}
				target.find('.cacheIcon').attr('src', src).show();
				target.find('span.title').css('color', cache != 'economy' ? '#C00000' : '#C08000');
			}
		}
		return cache;
	}
});
>
