Beijing question bank plug-in: What's the problem if you can't log in?

background introduction

What is Beijing Question Bank

Beijing Question Bank

It is a teaching and research platform focusing on the research and development of teaching products for primary and secondary schools. It has high-quality resources such as test papers and materials, and is committed to providing one-stop services for teachers' lesson preparation and teaching research. --"Baidu Encyclopedia"

To put it simply, a lot of information collected is relatively easy to use

However, its use has certain restrictions. For example, the web page must scan the QR code to log in to use it, which is not very convenient.

And some documents are charged, and there is a limit of two documents per day...these

Therefore, I have designed a Javascript plug-in for Tampermonkey to solve the problem of WeChat login on the web page (as for what functions you can develop, it has nothing to do with me) (≖ᴗ≖)✧

Beijing question bank version update

On May 12, 2023, Beijing Question Bank released "Beijing Question Bank 3.0", which has been jointly updated on mobile phones and web pages in recent days.

Compared with 2.0, the new version has stricter restrictions on WeChat login, and even the preview cannot be opened.

For the original 2.0 version, I wrote a plugin,

// ==UserScript==
// @name         北京题库使用优化
// @namespace    none
// @version      1.3
// @description  下载来自“北京题库”的试题
// @author       Mornwind
// @match        *://www.jingshibang.com/*
// @icon         http://www.jingshibang.com//favicon.ico
// @grant        GM_xmlhttpRequest
// ==/UserScript==
 
(function() {
    'use strict';
    function downloadTxt(fileName, content) {
        let a = document.createElement('a');
        a.href = 'data:text/plain;charset=utf-8,' + content
        a.download = fileName
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
    function directDownload(url, filename) {
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.click();
        link.remove();
    }
    function downloadPdf(url,name) {
        let list = {
            url,
            name: name,
            type: 'pdf'
        }
        downloadFile(list)
    }
 
 
    function downloadFile(data) {
        fetchDownloadFile(data)
    }
 
    function fetchDownloadFile(data) {
        fetch(data.url, {
            method: "get",
            mode: "cors",
        })
            .then((response) => response.blob())
            .then((res) => {
            const downloadUrl = window.URL.createObjectURL(
                //new Blob() 对后端返回文件流类型处理
                new Blob([res], {
                    type: data.type == "pdf" ? "application/pdf" : data.type == "word" ?
                    "application/msword" : data.type == "xlsx" ? "application/vnd.ms-excel" : ""
                })
            );
            //word文档为msword,pdf文档为pdf
            const link = document.createElement("a");
            link.href = downloadUrl;
            link.setAttribute("download", data.name);
            document.body.appendChild(link);
            link.click();
            link.remove();
        }).catch((error) => {
            window.open(data.url);
        });
    };
    //获取Base64
    function pathToBase64(url) {
        return new Promise((resolve, reject) => {
            let image = new Image();
            image.onload = function() {
                let canvas = document.createElement('canvas');
                canvas.width = this.naturalWidth;
                canvas.height = this.naturalHeight;
                canvas.getContext('2d').drawImage(image, 0, 0);
                let result = canvas.toDataURL('image/png')
                resolve(result);
            };
            image.setAttribute("crossOrigin", 'Anonymous');
            image.src = url
            image.onerror = () => {
                reject(new Error('urlToBase64 error'));
            };
        })
    }
    function getPDF(url){
        var id=url.split("id=").slice(-1)[0]
        try{
            id=id.split("&").slice(0)[0]
        }
        catch (e) { }
        console.log(id)
        GM_xmlhttpRequest({
            method:"get",
            url:"http://www.jingshibang.com/api/product/detailpc/"+id,
            //headers:header,
            async onload({ response }) {
                if(JSON.parse(response).data.storeInfo.store_type=="专辑"){
                    var text="第三方文档:\n"+JSON.parse(response).data.storeInfo.store_name+"\n百度网盘链接:"+JSON.parse(response).data.storeInfo.baidu_url+"\n提取码:"+JSON.parse(response).data.storeInfo.baidu_pw
                    downloadTxt(JSON.parse(response).data.storeInfo.store_name,text)
                }
                else{
                    var pdf_name=JSON.parse(response).data.storeInfo.store_name
                    if(JSON.parse(response).data.storeInfo.word_answer!=""){
                        if(confirm("是否下载WORD?")){
                            window.location.href="http://www.jingshibang.com"+JSON.parse(response).data.storeInfo.word_answer
                        }
                    }
                    var pdf_url="https://jsb2022-1253627302.cos.ap-beijing.myqcloud.com"+JSON.parse(response).data.storeInfo.pdf_answer
                    downloadPdf(pdf_url,pdf_name)
                }
            }
        })
    }
    function downloadPageT(){
        for(var i=1; i<10**10;i++){
            try{
                getPDF(document.querySelector("#__layout > div > div.index.wrapper_1200_no > div.acea-row > div.rightdiv > div.list.tablelist > a:nth-child("+i+")").href)
            }
            catch (e) {break }
        }
    }
    function downloadPageE(){
        for(var i=1; i<10**10;i++){
            try{
                getPDF(document.querySelector("#__layout > div > div.index.wrapper_1200_no > div > div:nth-child(2) > div.list.tablelist > a:nth-child("+i+")").href)
            }
            catch (e) {break }
        }
    }
    document.onclick = function (e) {
        if (e.ctrlKey && e.shiftKey){
            if(window.location.href.indexOf("id")!=-1){
                getPDF(window.location.href);
            }
            if(window.location.href=="http://www.jingshibang.com/home/"){
                downloadPageT()
            }
            if(window.location.href=="http://www.jingshibang.com/home/paper"){
                downloadPageE()
            }
        }
    }
})();

However, with newer versions, this version is completely unusable.

programming

Check legacy functionality

In the current page, once I want to open a document for preview, the following situation will appear

 

It is required to scan the code to log in, so I try to open it after logging in

After logging in, you can open an article

 

Also, the way documents are identified by id has not changed. First remove part of the page optimization code to eliminate the impact

In this case, the original code can still be used

 

But this needs to keep the WeChat login, otherwise the document cannot be opened, which is not in line with the original design intention.

Now, think about how to skip the process of opening the document.

Code

After experimenting, you can use such an idea to do the action of opening the document

Click on the document name --> search for the document name --> get the id --> download the document

(in the keyboard click event) the code is as follows


name=e.target.innerText
console.log(name)
var url="http://www.jingshibang.com/api/products?page=1&limit=99&keyword="+name
GM_xmlhttpRequest({
    method:"get",
    url:url,
    //headers:header,
    async onload({ response }) {
        const data=JSON.parse(response).data
        var pro
        for(var i=0;i<=data.length;i++){
            pro=data.slice(i)[0]
            console.log(pro)
            if(pro.store_name==name){
                window.open("http://www.jingshibang.com"+pro.pdf_answer)
                download(pro.id)
            }
        }
    }
})

The final result is this: 

In this way, the code effect is basically realized.

Full code:

// ==UserScript==
// @name         北京题库使用优化
// @namespace    none
// @version      2.0
// @description  下载来自“北京题库”的试题
// @author       Mornwind
// @match        *://www.jingshibang.com/*
// @icon         http://www.jingshibang.com//favicon.ico
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    var name,text,tp,pdf,word,tt;
    'use strict';
    function downloadTxt(fileName, content) {
        let a = document.createElement('a');
        a.href = 'data:text/plain;charset=utf-8,' + content
        a.download = fileName
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
    function directDownload(url, filename) {
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.click();
        link.remove();
    }
    function downloadPdf(url,name) {
        let list = {
            url,
            name: name,
            type: 'pdf'
        }
        downloadFile(list)
    }


    function downloadFile(data) {
        fetchDownloadFile(data)
    }

    function fetchDownloadFile(data) {
        fetch(data.url, {
            method: "get",
            mode: "cors",
        })
            .then((response) => response.blob())
            .then((res) => {
            const downloadUrl = window.URL.createObjectURL(
                //new Blob() 对后端返回文件流类型处理
                new Blob([res], {
                    type: data.type == "pdf" ? "application/pdf" : data.type == "word" ?
                    "application/msword" : data.type == "xlsx" ? "application/vnd.ms-excel" : ""
                })
            );
            //word文档为msword,pdf文档为pdf
            const link = document.createElement("a");
            link.href = downloadUrl;
            link.setAttribute("download", data.name);
            document.body.appendChild(link);
            link.click();
            link.remove();
        }).catch((error) => {
            window.open(data.url);
        });
    };
    //获取Base64
    function pathToBase64(url) {
        return new Promise((resolve, reject) => {
            let image = new Image();
            image.onload = function() {
                let canvas = document.createElement('canvas');
                canvas.width = this.naturalWidth;
                canvas.height = this.naturalHeight;
                canvas.getContext('2d').drawImage(image, 0, 0);
                let result = canvas.toDataURL('image/png')
                resolve(result);
            };
            image.setAttribute("crossOrigin", 'Anonymous');
            image.src = url
            image.onerror = () => {
                reject(new Error('urlToBase64 error'));
            };
        })
    }
    function get(id){
        console.log(id)
        GM_xmlhttpRequest({
            method:"get",
            url:"http://www.jingshibang.com/api/product/detailpc/"+id,
            //headers:header,
            async onload({ response }) {
                name=JSON.parse(response).data.storeInfo.store_name
                if(JSON.parse(response).data.storeInfo.store_type=="专辑"){
                    tp="txt"
                    text="第三方文档:\n"+JSON.parse(response).data.storeInfo.store_name+"\n百度网盘链接:"+JSON.parse(response).data.storeInfo.baidu_url+"\n提取码:"+JSON.parse(response).data.storeInfo.baidu_pw
                    name=JSON.parse(response).data.storeInfo.store_name
                }
                else{
                    tp="normal"
                    word="http://www.jingshibang.com"+JSON.parse(response).data.storeInfo.word_answer
                    pdf="https://jsb2022-1253627302.cos.ap-beijing.myqcloud.com"+JSON.parse(response).data.storeInfo.pdf_answer
                }
                if(name.indexOf(":")!=-1){
                    tt=name.split(":").slice(-1)[0]
                }
                else{
                    tt=name
                }
            }
        })
    }
    function getPDF(url){
        var flag=confirm("是否下载word?")
        if(flag){
            window.location.href=word
        }
        downloadPdf(pdf,name)
    }
    function downloadPageT(){
        for(var i=1; i<10**10;i++){
            try{
                getPDF(document.querySelector("#__layout > div > div.index.wrapper_1200_no > div.acea-row > div.rightdiv > div.list.tablelist > a:nth-child("+i+")").href)
            }
            catch (e) {break }
        }
    }
    function downloadPageE(){
        for(var i=1; i<10**10;i++){
            try{
                getPDF(document.querySelector("#__layout > div > div.index.wrapper_1200_no > div > div:nth-child(2) > div.list.tablelist > a:nth-child("+i+")").href)
            }
            catch (e) {break }
        }
    }
    function titleChange(content) {
        if (document.querySelector("title") && document.querySelector("title").innerHTML != content) {
            document.querySelector("title").innerHTML = content
        }
    }
    function open(e){
        console.log(e)
        try{
            name=e.target.innerText
            console.log(name)
            var url="http://www.jingshibang.com/api/products?page=1&limit=99&keyword="+name
            GM_xmlhttpRequest({
                method:"get",
                url:url,
                //headers:header,
                async onload({ response }) {
                    const data=JSON.parse(response).data
                    var pro
                    for(var i=0;i<=data.length;i++){
                        pro=data.slice(i)[0]
                        console.log(pro)
                        if(pro.store_name==name){
                            window.open("http://www.jingshibang.com"+pro.pdf_answer)
                            download(pro.id)
                        }
                    }
                }
            })
        }
        catch(ep){
            console.log(ep)
        }
    }
    function download(id){
        console.log(id)
        GM_xmlhttpRequest({
            method:"get",
            url:"http://www.jingshibang.com/api/product/detailpc/"+id,
            //headers:header,
            async onload({ response }) {
                console.log(response)
                name=JSON.parse(response).data.storeInfo.store_name
                if(JSON.parse(response).data.storeInfo.store_type=="专辑"){
                    tp="txt"
                    text="第三方文档:\n"+JSON.parse(response).data.storeInfo.store_name+"\n百度网盘链接:"+JSON.parse(response).data.storeInfo.baidu_url+"\n提取码:"+JSON.parse(response).data.storeInfo.baidu_pw
                    name=JSON.parse(response).data.storeInfo.store_name
                }
                else{
                    tp="normal"
                    word="http://www.jingshibang.com"+JSON.parse(response).data.storeInfo.word_answer
                    pdf="https://jsb2022-1253627302.cos.ap-beijing.myqcloud.com"+JSON.parse(response).data.storeInfo.pdf_answer
                }
                if(name.indexOf(":")!=-1){
                    tt=name.split(":").slice(-1)[0]
                }
                else{
                    tt=name
                }

                console.log(tp)
                if(tp=="normal"){
                    window.location.href=word
                    downloadPdf(pdf,name)
                }
                else{
                    downloadTxt(name,text)
                }
            }
        })
    }

    try{
        var url=window.location.href
        var id=url.split("id=").slice(-1)[0]
        try{
            id=id.split("&").slice(0)[0]
        }
        catch (e) { }
        get(id)
    }
    catch(e){}
    document.onclick = function (e) {
        if (e.ctrlKey && e.shiftKey){
            if(window.location.href.indexOf("id")!=-1){
                getPDF(window.location.href);
            }
            else{
                open(e)
            }
            /*if(window.location.href=="http://www.jingshibang.com/home/"){
                downloadPageT()
            }
            if(window.location.href=="http://www.jingshibang.com/home/paper"){
                downloadPageE()
            }*/
        }
    }
})();

Guess you like

Origin blog.csdn.net/m0_73390085/article/details/131124134