# nlフィルタ定義(文字コード判定用なのでこの行は削除しないこと)
# reEncodingChecker.txt 17-06-01
# 参考: movieFetcherFilter.txt, 15_thumbInfoFilter(基本).txt

[Replace]
Name = JavaScript埋め込み
URL = (?:www|ext|ch|com)\.nicovideo\.jp/
ContentType = text/html
Match<
(?=</head>)
>
Replace<
<script type="text/javascript" src="/$TS(local/reEncodingChecker.js)"></script><CRLF>
>

[Replace]
Name = 再エンコード判定情報を付加(mylist)
URL = www\.nicovideo\.jp/(?:mylist/\d+|my/mylist|api/(?:deflist|mylist|mymemory)/list)
Multi = TRUE
Match<
"video_id":\s*"([a-z]{2}\d+)"
>
Replace<
$0,"reencoded":$REENCODED($1)
>

[Replace]
Name = 再エンコード判定情報を付加(recommendations)
URL = www\.nicovideo\.jp/(?:|api/)recommendations
Multi = TRUE
Match<
"id":\s*"([a-z]{2}\d+)"
>
Replace<
$0,"reencoded":$REENCODED($1)
>

[Script]
Name = 再エンコード判定情報を付加(ニコレポ)
URL = www\.nicovideo\.jp/(?:my|user/\d+)(?:/?(?:\?|$)|/top)
Append<
NicoCache_nl.moHooks.addLazy(function(item, info) {
  var thumb = item.querySelector(".log-target-thumbnail img");
  if (thumb == null) return;
  var anchor = thumb.parentElement;
  var logTargetThumbnail = anchor.parentElement;
  
  var m = anchor.href.match(/watch\/(\w{2}\d+)/);
  if (!m) return;
  var videoId = m[1];
  logTargetThumbnail.style.height = "auto";
  logTargetThumbnail.insertAdjacentHTML("beforeEnd",
    '<div style="width: 100%; background: white; display: inline-block;"><span style="font-size:10px;">' + 
    (info == null || info.reEncoded == null ? '<a href="javascript:void(0);" id="reencoded_' + videoId + '" onclick="checkReEncoding(this,\'' + videoId + '\')">is re-encoded?</a>':
     info.reEncoded ? "reencoded" : "RAW") +
    '</span></div>');
});
>

[Script]
Name = 再エンコード判定情報を付加(タグレポ)
URL = www\.nicovideo\.jp/my/tagrepo/
Append<
NicoCache_nl.moHooks.addLazy(function(item, info) {
  var thumb = item.querySelector(".contents-thumbnail-box img");
  if (thumb == null) return;
  var anchor = thumb.parentElement;
  var contentsThumbnailBox = item.querySelector(".contents-thumbnail-box");
  
  var m = anchor.href.match(/watch\/(\w{2}\d+)/);
  if (!m) return;
  var videoId = m[1];
  contentsThumbnailBox.insertAdjacentHTML("afterEnd",
    '<div style="width: 100%; background: white; display: inline-block;"><span style="font-size:10px;">' + 
    (info == null || info.reEncoded == null ? '<a href="javascript:void(0);" id="reencoded_' + videoId + '" onclick="checkReEncoding(this,\'' + videoId + '\')">is re-encoded?</a>':
     info.reEncoded ? "reencoded" : "RAW") +
    '</span></div>');
});
>

[Replace]
Name = 再エンコード判定リンク追加(投稿動画)
URL = www\.nicovideo\.jp/(?:my|user/\d+)/video
Multi = TRUE
Match<
<a href="watch/(\w{2}\d+)"[^>]*><img[^>]+class="video"\s*/?></a>\s*
\s*(?s:<div class="cacheIcon[^"]*">.*?</div>)?
\s*(?:<span class="videoTime[^<]+</span>)
>
Replace<
$0
<span style="font-size:10px;"><nlcase "$REENCODED($1)"><when "true">reencoded<when "false">RAW<when "null"><a href="javascript:void(0);" id="reencoded_$1" onclick="checkReEncoding(this,'$1')">is re-encoded?</a></nlcase></span>
>

[Replace]
Name = 再エンコード判定リンク追加(mylist)
URL = www\.nicovideo\.jp/mylist/\d+
Match<
<p class="vinfo_length">.*?</p>(?s:\s*\{if \$item\.item_data\.cache\}.*?\{/if\})?
>
Replace<
$0
<span style="font-size:10px;">{if \$item.item_data.reencoded == null}<a href="javascript:void(0);" id="reencoded_{\$item.item_data.video_id}" onclick="checkReEncoding(this,'{\$item.item_data.video_id}')">is re-encoded?</a>{elseif \$item.item_data.reencoded}reencoded{else}RAW{/if}</span>
>

[Replace]
Name = 再エンコード判定リンク追加(my/mylist)
URL = www\.nicovideo\.jp/my/mylist
Match<
<img[^>]+item_data\.thumbnail_url[^>]+class="jsLazyImage"\s*/?>\s*
(?:\{if[^<]+<span class="videoTime[^<]+</span>\{/if\}\s*)?[\s\S]*?</a>
>
Replace<
$0
<span style="font-size:10px;">{if \$item.item_data.reencoded == null}<a href="javascript:void(0);" id="reencoded_{\$item.item_data.video_id}" onclick="checkReEncoding(this,'{\$item.item_data.video_id}')">is re-encoded?</a>{elseif \$item.item_data.reencoded}reencoded{else}RAW{/if}</span>
>

[Replace]
Name = 再エンコード判定リンク追加(recommendations)
URL = www\.nicovideo\.jp/recommendations
Match<
<img[^>]+src="\{\$item\.thumbnail_url\}"[^>]*>\s*</a>\s*<span[^>]*>\{\$item\.length\}</span>(?s:\{if \$item\.cache\}.*?\{/if\})?\s*</div>
>
Replace<
$0
<span style="font-size:10px;">{if \$item.reencoded == null}<a href="javascript:void(0);" id="reencoded_{\$item.id}" onclick="checkReEncoding(this,'{\$item.id}')">is re-encoded?</a>{elseif \$item.reencoded}reencoded{else}RAW{/if}</span>
>

[Replace]
Name = 再エンコード判定リンク追加(検索用)
URL = www\.nicovideo\.jp/(?:search/|tag/|newarrival|recent|hotlist)
Require = !Cookie\.set\('col',\s*'2'
Multi = TRUE
Match<
(?s:<div class="itemThumbBox"><div class="itemThumb"[^>]*data-id="([^"]+)".+?(<div class="cacheIcon[^"]*"></div>)?</div>.+?</div>)
>
Replace<
$0
<span style="font-size:10px;"><nlcase "$REENCODED($1)"><when "true">reencoded<when "false">RAW<when "null"><a href="javascript:void(0);" id="reencoded_$1" onclick="checkReEncoding(this,'$1')">is re-encoded?</a></nlcase></span>
>

[Replace]
Name = 再エンコード判定リンク追加(カテゴリ・ランキング)
URL = www\.nicovideo\.jp/ranking
Multi = TRUE
Match<
(?s:<div class="itemThumbBox"><div class="itemThumb"[^>]*data-id="([^"]+)".+?(<div class="cacheIcon[^"]*"></div>)?</div>.+?</div>)
>
Replace<
$0
<span style="font-size:10px;"><nlcase "$REENCODED($1)"><when "true">reencoded<when "false">RAW<when "null"><a href="javascript:void(0);" id="reencoded_$1" onclick="checkReEncoding(this,'$1')">is re-encoded?</a></nlcase></span>
>
