async function init() {
do {
var next = await replaceHKTRPG_Text();
} while (next);
}
async function replaceHKTRPG_Text() {
let body = DocumentApp.getActiveDocument().getBody();
let target = body.findText("^(?im)/hk\\s+(.*)");
if (!target) return;
let text = target.getElement().asText().getText();
let result = await HKTRPG_Api(text.replace(/\/hk\s+/i, ''))
target.getElement().asText().setText('HKTRPG\n' + result.message);
return true;
}
async function HKTRPG_Api(query = 'cc 80') {
if (!query) return;
var url = 'https://www.hktrpg.com:20721/api'
+ '?msg=' + encodeURIComponent(query);
var response = UrlFetchApp.fetch(url, { 'muteHttpExceptions': true });
var json = response.getContentText();
var data = JSON.parse(json);
return data;
}