Advertisement
Spark_code

Untitled

Apr 17th, 2022
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function invoke(action, version, params = {}) {
  2.     return new Promise((resolve, reject) => {
  3.         const xhr = new XMLHttpRequest();
  4.         xhr.addEventListener('error', () => reject('failed to issue request'));
  5.         xhr.addEventListener('load', () => {
  6.             try {
  7.                 const response = JSON.parse(xhr.responseText);
  8.                 if (Object.getOwnPropertyNames(response).length != 2) {
  9.                     throw 'response has an unexpected number of fields';
  10.                 }
  11.                 if (!response.hasOwnProperty('error')) {
  12.                     throw 'response is missing required error field';
  13.                 }
  14.                 if (!response.hasOwnProperty('result')) {
  15.                     throw 'response is missing required result field';
  16.                 }
  17.                 if (response.error) {
  18.                     throw response.error;
  19.                 }
  20.                 resolve(response.result);
  21.             } catch (e) {
  22.                 reject(e);
  23.             }
  24.         });
  25.  
  26.         xhr.open('POST', 'http://127.0.0.1:8765');
  27.         xhr.send(JSON.stringify({ action, version, params }));
  28.     });
  29. }
  30.  
  31. await invoke('requestPermission', 6);
  32.  
  33.  
  34. await invoke('guiAddCards', 6, {
  35.     "note": {
  36.         "deckName": "Default",
  37.         "modelName": "Cloze",
  38.         "fields": {
  39.             "Text": "The capital of Romania is {{c1::Bucharest}}",
  40.             "Extra": "Romania is a country in Europe"
  41.         },
  42.         "options": {
  43.             "closeAfterAdding": true
  44.         },
  45.         "tags": [
  46.           "countries"
  47.         ],
  48.         "picture": [{
  49.             "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/EU-Romania.svg/285px-EU-Romania.svg.png",
  50.             "filename": "romania.png",
  51.             "fields": [
  52.                 "Extra"
  53.             ]
  54.         }]
  55.     }
  56. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement