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

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

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 or Violentmonkey 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.avsow.com/*
// @include     http://www.avmask.com/*
// @include     http://www.javlibrary.com/*
// @include     http://www.dmm.co.jp/digital/videoa/*
// @include     http://www.libredmm.com/products/*
// @include     http://www.minnano-av.com/av*
// @include     http://www.oisinbosoft.com/dera/*
// @include     http://www.javbus.com/*
// @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
// @version     1.05
// @grant       GM_xmlhttpRequest
// @grant       GM_setClipboard
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_addStyle
// ==/UserScript==
var av_id = '';
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 (callback) {
  var magnet = GM_getValue('magnet');
  if (magnet) {
    GM_setValue('magnet', '');
    callback(magnet);
  }
};
var insert_js = function (magnet, func) {
  var js = document.createElement('script');
  js.innerHTML = '(' + func.toString() + ')(\'' + magnet + '\');';
  document.body.appendChild(js);
};
var hostmatch = function (url) {
  if (location.host == url) {
    return true;
  }
  return false;
};
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;}.magnet-th,.magnet-td{height:30px; border:1px solid #cad9ea;padding:0 1em 0;}.magnet-copy{color:#08c;}.magnet-download{color:#0003FF;}');
  }
};
var create_wrapper = function (data) {
  var html = '';
  if (data) {
    html = '<tr><th class=\'mgnet-th\'>标题</th><th class=\'magnet-th\'>大小</th><th class=\'magnet-th\'>操作</th><th class=\'magnet-th\'>离线下载</th></tr>';
    var str = '<td class=\'magnet-td\'><a class=\'magnet-copy\' href=\'javascript:void(0);\'>复制</a></td><td class=\'magnet-td\'><a class=\'magnet-download\' target=\'_blank\' href=\'http://pan.baidu.com/disk/home\'>百度云</a> | <a class=\'magnet-download\' target=\'_blank\' href=\'http://115.com/?tab=offline&mode=wangpan\'>115离线</a> | <a class=\'magnet-download\' target=\'_blank\' href=\'http://cloud.letv.com/webdisk/home/index\'>乐视云</a> | <a class=\'magnet-download\' target=\'_blank\' href=\'https://www.furk.net/users/files/add\'>Furk</a></td>';
    for (var i = 0; i < data.length; i++) {
      html += '<tr hash=\'' + data[i].hash + '\'><td class=\'magnet-td\'>' + data[i].title + '</td>';
      html += '<td class=\'magnet-td\'>' + data[i].size + '</td>';
      html += str;
      html += '</tr>';
    }
  } 
  else {
    html = '<tr><th class=\'magnet-th\'>没有搜索结果</th></tr>';
  }
  var table = document.createElement('table');
  table.innerHTML = html;
  var wrapper = document.createElement('div');
  wrapper.id = 'magnet-tab';
  wrapper.appendChild(table);
  return wrapper;
};
var gethash = function (av_id, callback) {
  var search_url = 'http://www.btspread.com/search/';
  xhr(search_url + av_id, function (html) {
    var doc = document.implementation.createHTMLDocument('');
    doc.documentElement.innerHTML = html;
    if (search_url == 'http://www.btspread.com/search/') { //搜索引擎切换?
      var t = doc.getElementsByTagName('tbody') [0];
      if (!t) {
        callback(false);
      } 
      else {
        var data = [
        ];
        var elems = t.getElementsByClassName('files-size');
        for (var i = 0; i < elems.length; i++) {
          data.push({
            'title': elems[i].previousElementSibling.firstChild.title,
            'hash': elems[i].previousElementSibling.firstChild.href.replace(/.*hash\//, ''),
            'size': elems[i].textContent
          });
        }
        callback(data);
      }
    }
  });
};
var handle_event = function (event) {
  if (event.target.className == 'magnet-copy') {
    GM_setClipboard('magnet:?xt=urn:btih:' + event.target.parentElement.parentElement.getAttribute('hash'));
  } 
  else if (event.target.className == 'magnet-download') {
    GM_setValue('magnet', 'magnet:?xt=urn:btih:' + event.target.parentElement.parentElement.getAttribute('hash'));
  }
};
var reg_event = function () {
  var elem_copy = document.getElementsByClassName('magnet-copy');
  var elem_down = document.getElementsByClassName('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_down.length; j++) {
    elem_down[j].addEventListener('click', handle_event, false);
  }
};
if (hostmatch('www.avsow.com') || hostmatch('www.avmask.com')) {
  if (location.href.match('movie')) {
    av_id = document.getElementsByClassName('header') [0].nextElementSibling.innerHTML;
    gethash(av_id, function (data) {
      add_style();
      var wrapper = create_wrapper(data);
      var title = document.createElement('h4');
      title.innerHTML = '磁链接';
      var tmp = document.getElementById('movie-share');
      tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
      tmp.parentElement.insertBefore(title, tmp.nextElementSibling);
      reg_event();
    });
  }
} 
else if (hostmatch('www.javlibrary.com')) {
  if (location.search.match('\\?v=')) {
    av_id = document.getElementById('video_id').getElementsByClassName('text') [0].innerHTML;
    gethash(av_id, function (data) {
      add_style();
      var wrapper = create_wrapper(data);
      var tmp = document.getElementById('video_favorite_edit');
      tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
      reg_event();
    });
  }
} 
else if (hostmatch('www.libredmm.com')) {
  av_id = location.pathname.substr(10);
  gethash(av_id, function (data) {
    add_style();
    var wrapper = create_wrapper(data);
    var tmp = document.getElementsByClassName('container') [0];
    tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
    reg_event();
  });
} 
else if (hostmatch('www.dmm.co.jp')) {
  var result = location.href.replace(/.*cid=/, '').replace(/\/\??.*/, '').match(/[^h_0-9].*/);
  //console.log(result);
  if (result[0]) {
    av_id = result[0].replace('00', '');
    gethash(av_id, function (data) {
      add_style();
      var wrapper = create_wrapper(data);
      var tmp = document.getElementsByClassName('lh4') [0];
      tmp.parentElement.insertBefore(wrapper, tmp.previousElementSibling);
      reg_event();
    });
  }
} 
else if (hostmatch('www.minnano-av.com')) {
  av_id = (function () {
    var elems = document.getElementsByClassName('t11');
    var r = '';
    for (var i = 0; i < elems.length; i++) {
      if (elems[i].textContent == '品番') {
        r = elems[i].nextElementSibling.textContent;
        break;
      }
    }
    return r;
  }) ();
  gethash(av_id, function (data) {
    console.log(av_id);
    add_style();
    var wrapper = create_wrapper(data);
    var tmp = (function () {
      var a = document.getElementsByTagName('table');
      for (var i = 0; i < a.length; i++) {
        if (a[i].bgColor == '#EEEEEE') {
          return a[i];
        }
      }
    }) ();
    tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
    reg_event();
  });
} 
else if (hostmatch('www.oisinbosoft.com')) {
  av_id = (function () {
    var r = location.pathname.replace(/.*\/+/, '').replace('.html', '');
    if (r.indexOf('-') == r.lastIndexOf('-')) {
      return r;
    } 
    else {
      return r.replace(/\w*-?/, '');
    }
  }) ();
  gethash(av_id, function (data) {
    add_style('#magnet-tab table{margin:10px auto;border:1px solid #cad9ea;color:#666;font-size:12px;clear:both;}.magnet-th,.magnet-td{height:30px; border:1px solid #cad9ea;padding:0 1em 0;}.magnet-copy{color:#08c;}.magnet-download{color:#0003FF;}');
    var wrapper = create_wrapper(data);
    var tmp = document.getElementById('detail_info');
    tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
    reg_event();
  });
} 
else if (hostmatch('www.javbus.com')) {
  var result = document.getElementsByClassName('movie-code');
  if (result) {
    av_id = result[0].textContent;
    gethash(av_id, function (data) {
      add_style();
      var wrapper = create_wrapper(data);
      var tmp = document.getElementsByClassName('movie') [0].parentElement;
      tmp.parentElement.insertBefore(wrapper, tmp.nextElementSibling);
      reg_event();
    });
  }
}
/*else if(hostmatch("duga.jp")){
  av_id = location.href.replace(/.*ppv\//,"").replace(/\/\?.* /,"");
  gethash(av_id,function(data){
    add_style();
    var wrapper = create_wrapper(data);
    var tmp = document.getElementsByClassName("summarywrap")[0];
    tmp.parentElement.insertBefore(wrapper,tmp.nextElementSibling);
  });
}*/
 else if (hostmatch('pan.baidu.com')) {
  exist_magnet(function (magnet) {
    insert_js(magnet, function (magnet) {
      $('.icon-btn-download').click();
      setTimeout(function () {
        $('#_disk_id_24').click();
        setTimeout(function () {
          $('#_disk_id_12').click();
          $('#share-offline-link').val(magnet);
        }, 500);
      }, 500);
    });
  });
} 
else if (hostmatch('115.com')) {
  exist_magnet(function (magnet) {
    insert_js(magnet, function (magnet) {
      setTimeout(function () {
        Core['OFFL5Plug'].OpenLink();
        setTimeout(function () {
          $('#js_offline_new_add').val(magnet);
        }, 0);
      }, 1000);
    });
  });
} 
else if (hostmatch('cloud.letv.com')) {
  exist_magnet(function (magnet) {
    insert_js(magnet, function (magnet) {
      setTimeout(function () {
        $('#offline-btn').click();
        setTimeout(function () {
          $('#offline_clear_complete').prev().click();
          setTimeout(function () {
            $('#offline-add-link').val(magnet);
          }, 500);
        }, 1000);
      }, 2000);
    });
  });
} 
else if (hostmatch('www.furk.net')) {
  exist_magnet(function (magnet) {
    insert_js(magnet, function (magnet) {
      setTimeout(function () {
        $('#url').val(magnet.replace('magnet:?xt=urn:btih:', ''));
      }, 1500);
    });
  });
}