自动获取磁链接并自动离线下载

2015-05-31 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        挊
// @namespace   撸
// @description 自动获取磁链接并自动离线下载
// @include     http://www.javdog.com/*
// @include     http://www.avmask.com/*
// @include     http://www.javlibrary.com/*
// @include     http://www.libredmm.com/products/*
// @include     http://www.dmm.co.jp/digital/videoa/*
// @include     http://www.minnano-av.com/av*
// @include     http://www.oisinbosoft.com/dera/*
// @include     http://www.javbus.co/*
// @include     http://avdb.la/movie/*
// @include     http://www.141jav.com/view/*
// @include     http://av4you.net/work/*.htm
// @include     http://pan.baidu.com/disk/home
// @include     http://115.com/?tab=offline&mode=wangpan
// @include     http://cloud.letv.com/webdisk/home/index
// @include     https://www.furk.net/users/files/add
// @include     *.yunpan.360.cn/my/
// @include     http://www.btcherry.com/search?keyword=*
// @include     https://btdigg.org/search*
// @include     http://www.shousibaocai.com/search/*
// @include     http://www.shousibaocai.com/list/*
// @include     http://www.btspread.com/search/*
// @version     1.14
// @run-at      document-end
// @grant       GM_xmlhttpRequest
// @grant       GM_setClipboard
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_addStyle
// ==/UserScript==
var apply_search_site = true; //将脚本应用到搜索页面
var $ = function (selector) {
  var result = document.querySelectorAll(selector);
  if (selector[0] == '#') {
    return result[0];
  } 
  else {
    return result;
  }
};
var $after = function (target, newnode) {
  target.parentElement.insertBefore(newnode, target.nextElementSibling);
};
var $eafter = function (selector, target, func) {
  var result = document.querySelectorAll(selector);
  //isdeep 是否克隆子元素
  var tmp = null;
  for (var i = 0; i < result.length; i++) {
    tmp = target.cloneNode(true);
    if (result[i].nextElementSibling === null) {
      result[i].parentElement.appendChild(tmp);
    }
    else {
      result[i].parentElement.insertBefore(tmp, result[i].nextElementSibling);
    }
    tmp.setAttribute('data', func(result[i]));
  }
};
var $eappend = function (selector, target, func) {
  var result = document.querySelectorAll(selector);
  for (var i = 0; i < result.length; i++) {
    result[i].appendChild(target.cloneNode(true));
    func(result[i]);
  }
};
var xhr = function (url, cb) {
  GM_xmlhttpRequest({
    method: 'GET',
    url: url,
    onload: function (result) {
      console.log('番号搜索', url);
      cb(result.responseText);
    },
    onerror: function (e) {
      console.log(e);
    }
  });
};
var exist_magnet = function () {
  var magnet = GM_getValue('magnet');
  if (magnet) {
    GM_setValue('magnet', '');
    return magnet;
  } 
  else {
    return '';
  }
};
var insert_js = function (value, func) {
  var js = document.createElement('script');
  js.innerHTML = '(' + func.toString() + ')(\'' + value + '\');';
  document.body.appendChild(js);
};
var add_style = function (css) {
  if (css) {
    GM_addStyle(css);
  } 
  else {
    GM_addStyle([
    '#magnet-tab table{margin:10px auto;border:1px solid #cad9ea;color:#666;font-size:12px;text-align:center;}',
    '.magnet-th,.magnet-td{height:30px; border:1px solid #cad9ea;padding:0 1em 0;}',
    '.magnet-copy{color:#08c;}',
    '.magnet-download{color: #d22222;margin-right: 4px;border: solid 1px #000;}'
    ].join(''));
  }
};
var shorten_str = function (str) {
  return str.length > 30 ? str.slice(0, 30) + '...' : str;
};
//------------
var offline_data = {
  baidu: {
    url: 'http://pan.baidu.com/disk/home',
    name: '百度云',
  },
  115: {
    name: '115离线',
    url: 'http://115.com/?tab=offline&mode=wangpan'
  },
  letv: {
    name: '乐视云',
    url: 'http://cloud.letv.com/webdisk/home/index'
  },
  360: {
    name: '360云',
    url: 'http://yunpan.360.cn/my/'
  },
  furk: {
    name: 'Furk',
    url: 'https://www.furk.net/users/files/add'
  },
};
var init_offline = function (parent, child) {
  for (var key in offline_data) {
    var tmp = child.cloneNode();
    tmp.href = offline_data[key].url;
    tmp.textContent = offline_data[key].name;
    parent.appendChild(tmp);
  }
  return parent;
};
var init_search_offline = function (callback) {
  var link = document.createElement('a');
  link.className = 'magnet-download';
  link.target = '_blank';
  var offline = init_offline(document.createElement('div'), link);
  offline.className = 'offline-div';
  callback(offline);
  add_style();
  reg_event();
};
//------------
var create_table_th = function () {
  var tr = document.createElement('tr');
  var th = document.createElement('th');
  th.className = 'magnet-th';
  tr.appendChild((function () {
    var t = th.cloneNode();
    var a = document.createElement('a');
    a.id = 'switch_engine';
    a.href = 'javascript:void(0);';
    a.textContent = '标题';
    t.appendChild(a);
    return t;
  }) ());
  var strings = [
    '大小',
    '操作',
    '离线下载'
  ];
  for (var i = 0; i < strings.length; i++) {
    var t = th.cloneNode();
    t.textContent = strings[i];
    tr.appendChild(t);
  }
  return tr;
};
var create_table_td = function (data) {
  var tr = document.createElement('tr');
  var td = document.createElement('td');
  td.className = 'magnet-td';
  tr.appendChild((function () {
    var title = td.cloneNode();
    title.setAttribute('title', data.title);
    title.textContent = shorten_str(data.title);
    return title;
  }) ());
  tr.appendChild((function () {
    var size = td.cloneNode();
    size.textContent = data.size;
    return size;
  }) ());
  tr.appendChild((function () {
    var copy = td.cloneNode();
    var link = document.createElement('a');
    link.className = 'magnet-copy';
    link.textContent = '复制';
    link.href = 'magnet:?xt=urn:btih:' + data.hash;
    copy.appendChild(link);
    return copy;
  }) ());
  tr.appendChild((function () {
    var link = document.createElement('a');
    link.className = 'magnet-download';
    link.target = '_blank';
    var offline = init_offline(td.cloneNode(), link);
    offline.setAttribute('data', 'magnet:?xt=urn:btih:' + data.hash);
    return offline;
  }) ());
  return tr;
};
var create_wrapper = function (data) {
  var table = document.createElement('table');
  table.appendChild(create_table_th());
  if (data) {
    for (var i = 0; i < data.length; i++) {
      table.appendChild(create_table_td(data[i]));
    }
  } 
  else {
    var p = document.createElement('p');
    p.textContent = '没有找到...';
    table.appendChild(p);
  }
  var wrapper = document.createElement('div');
  wrapper.id = 'magnet-tab';
  wrapper.appendChild(table);
  return wrapper;
};
//------------
var gethash = function (vid, callback) {
  var search_url = 'http://www.btspread.com/search/';
  xhr(search_url + vid, function (html) {
    var doc = document.implementation.createHTMLDocument('');
    doc.documentElement.innerHTML = html;
    var data = [
    ];
    if (search_url == 'http://www.btspread.com/search/') { //搜索引擎切换?
      var t = doc.getElementsByClassName('data-list') [0];
      if (t) {
        var elems = t.getElementsByTagName('a');
        for (var i = 0; i < elems.length; i++) {
          data.push({
            'title': elems[i].title,
            'hash': elems[i].href.replace(/.*hash\//, ''),
            'size': elems[i].nextElementSibling.textContent
          });
        }
        callback(create_wrapper(data));
      } 
      else {
        callback(create_wrapper(false));
      }
      add_style();
      reg_event();
    }
  });
};
var handle_event = function (event) {
  if (event.target.className == 'magnet-copy') {
    event.target.innerHTML = '成功';
    GM_setClipboard(event.target.href);
    setTimeout(function () {
      event.target.innerHTML = '复制';
    }, 1000);
    event.preventDefault(); //阻止跳转
  } 
  else if (event.target.className == 'magnet-download') {
    GM_setValue('magnet', event.target.parentElement.getAttribute('data'));
  }
};
var reg_event = function () {
  var elem_copy = $('.magnet-copy');
  var elem_dl = $('.magnet-download');
  for (var i = 0; i < elem_copy.length; i++) {
    elem_copy[i].addEventListener('click', handle_event, false);
  }
  for (var j = 0; j < elem_dl.length; j++) {
    elem_dl[j].addEventListener('click', handle_event, false);
  }
  /*$('#switch_engine').addEventListener('click', function () {
    //TO DO ....
    }, false);*/

};
var main = {
  //av信息查询 类
  avsow: {
    regexp: /javdog.*movie.*/,
    vid: function () {
      return $('.header') [0].nextElementSibling.innerHTML;
    },
    proc: function (wrapper) {
      var title = document.createElement('h4');
      title.innerHTML = '磁链接';
      $after($('#movie-share'), wrapper);
      $after($('#movie-share'), title);
    }
  },
  avmask: {
    regexp: /avmask.*movie/,
    vid: function () {
      return $('.header') [0].nextElementSibling.innerHTML;
    },
    proc: function (wrapper) {
      var title = document.createElement('h4');
      title.innerHTML = '磁链接';
      $after($('#movie-share'), wrapper);
      $after($('#movie-share'), title);
    }
  },
  javlibrary: {
    regexp: /javlibrary.*\?v=.*/,
    vid: function () {
      return $('#video_id').getElementsByClassName('text') [0].innerHTML;
    },
    proc: function (wrapper) {
      $after($('#video_favorite_edit'), wrapper);
    }
  },
  libredmm: {
    regexp: /libredmm/,
    vid: function () {
      return location.href.match(/products\/(.*)/)[1];
    },
    proc: function (wrapper) {
      $after($('.container')[0],wrapper)
    }
  },
  dmm: {
    regexp: /dmm\.co\.jp/,
    vid: function () {
      var result = location.href.replace(/.*cid=/, '').replace(/\/\??.*/, '').match(/[^h_0-9].*/);
      if (result[0]) {
        return result[0].replace('00', '');
      } 
      else {
        return '';
      }
    },
    proc: function (wrapper) {
      $after($('.lh4') [0], wrapper)
    }
  },
  minnano: {
    regexp: /minnano-av/,
    vid: function () {
      var elems = $('.t11');
      var r = '';
      for (var i = 0; i < elems.length; i++) {
        if (elems[i].textContent == '品番') {
          r = elems[i].nextElementSibling.textContent;
          break;
        }
      }
      return r;
    },
    proc: function (wrapper) {
      var tmp = (function () {
        var a = $('table');
        for (var i = 0; i < a.length; i++) {
          if (a[i].bgColor == '#EEEEEE') {
            return a[i];
          }
        }
      }) ();
      $after(tmp, wrapper);
    }
  },
  oisinbosoft: {
    regexp: /oisinbosoft/,
    vid: function () {
      var r = location.pathname.replace(/.*\/+/, '').replace('.html', '');
      if (r.indexOf('-') == r.lastIndexOf('-')) {
        return r;
      } 
      else {
        return r.replace(/\w*-?/, '');
      }
    },
    proc: function (wrapper) {
      add_style('#magnet-tab table{clear:both;}');
      $after($('#detail_info'), wrapper);
    }
  },
  javbus: {
    regexp: /javbus/,
    vid: function () {
      var result = $('.movie-code');
      if (result) {
        return result[0].textContent;
      } 
      else {
        return '';
      }
    },
    proc: function (wrapper) {
      $after($('.movie') [0].parentElement, wrapper);
    }
  },
  avdb: {
    regexp: /avdb\.la/,
    vid: function () {
      return $('.info') [0].firstElementChild.innerHTML.replace(/<.*>/, '').trim();
    },
    proc: function (wrapper) {
      wrapper.className = 'movie';
      var tmp = $('#downs');
      var title = document.createElement('h4');
      title.innerHTML = '磁链接';
      $after($('#downs').previousElementSibling, title);
      $after($('#downs').previousElementSibling, wrapper);
    }
  },
  jav141: {
    regexp: /141jav/,
    vid: function () {
      return location.href.match(/view\/(.*)\//) [1];
    },
    proc: function (wrapper) {
      $after($('.dlbtn') [0].previousElementSibling, wrapper);
    },
  },
  av4you: {
    regexp: /av4you/,
    vid: function () {
      return $('.star-detail-name') [0].textContent.trim();
    },
    proc: function (wrapper) {
      $after($('.star-detail') [0], wrapper);
    }
  },
  //网盘下载 类
  baidu: {
    regexp: /pan\.baidu\.com/,
    proc: function (magnet) {
      $('.icon-btn-download').click();
      setTimeout(function () {
        $('#_disk_id_24').click();
        setTimeout(function () {
          $('#_disk_id_13').click();
          $('#share-offline-link').val(magnet);
        }, 500);
      }, 500);
    }
  },
  115: {
    regexp: /115\.com/,
    proc: function (link) {
      var readyStareChange = setInterval(function () {
        if (document.readyState == 'complete') {
          clearInterval(readyStareChange);
          setTimeout(function () {
            Core['OFFL5Plug'].OpenLink();
            setTimeout(function () {
              $('#js_offline_new_add').val(link);
            }, 0);
          }, 1000);
        }
      }, 200);
    }
  },
  letv: {
    regexp: /cloud\.letv\.com/,
    proc: function (link) {
      setTimeout(function () {
        $('#offline-btn').click();
        setTimeout(function () {
          $('#offline_clear_complete').prev().click();
          setTimeout(function () {
            $('#offline-add-link').val(link);
          }, 500);
        }, 1000);
      }, 2000);
    }
  },
  furk: {
    regexp: /www\.furk\.net/,
    proc: function (link) {
      setTimeout(function () {
        $('#url').val(link.replace('magnet:?xt=urn:btih:', ''));
      }, 1500);
    }
  },
  360: {
    regexp: /yunpan\.360\.cn\/my/,
    proc: function (link) {
      yunpan.cmdCenter.showOfflineDia();
      setTimeout(function () {
        $('.offdl-btn-create').click();
        setTimeout(function () {
          $('#offdlUrl').val(link)
        }, 500)
      }, 1000);
    }
  },
  //磁链接搜索 类
  btcherry: {
    regexp: /btcherry/,
    is_search: true,
    proc: function (div) {
      $eafter('.r div a', div, function (elem) {
        //elem 等于 document.querySelectorAll(.r div a)的成员
        return elem.href;
      });
    }
  },
  btdigg: {
    regexp: /btdigg/,
    is_search: true,
    proc: function (div) {
      $eafter('.snippet', div, function (elem) {
        return elem.parentElement.getElementsByClassName('ttth') [0].firstElementChild.href;
      });
    }
  },
  shousibaocai: {
    regexp: /shousibaocai/,
    is_search: true,
    proc: function (div) {
      $eafter('.tail', div, function (elem) {
        return elem.getElementsByTagName('a') [0].href;
      });
    }
  },
  btspread: {
    regexp: /btspread/,
    is_search: true,
    proc: function (div) {
      $eafter('.data-list .date', div, function (elem) {
        return 'magnet:?xt=urn:btih:' + elem.parentElement.getElementsByTagName('a') [0].href.match(/hash\/(.*)/) [1];
      });
    }
  }
};
for (var key in main) {
  if (location.href.match(main[key].regexp)) {
    if (main[key].vid) {
      gethash(main[key].vid(), main[key].proc);
    } 
    else if (apply_search_site && main[key].is_search) {
      init_search_offline(main[key].proc);
    } 
    else {
      var magnet = exist_magnet();
      if (magnet) {
        insert_js(magnet, main[key].proc);
      }
    }
    break;
  }
}