Action

Send to flomo

Posted by geekdada, Last update almost 4 years ago

特性

  1. 将整篇 draft 添加至 flomo;
  2. 默认添加 #Drafts 方便后期归纳整理;
  3. 读取 draft 中的 tag 添加至文本末尾;

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    async function main() {
      const { api } = getCredential();
    
      if (!api) {
        app.displayErrorMessage('没有配置 API 信息');
        context.cancel();
        return;
      }
    
      if (!draft.content) {
        app.displayErrorMessage('内容为空');
        context.cancel();
        return;
      }
    
      const tags = draft.tags ? draft.tags : [];
    
      tags.push('Drafts');
    
      let content = draft.content;
    
      if (tags.length) {
        content += '\n\n' + tags.map((tag) => `#${tag}`).join(' ');
      }
    
      putMemo(api, content);
    }
    
    function putMemo(api, content) {
      const http = HTTP.create();
    
      const response = http.request({
        url: api,
        method: 'POST',
        encoding: 'json',
        data: {
          content,
        },
      });
    
      if (response.error) {
        const err = new Error(response.error);
        err.response = JSON.stringify(response);
        throw err;
      }
    }
    
    function getCredential() {
      const credential = Credential.create('Send to flomo', '输入 API 信息');
      credential.addTextField('api', 'API');
      credential.authorize();
    
      return {
        api: credential.getValue('api'),
      };
    }
    
    main()
      .then(() => script.complete())
      .catch((err) => {
        console.log(err);
        context.fail(err.message);
        script.complete();
      });
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.