// ==UserScript==
// @name         美剧天堂：一键复制所有下载链接
// @namespace    https://github.com/patsoncy/mjtt
// @version      0.1.1
// @description  在剧集页面右边增加一排按钮，点击按钮可以复制改版本的所有下载链接
// @author       patsoncy
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser-polyfill.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.min.js
// @match        http://www.meijutt.com/content/*
// @grant        GM_setClipboard
// ==/UserScript==

/* jshint ignore:start */
var inline_src = (<><![CDATA[
    /* jshint ignore:end */
    /* jshint esnext:true */

    const downloadBlockSelector = 'div.o_list_cn_r',
    	  checkboxSelector      = 'input[type=checkbox]',
          btnsBlockStyle        = 'width:200px;position:fixed;right:0;top:40%;z-index:999',
          btnStyle              = 'display:block;width:100%;padding:20px;color:#f34d41;background:#efefef';

    class MJTT {
    	constructor(){
    		this.nodes = [];
    	}

    	init(){
    		let self = this,
    		    downloadBlocks = [].slice.apply(document.querySelectorAll(downloadBlockSelector))
                                  .filter((block) => block.querySelectorAll(checkboxSelector).length > 0);

    		self.nodes = downloadBlocks.map((block,index) => {
                let checkboxs = [].slice.apply(block.querySelectorAll(checkboxSelector));
    			return {
                    firstLineDesc : block.innerText.split('\n')[0],
                    downloadLinks : self.uniq(checkboxs.map((checkbox) => checkbox.value))
                }
    		})
    		return self;
    	}

    	uniq(arr){
    		let tmp = {},
    			newArr = [];
    		for (var i = 0; i < arr.length; i++) {
    			let t = arr[i];
    			if(!tmp[t]){
    				tmp[t] = 1;
    				newArr.push(t);
    			}
    		}
    		return newArr;
    	}

    	draw(){
            let body = document.body,
                firstNode = body.children[0],
                btnsBlock = document.createElement('div');
                btnsBlock.style.cssText = btnsBlockStyle;
            for (let i = 0; i < this.nodes.length; i++) {
                let btnObj = this.nodes[i];
                let btn = document.createElement('button');
                btn.innerHTML = btnObj.firstLineDesc + '(一共' + btnObj.downloadLinks.length + '集)';
                btn.value = btnObj.downloadLinks.join('\n');
                btn.addEventListener('click',this.handleClick);
                btn.style.cssText = btnStyle;
                btnsBlock.appendChild(btn);
            };
            body.insertBefore(btnsBlock,firstNode);
    	}

        handleClick(e){
            GM_setClipboard(e.target.value);
            let p = document.createElement('p');
            p.style.color = 'red';
            p.innerHTML = 'copied!';
            e.target.parentNode.appendChild(p);
        }
    }

    new MJTT().init().draw();

    /* jshint ignore:start */
]]></>).toString();
var c = babel.transform(inline_src);
eval(c.code);
/* jshint ignore:end */
@@ -115,17 +115,17 @@
     0.1.
-1
+2
 %0A// @des
@@ -160,14 +160,15 @@
 %E5%8F%AF%E4%BB%A5%E5%A4%8D%E5%88%B6
-%E6%94%B9%E7%89%88%E6%9C%AC%E7%9A%84
+%E5%AF%B9%E5%BA%94%E5%9D%97%E4%B8%8B
 %E6%89%80%E6%9C%89
+%E7%9A%84
 %E4%B8%8B%E8%BD%BD%E9%93%BE%E6%8E%A5
@@ -683,16 +683,19 @@
      = '
+li 
 input%5Bty
@@ -2232,16 +2232,24 @@
 rHTML = 
+'%E7%82%B9%E5%87%BB%E5%A4%8D%E5%88%B6%22'+
 btnObj.f
@@ -2264,16 +2264,19 @@
 Desc + '
+%22%E9%83%A8%E5%88%86
 (%E4%B8%80%E5%85%B1' + b

fix bug