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 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!
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.
ps: request https://api.bilibili.com/pgc/web/season/section?season_id=32638, and parse the json to the items.
// 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;
}
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
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.