mesak / dmm18

// ==UserScript==
// @name         dmm18
// @namespace    dmm18
// @version      1.4
// @description  dmm18
// @author       Mesak
// @match        http://www.dmm.co.jp/*
// @match        http://www.dmm.com/*
// @include      http://www.dmm.com/*
// @include      http://www.dmm.co.jp/*
// @grant        none
// @license MIT
// ==/UserScript==
var urlPage = window.location.href;
Element.prototype.remove = function() {
    this.parentElement.removeChild(this);
};
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
    for(var i = 0, len = this.length; i < len; i++) {
        if(this[i] && this[i].parentElement) {
            this[i].parentElement.removeChild(this[i]);
        }
    }
};
NodeList.prototype.forEach = Array.prototype.forEach;
if( document.getElementById("welcome") ){
	document.getElementById("welcome").remove();
}
function isArray(object)
{
	return ('isArray' in Array) ? Array.isArray(object) : function (value) { return Object.prototype.toString.call(value) === '[object Array]';};
}
function create_download_link(no , title){
    title = typeof title === 'undefined' ? no : title;
    return '<a href="http://sukebei.nyaa.se/?page=search&term=' + no + '" target="_blank" title="' + title + '">' + title + '</a>';
}
function find_avnum( oALink )
{
	var result = '',av_number = oALink.getAttribute("href");
    //console.log( tmp )
    var no = getNum(av_number);
	if( no != av_number )
    {
		result = create_download_link(no);
	}
	return result;
}
function getNum( av_number ){
    var tmp = av_number.match(/cid=[\d+]{0,}([\d\w]+)/), no = av_number;
	if( isArray(tmp) )
    {
		no = tmp[1];
        tmp = no.toLowerCase().match(/([a-z]+)([0-9]+)/);
        if( tmp[2].length == 5 && tmp[2].substr(0,2) == '00')
        {
            no = tmp[1] + '-' + tmp[2].substr(2,3);
        }else{
            no = tmp[1] + '-' + tmp[2];
        }
        no = no.toUpperCase();
    }
    return no;
}
function urlHas( keyWord ){
	return (urlPage.indexOf(keyWord) != -1);
}
if( urlHas('/list/') ||  urlHas('/sort=') )
{
	if( urlHas('view=text') )
	{
		var row = document.querySelectorAll('.d-item tbody > tr[class]');
		row.forEach(function(tr,trindex){
			var oALink =  tr.querySelector('.ttl a');
			tr.querySelectorAll('td')[1].innerHTML = find_avnum( oALink );
		});
	}else{
		var row = document.querySelectorAll('.d-item #list > li');
		row.forEach(function(li,liindex){
			var oALink =  li.querySelector('.tmb a');
			li.querySelectorAll('.value .rate')[0].innerHTML  = find_avnum( oALink );
		});
	}
}

if( urlHas('/detail/') )
{
   var no =  getNum( urlPage );
   var tds = document.querySelectorAll('.page-detail .mg-b20 td');
   tds.forEach(function(td,tdindex){
       if( td.innerHTML.indexOf('品番') != -1 ){
           var title = tds[tdindex+1].innerHTML;
           tds[tdindex+1].innerHTML = create_download_link(no,title);
       }
   });
}
document.addEventListener("keydown", function(e){
    var oViewer  = document.getElementById('viewer');
    if( oViewer.style.display == 'inline')
    {
        //console.log(e);
        switch(e.keyCode)
        {
            case 39:
                document.getElementById('next_num').onclick();
                break;
            case 37:
                document.getElementById('back_num').onclick();
                break;
        }
    }

});