noahzark / Autofill access code for baidu netdisk

// ==UserScript==
// @name         Autofill access code for baidu netdisk
// @version      0.5
// @original     https://openuserjs.org/meta/qip/My_Script.meta.js
// @updateURL    https://openuserjs.org/meta/noahzark/Autofill_access_code_for_baidu_netdisk.meta.js
// @description  自动尝试将百度盘提取码加入URL,并在百度盘提取页自动填充并提交提取码
// @author       ._., Feliciano Long
// @match        *://pan.baidu.com/s*
// @match        *://ccba.me/*
// @match        *://ccba.cc/*
// @match        *://ccba6.info/*
// @license     MIT
// @run-at     document-end
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

function checkBox() {
  console.log("try to load save box");
  var box = document.getElementsByClassName("save-path-item");
  if (box.length > 0) {
    box[0].classList.add("check");
    console.log("save box checked");
  }
  else {
    setTimeout(checkBox, 1000);
  }
}

var sheet = document.createElement('style');
sheet.innerHTML = ".laytable-cell-1-1 {width: 75px;}";
document.body.appendChild(sheet);

(function () {
  'use strict';

  if (location.href.indexOf('://pan.baidu.com/s') > -1) {
    if (document.getElementsByClassName('QKKaIE LxgeIt').length > 0) {
      var accessCode = location.hash.replace('#', '');
      if (!accessCode) {
        accessCode = GM_getValue('accessCode');
        if (accessCode) {
          accessCode = accessCode.replace('文件1', '');
          accessCode = accessCode.replace('密码:', '');
          accessCode = accessCode.replace(' ', '');
          console.log('Loaded access code from storage: ' + accessCode);
        }
      }
      if (accessCode.length == 4) {
        document.getElementsByClassName('QKKaIE LxgeIt')[0].value = accessCode;
        // document.getElementById('submitBtn').click();
        var submitBtn = document.getElementsByClassName("g-button g-button-blue-large");
        if (submitBtn.length > 0) {
          submitBtn[0].click();
        }
      }
    }
    else {
      window.addEventListener('load', function () {
        console.log("try to click save button");
        var saveBtn = document.getElementsByClassName("g-button g-button-blue");
        if (saveBtn.length > 0) {
          saveBtn[0].click();
          checkBox();
        }
      }, false);
    }
  }
  else if (location.href.indexOf('://ccba.cc/') > -1) {
    $('div.sc-box').each(function (i, e) {
      e.innerHTML = e.innerHTML.replace(/[^">]http[s]?:\/\/pan.baidu.com\/s[\w\/]*/g, function (i) {
        return '<a href="' + i + '">' + i + '</a>';
      });
    });
    var reg = [],
      accessCodeRegex = /(?:a>.*?[\s::])([\w\d]{4})/g,
      output = location.href + '\n';
    $('a').each(function (i, e) {
      if (e.href.indexOf('pan.baidu.com/s/') > -1 && e.href.indexOf('#') == -1) {
        var accessCode = [],
          result;
        while ((result = accessCodeRegex.exec($(e).parents('div.sc-box').html())) !== null) {
          accessCode.push(result[1]);
        }
        while ((result = accessCodeRegex.exec($(e).parents('div.layui-field-box').html())) !== null) {
          accessCode.push(result[1]);
        }
        if (accessCode) {
          if (accessCode.length > 1) {
            if (reg.indexOf(accessCode[accessCode.length - 1]) == -1) {
              reg = accessCode;
            }
            e.href += '#' + reg.shift();
            output += e.href + '\n';
          }
          else {
            e.href += '#' + accessCode[0];
            output += e.href + '\n';
          }
        }
      }
    });
    console.log(output);
  }
  else if (location.href.indexOf('://ccba6.info') > -1) {
    if (location.href.indexOf('://ccba6.info/wp-content/plugins/erphpdown/download.php?postid=') > -1) {
      // Get access code and click pan link
      console.log(document.getElementsByTagName("a")[0].href);
      var code = document.getElementsByTagName("p")[0].textContent;
      GM_setValue('accessCode', code);
      console.log('code: ' + code);
      if (code) {
        console.log('Saved access code to storage');
        //window.location = document.getElementsByTagName("a")[0];
        document.getElementsByTagName("a")[0].click();
        close();
      }
    }
    else if (location.href.indexOf('/download') > -1) {
      /*if ($('.layui-table').length == 3) {
          var table = $('.layui-table')[2];
          for (var i = 0, row; (row = table.rows[i]); i++) {
              if (row.cells.length == 6) {
                  console.log(row.cells[4]);
                  console.log(row.cells[5]);
              }
          }
      }*/
      var regs = [],
        output_str = location.href + '\n';
      var aCodeRegex = /(?:i>.*?[\s:;提取码:])([\w\d]{4})/g;
      var baiduLinkRegex = /http[s]?:\/\/pan.baidu.com\/s[\w\/]*/g;
      var accessCodes = [],
        result;
      window.addEventListener('load', function () {
        $('div.layui-table-cell.laytable-cell-1-6').each(function (i, e) {
          console.log('hi');
          var output_link = '';
          while ((result = aCodeRegex.exec(e.innerHTML)) !== null) {
            console.log(result[1]);
            output_link = result[1];
          }
          if (output_link) {
            var col = $(e).parents('tr').html();
            while ((result = baiduLinkRegex.exec(col)) !== null) {
              console.log(result[0]);
              output_link = result[0] + '#' + output_link;
            }
            if (output_link.indexOf('#') > -1)
              e.innerHTML = e.innerHTML.replace('&nbsp;', function (i) {
                return '&nbsp;<a target="_blank" href="' + output_link + '"><i class="fa fa-cloud-download"></i></a>&nbsp;';
              });
          }
        });
      });
    }
    else if (location.href.indexOf('/page/') > -1) {
      // Generate list
      var pages = '';
      $(".zoom").each(function (index) {
        pages += this.title + '\n' + this.href + '\n';
      });
      console.log(pages);
    }
    else {
      return;
      // click download page
      var downloadPage = document.getElementsByClassName('iframe');
      if (downloadPage.length > -1) {
        console.log('opened page');
        window.location = downloadPage[0].href;
      }
    }
  }
})();