Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取到的值为undefined报错解决办法 #6

Open
Secd0g opened this issue Apr 8, 2023 · 0 comments
Open

获取到的值为undefined报错解决办法 #6

Secd0g opened this issue Apr 8, 2023 · 0 comments

Comments

@Secd0g
Copy link

Secd0g commented Apr 8, 2023

如果豆瓣有些出版年或者出版社等信息没有的话就会出现undefined错误,导致不能同步到notion当中,可以将getBookInfo的函数修改一下即可

function getBookInfo(){
    let infos = document.getElementById("info").getElementsByClassName("pl");
    var book = {};
    book["title"] = document.getElementsByTagName("h1")[0].getElementsByTagName("span")[0].innerText;
    book["封面"] = document.getElementsByClassName("nbg")[0].href;
    book["评分"] = document.getElementsByClassName("rating_num")[0].innerText;
    for(let info of infos) {
        if(info.innerText === "作者") {
            book["作者"] = info.nextSibling.nextSibling.innerText
        }
        else if(info.innerText === "出版社:") {
            if(info.nextSibling.nextSibling.innerText == ""){
                book["出版社"] =info.nextSibling.textContent
            }else{
                book["出版社"] = info.nextSibling.nextSibling.innerText
            }
             
        }
        else {
            prop = info.innerText.substr(0,info.innerText.length-1)
            book[prop] = info.nextSibling.data
        }  
    }
    var preInfos = ["作者","评分","出版社","出版年","页数","ISBN"]
    for(let info of preInfos){
        if(book[info]==undefined){
            book[info]=""
        }
    }

    return book
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant