店小秘通用函数

通用函数

目前为 2024-07-03 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/499487/1404439/%E5%BA%97%E5%B0%8F%E7%A7%98%E9%80%9A%E7%94%A8%E5%87%BD%E6%95%B0.js

// ==UserScript==
// @name        店小秘通用函数
// @namespace   Violentmonkey Scripts
// @license MIT
// @icon        https://www.dianxiaomi.com/favicon.ico
// @grant GM_xmlhttpRequest
// @version     0.0.2
// @author      KuromiNote
// ==UserScript==
const tool = {
    getTotalPage:async function(url,data=undefined,op='GET'){
      let book = [];
      if(!Object.keys(data).includes("pageNo")){
        return;
      }
      if(op.toUpperCase() === 'GET' && data){
        url = this.setUrlParam(url,data)
      }
      var pageNo = +data['pageNo'];
      var firstPage = await new Promise((resolve) => {
        GM_xmlhttpRequest({
          method: op,
          url: url,
          data:data,
          headers: {
              "Content-Type": "application/x-www-form-urlencoded"
          },
          onload: function (response) {
            html = $(response.responseText);
            resolve(html);
          }
        });
      });
      book.push(firstPage);
      var tasks = [];
      var totalPage = firstPage.find("#totalPage");
      if(totalPage.length === 0){
        totalPage = firstPage.closest("#totalPage");
      }
      if(totalPage.length === 0){
        totalPage = firstPage.find("#totalPages");
      }
      if(totalPage.length === 0){
        totalPage = firstPage.closest("#totalPages");
      }
      totalPage = +totalPage.val();
      for(var i=pageNo+1;i<=totalPage;++i){
        data['pageNo'] = i;
        tasks.push(new Promise((resolve) => {
          GM_xmlhttpRequest({
            method: op,
            url: url,
            data:data,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            onload: function (response) {
              html = $(response.responseText);
              resolve(html);
            }
          });
        }));
      }
      book = book.concat(await Promise.all(tasks));
      return book;
    },
    setUrlParam: function(url, param){
      return url+"?"+$.map(Object.keys(param),function(val){return `${val}=${param[val]}`}).join("&");
    },
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址