Action

Add to Readwise Reader

Posted by EvanR, Last update about 2 years ago

Get your access token via https://readwise.io/access_token

Steps

  • script

    const SOURCE_TYPE = "drafts";
    const BASE_URL = "https://readwise.io/api/v3/";
    
    let selectedText;
    
    if (editor.getSelectedText().length > 0) {
      selectedText = editor.getSelectedText();
    } else {
      selectedText = draft.content.replace(/(?:\r\n\n|\r|\n\n)/g, '<p>');
    }
    
    postToReadwise(selectedText, draft.displayTitle);
    
    function postToReadwise(selectedText, selectedBook) {
      const POST_URL = BASE_URL + "save/";
      const htmlString = `<div><p>${selectedText}</p></div>`;
      const uuid = draft.uuid;
      const randomUrl = `https://redir.getdrafts.com/open?uuid=${uuid}`;
      let credReadwise = Credential.create(
        "Readwise",
        "Highlight surfacing service."
      );
      credReadwise.addPasswordField("token", "API Token");
      credReadwise.authorize();
    
    
    
      let httpMain = HTTP.create();
      let respMain = httpMain.request({
        url: POST_URL,
        method: "POST",
        data: {
        		  title: selectedBook,
              url: randomUrl,
              html: htmlString,
              document: selectedBook,
              author: "Evan Rauner",
              tags: ["thoughts"],
        },
        headers: {
          Authorization: `Token ${credReadwise.getValue("token")}`,
        },
      });
    
      if (respMain.success) {
        return true;
      } else {
        console.log(`[${respMain.statusCode}] ${respMain.error}`);
        return false;
      }
    }
    
    function getRandomInt(max) {
      return Math.floor(Math.random() * max);
    }
    
    

Options

  • After Success Archive , Tags: readwise
    Notification Info
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.