in translator, how to send a Synchronous request(CORS)

I will create a translator for bilibili(e.g. https://www.bilibili.com/bangumi/media/md28227950).
I create a XMLHttpRequest to get the episode list by the API(https://api.bilibili.com/pgc/web/season/section?season_id=32638), but the responseText is return empty string. And i try to use ZU.doGet, it can reuturn the expected value, but it is asynchronously.

now i need a method to request the api, and Synchronous, an Support CORS. but i don't knonw how can i do? need your help! thanks!
  • I'm not sure exactly what you're trying to do, but you can only use the functions available to translators, such as doGet(). You can't use XHR directly. (And synchronous XHR has long been deprecated or banned in browsers.)

    There's nothing wrong with the async doGet(), which is used in doWeb() in many translators. It's just not currently possible to make network requests in detectWeb(). That will likely be possible in a future version with a new Promise-based API for network requests in translators.
  • I want use doGet() in getSearchResults() and return the items. so i need a synchronous request.

    ps: request https://api.bilibili.com/pgc/web/season/section?season_id=32638, and parse the json to the items.
  • edited July 7, 2020
    function getSearchResults(doc, checkOnly) {
    // do something...
    ZU.doGet('https://api.bilibili.com/pgc/web/season/section?season_id=' + obj.season_id, function (pageSection) {
    obj = JSON.parse(pageSection);
    for (var item of obj.result.main_section.episodes) {
    let href = item.share_url;
    let title = '第' + item.title + '集 ' + ZU.trimInternal(item.long_title);
    if (!href || !title) continue;
    found = true;
    if (checkOnly) {
    return found;
    }
    items[href] = title;
    }
    });
    // TODO How to wait the request return and get items???
    return found ? items : false;
    }
  • You don't have to use the templated structure of the translators: getSearchResults is a good way for many translators to avoid false positives in detectWeb and to grab search results off a page in a standardized way, but you don't have to use it in a translator.

    I haven't looked at the site you're linking to in detail, but from what you describe it sounds like you'd want to call Z.selectItems from within a ZU.doGet call to the API in doWeb. As dstillman implies, you'll want to detect multiples without making any network requests by inspecting the CSS/DOM
  • edited July 7, 2020
    e.g. https://www.bilibili.com/bangumi/media/md28227950

    I had tried inspect the CSS/DOM as use the templated structure of the translators. but i don't grab the href. the web had not use the <a> tag, and use event listener. so i don't know how to do.
Sign In or Register to comment.