Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function invoke(action, version, params = {}) {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.addEventListener('error', () => reject('failed to issue request'));
- xhr.addEventListener('load', () => {
- try {
- const response = JSON.parse(xhr.responseText);
- if (Object.getOwnPropertyNames(response).length != 2) {
- throw 'response has an unexpected number of fields';
- }
- if (!response.hasOwnProperty('error')) {
- throw 'response is missing required error field';
- }
- if (!response.hasOwnProperty('result')) {
- throw 'response is missing required result field';
- }
- if (response.error) {
- throw response.error;
- }
- resolve(response.result);
- } catch (e) {
- reject(e);
- }
- });
- xhr.open('POST', 'http://127.0.0.1:8765');
- xhr.send(JSON.stringify({ action, version, params }));
- });
- }
- await invoke('requestPermission', 6);
- await invoke('guiAddCards', 6, {
- "note": {
- "deckName": "Default",
- "modelName": "Cloze",
- "fields": {
- "Text": "The capital of Romania is {{c1::Bucharest}}",
- "Extra": "Romania is a country in Europe"
- },
- "options": {
- "closeAfterAdding": true
- },
- "tags": [
- "countries"
- ],
- "picture": [{
- "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/EU-Romania.svg/285px-EU-Romania.svg.png",
- "filename": "romania.png",
- "fields": [
- "Extra"
- ]
- }]
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement