CL115离线助手

115离线助手 for 1024

// ==UserScript==
// @name         CL115离线助手
// @namespace    1024.t66y
// @version      1.3.1
// @description  115离线助手 for 1024
// @author       岱宗@1024
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @match       http://115.com/*
// @match       http://*.115.com/*
// @match       https://115.com/*
// @match       https://*.115.com/*
// @match       http://www.t66y.com/htm_data/2/*
// @match       http://t66y.com/htm_data/2/*
// @grant       GM_xmlhttpRequest
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_openInTab
// @run-at      document-end
// ==/UserScript==
var X_userID = '0'; //115用户ID
X_userID = GM_getValue('X_userID', '0'); //115用户ID缓存
//获取115ID
if (X_userID == '0') {
  if (location.host.indexOf('115.com') >= 0) {
    if (typeof (window.wrappedJSObject.user_id) != 'undefined') {
      X_userID = window.wrappedJSObject.user_id;
      GM_setValue('X_userID', X_userID);
      alert('115登陆成功!');
      return;
    }
  } else {
    alert('请先登录115账户!');
    GM_setValue('X_userID', '0');
    GM_openInTab('http://115.com');
  }
}
if (location.host.indexOf('115.com') >= 0) {
  //115到此结束
  return;
}
function LXTo115(url) { //离线至115
  var fullUrl = 'http://115.com/?ct=offline&ac=space&_='; //115Key地址
  fullUrl += new Date().getTime();
  document.getElementById('lxBtn').innerHTML = '离线下载中';
  GM_xmlhttpRequest({
    method: 'GET',
    url: fullUrl,
    onload: function (responseDetails)
    {
      if (responseDetails.responseText.indexOf('html') >= 0) {
        alert('请先登录115账户!然后刷新页面即可!');
        GM_setValue('X_userID', '0');
        GM_openInTab('http://115.com');
        return;
      }
      var sign115 = JSON.parse(responseDetails.responseText).sign;
      var time115 = JSON.parse(responseDetails.responseText).time;
      downTo115(url, X_userID, sign115, time115);
    }
  });
}
function downTo115(url, X_userID, sign115, time115) { //POST离线报文
  var lxURL = 'http://115.com/web/lixian/?ct=lixian&ac=add_task_url&' + 'sign=' + sign115 + '&time=' + time115 + '&uid=' + X_userID + '&url=' + encodeURIComponent(url);
  GM_xmlhttpRequest({
    method: 'GET',
    url: lxURL,
    onload: function (responseDetails) {
      var lxRs = JSON.parse(responseDetails.responseText); //离线结果
      if (lxRs.state) {
        var rst = document.getElementById('lxBtnDiv');
        rst.removeChild(document.getElementById('lxBtn'));
        rst.innerHTML = '离线下载成功!1秒后自动关闭页面!';
        window.setTimeout(function () {
          window.close(); //默认离线成功后1秒自动关闭页面,如不需要请注释掉此行!
          //alert('应该已经退出!');
        }, 300);
      } else {
        //        alert('离线下载失败!');
        var rst = document.getElementById('lxBtn');
        rst.innerHTML = '离线下载失败!错误信息:《《' + lxRs.error_msg + '》》。请刷新后点击重试!请去115页面进行验证码验证!必要时可清空历史纪录或重启浏览器!';
        return;
      }
    }
  });
  return;
} //1024
//加入离线提示条
document.head.appendChild(document.createElement('style')).textContent = '#lxBtnDiv {font-size: 22px; position: fixed;top:0px;background-color: #f9f9ec !important;padding: 5px;width: 100%;}';
var lxBtnDiv = document.createElement('div');
lxBtnDiv.id = 'lxBtnDiv';
var hashLinks = $('a:contains(\'hash=\')'); //提取磁力链接HASH值
if (!hashLinks.length) {
  return; //若获取磁力失败则终止脚本
}
var magnetLink = 'magnet:?xt=urn:btih:' + hashLinks.text().split('=') [1].slice(3); //拼接磁力链接
var lxBtn = document.createElement('a');
lxBtn.id = 'lxBtn';
lxBtn.innerHTML = '点击离线资源:' + magnetLink;
//lxBtn.href = '#';
lxBtn.addEventListener('click', function () {
  LXTo115(magnetLink);
}, true); //加入离线触发事件
lxBtnDiv.appendChild(lxBtn);
document.body.appendChild(lxBtnDiv); //插入DOM树