吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 269|回复: 5
收起左侧

[经验求助] 我这个脚本怎么改写后速度提升5到10倍,请大神赐教!

[复制链接]
Lizhengwei123 发表于 2024-10-21 15:17
40吾爱币


如下:
var ginfo=document.getElementsByClassName("teaching-class-header card")[0],gpro=0,st=0;
function GetSemesters(){//获取学期列表
    $.ajax({
        method:"GET",
        url:"/api/my-semesters",
        success:function(j){
            GetCourses(j.semesters[0].id);
        }
    });
}
function GetCourses(semester_id){//获取课程列表
    $.ajax({
        method:"GET",
        url:"/api/my-courses?conditions=%7B%22semester_id%22:%5B%22"+semester_id+"%22%5D,\"status\":%5B\"ongoing\"%5D,\"keyword\":\"\"%7D&fields=id,name,course_code,department(id,name),grade(id,name),klass(id,name),course_type,cover,small_cover,start_date,end_date,is_started,is_closed,academic_year_id,semester_id,credit,compulsory,second_name,display_name,created_user(id,name),org(is_enterprise_or_organization),org_id,public_scope,course_attributes(teaching_class_name,copy_status,tip,data),audit_status,audit_remark,can_withdraw_course,imported_from,allow_clone,is_instructor,is_team_teaching,academic_year(id,name),semester(id,name),instructors(id,name,email,avatar_small_url),is_master,is_child,has_synchronized,master_course(name)&page=1&page_size=50",
        success:function(j){
            for(let i=0;i<j.courses.length;i++){
                st += 500 + Math.floor(Math.random() * 1000);
                setTimeout(()=>{GetCompleteness(j.courses[i].id)},st);
            }
        }
    });
}
function GetCompleteness(id){//获取已完成列表
    $.ajax({
        method:"GET",
        url:"/api/course/"+id+"/my-completeness",
        success:function(j){
            GetModules(id,j.completed_result.completed.learning_activity);
        }
    });
}
function GetModules(id,ywc){//获取模块列表
    $.ajax({
        method:"GET",
        url:"/api/courses/"+id+"/modules",
        success:function(j){
            for(let i=0;i<j.modules.length;i++){
                GetActivities(id,j.modules[i].id,ywc);
            }
        }
    });
}
function GetActivities(kid,id,ywc){//获取模块列表
    $.ajax({
        method:"GET",
        url:"/api/course/"+kid+"/all-activities?module_ids=["+id+"]&activity_types=learning_activities,exams,classrooms,live_records,rollcalls&no-loading-animation=true",
        success:function(j){
            for(let i=0;i<j.learning_activities.length;i++){
                if(ywc.join(",").indexOf(j.learning_activities[i].id)==-1){
                    st += 3000 + Math.floor(Math.random() * 2000);
                    gpro++;
                    setTimeout(()=>{PostActivities(j.learning_activities[i].uploads,j.learning_activities[i].id)},st);
                }
            }
        }
    });
}
function PostActivities(uploads,id){//获取模块列表
    let str="{}";
    if(uploads.length>0){
        if(uploads[0].videos.length>0){
            str="{\"start\":0,\"end\":"+uploads[0].videos[0].duration+"}"
        }
    }
    $.ajax({
        method:"POST",
        url:"/api/course/activities-read/"+id,
        data:str,
        contentType:"application/json",
        dataType:"json",
        success:function(j){
            gpro--;
            ginfo.innerHTML="<span style=\"color:blue;\"> 学习剩余数:"+gpro+"</span>";
            console.log(str+"="+j.completeness);
        }
    });
}
(function() {
    'use strict';
    if(location.href.indexOf("/user/courses")>-1){
        ginfo.innerHTML="<span style=\"color:red;\">学习初始化中……</span>";
        GetSemesters();
    }
})();

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

rimelight 发表于 2024-10-22 08:36
前端再怎么改也没啥影响,毕竟这里不占主要时间,主要耗时是接口;
可以减少一些回调里的timeout时间,如果允许的话,建议修改或优化接口逻辑,比如通过传一个列表来替代大量单个请求,这样只需要请求一次,且可以去掉timeout
Broadm 发表于 2024-10-22 08:59
正常情况下,这种批量查询的都需要接口支持,一次性传入多个参数
如果你不能修改接口, 接口也没有批量查询功能, 就只能一个个查询了, 但是可以通过多线程并发查询提高效率
li19980414 发表于 2024-10-22 09:00
[JavaScript] 纯文本查看 复制代码
var ginfo = document.getElementsByClassName("teaching-class-header card")[0],
  gpro = 0;

async function GetSemesters() {
  // 获取学期列表
  const response = await $.ajax({
    method: "GET",
    url: "/api/my-semesters",
  });
  await GetCourses(response.semesters[0].id);
}

async function GetCourses(semester_id) {
  // 获取课程列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/my-courses?conditions={"semester_id":["${semester_id}"],"status":["ongoing"],"keyword":""}&fields=id,name,...&page=1&page_size=50`,
  });

  for (const course of response.courses) {
    await GetCompleteness(course.id);
  }
}

async function GetCompleteness(id) {
  // 获取已完成列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/course/${id}/my-completeness`,
  });
  await GetModules(id, response.completed_result.completed.learning_activity);
}

async function GetModules(id, ywc) {
  // 获取模块列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/courses/${id}/modules`,
  });

  for (const module of response.modules) {
    await GetActivities(id, module.id, ywc);
  }
}

async function GetActivities(kid, id, ywc) {
  // 获取模块活动
  const response = await $.ajax({
    method: "GET",
    url: `/api/course/${kid}/all-activities?module_ids=[${id}]&activity_types=learning_activities,exams,classrooms,live_records,rollcalls&no-loading-animation=true`,
  });

  for (const activity of response.learning_activities) {
    if (!ywc.includes(activity.id)) {
      gpro++;
      await PostActivities(activity.uploads, activity.id);
    }
  }
}

async function PostActivities(uploads, id) {
  // 获取模块活动
  let str = "{}";
  if (uploads.length > 0 && uploads[0].videos.length > 0) {
    str = `{"start":0,"end":${uploads[0].videos[0].duration}}`;
  }
  
  const response = await $.ajax({
    method: "POST",
    url: `/api/course/activities-read/${id}`,
    data: str,
    contentType: "application/json",
    dataType: "json",
  });

  gpro--;
  ginfo.innerHTML = `<span style="color:blue;">学习剩余数:${gpro}</span>`;
  console.log(str + "=" + response.completeness);
}

(function () {
  "use strict";
  ginfo.innerHTML = '<span style="color:red;">学习初始化中……</span>';
  GetSemesters();
})();
li19980414 发表于 2024-10-22 09:03
并发

[JavaScript] 纯文本查看 复制代码
var ginfo = document.getElementsByClassName("teaching-class-header card")[0],
  gpro = 0;

async function GetSemesters() {
  // 获取学期列表
  const response = await $.ajax({
    method: "GET",
    url: "/api/my-semesters",
  });
  await GetCourses(response.semesters[0].id);
}

async function GetCourses(semester_id) {
  // 获取课程列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/my-courses?conditions={"semester_id":["${semester_id}"],"status":["ongoing"],"keyword":""}&fields=id,name,...&page=1&page_size=50`,
  });

  const completenessPromises = response.courses.map(course => GetCompleteness(course.id));
  await Promise.all(completenessPromises);
}

async function GetCompleteness(id) {
  // 获取已完成列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/course/${id}/my-completeness`,
  });
  await GetModules(id, response.completed_result.completed.learning_activity);
}

async function GetModules(id, ywc) {
  // 获取模块列表
  const response = await $.ajax({
    method: "GET",
    url: `/api/courses/${id}/modules`,
  });

  const activityPromises = response.modules.map(module => GetActivities(id, module.id, ywc));
  await Promise.all(activityPromises);
}

async function GetActivities(kid, id, ywc) {
  // 获取模块活动
  const response = await $.ajax({
    method: "GET",
    url: `/api/course/${kid}/all-activities?module_ids=[${id}]&activity_types=learning_activities,exams,classrooms,live_records,rollcalls&no-loading-animation=true`,
  });

  const activityPromises = response.learning_activities
    .filter(activity => !ywc.includes(activity.id))
    .map(activity => {
      gpro++;
      return PostActivities(activity.uploads, activity.id);
    });

  await Promise.all(activityPromises);
}

async function PostActivities(uploads, id) {
  // 获取模块活动
  let str = "{}";
  if (uploads.length > 0 && uploads[0].videos.length > 0) {
    str = `{"start":0,"end":${uploads[0].videos[0].duration}}`;
  }
  
  const response = await $.ajax({
    method: "POST",
    url: `/api/course/activities-read/${id}`,
    data: str,
    contentType: "application/json",
    dataType: "json",
  });

  gpro--;
  ginfo.innerHTML = `<span style="color:blue;">学习剩余数:${gpro}</span>`;
  console.log(str + "=" + response.completeness);
}

(function () {
  "use strict";
  ginfo.innerHTML = '<span style="color:red;">学习初始化中……</span>';
  GetSemesters();
})();
 楼主| Lizhengwei123 发表于 2024-10-22 20:51
li19980414 发表于 2024-10-22 09:03
并发

[mw_shl_code=javascript,true]var ginfo = document.getElementsByClassName("teaching-class-hea ...

没有效果
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-15 01:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表