Greasy Fork

Greasy Fork is available in English.

pkuElective自动翻页

no description

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @name         pkuElective自动翻页
// @version      0.1
// @description  no description
// @author       test12345
// @match        https://elective.pku.edu.cn/elective2008/edu/pku/stu/elective/controller/supplement/*
// @grant        none
// @license      MIT
// @namespace    test12345
// ==/UserScript==

'use strict';

const headersOfCurrentPage = Object.fromEntries(new Map(document.location.search.slice(1).split('&').map(x => x.split('='))));
const studentId = headersOfCurrentPage.xh;

async function main(){
    const electiveTable = $("table.datagrid")[0];
    const tableHeader = electiveTable.children[0].children[0];
    function parseTable(table){
        const linesInTable = Array.prototype.slice.call(table.children[0].children);
        const pageInformation = linesInTable.at(-1).children[0].childNodes[0].data.split(' '); // ['Page', '*', 'of', '*']
        return {
            pageId: parseInt(pageInformation[1]),
            pageNumber: parseInt(pageInformation[3]),
            content: linesInTable.slice(1,-2),
        }
    }
    const pageNumber = parseTable(electiveTable).pageNumber;
    let allPageTableBody = Array(pageNumber), promiseList = [];
    for(let i=0;i<pageNumber;++i){
        promiseList.push(
            $.get(`https://elective.pku.edu.cn/elective2008/edu/pku/stu/elective/controller/supplement/supplement.jsp?netui_pagesize=electableListGrid%3B20&xh=${studentId}&netui_row=electableListGrid%3B` + (20*i))
            .done((data) => {
                const parsedTable = parseTable($('table.datagrid',data)[0]);
                allPageTableBody[parsedTable.pageId - 1] = parsedTable.content;
            })
        );
    }
    await Promise.all(promiseList);
    $(electiveTable).html('<table><tbody>' + '<tr class="datagrid-header">' + $(tableHeader).html() + '</tr>' +
                          allPageTableBody.flat().map((x, index) => `<tr class="${(index & 1)? 'datagrid-odd' : 'datagrid-even'}">` + $(x).html() + '</tr>').join('') +
                          '</table></tbody>');
};

main();