Action

Add to Readwise (choose Book)

Posted by demaya, Last update 3 months ago

Adds either the selected text, or if no selected text, the content of the draft to Readwise. Before pushing to Readwise you can select which Book it should be added to.

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

Steps

  • script

    
    postToReadwise(selectedText(), selectBook(getBooksFromReadwise()));
    
    function postToReadwise(selectedText, selectedBook) {
        const BASEURL = "https://readwise.io/api/v2/highlights/";
    		
    	let credReadwise = Credential.create("Readwise", "Highlight surfacing service.");
    	credReadwise.addPasswordField("token", "API Token");
    	credReadwise.authorize();
    
    	let httpMain = HTTP.create();
    	let respMain = httpMain.request(
    	{
    		"url" : BASEURL,
    		"method" : "POST",
    		"data": 
    		{
    			"highlights" : [
                    {
                        "text" : escape(selectedText.toString()),
                        "title": escape(selectedBook.toString())
                    }
                ]
    		},
    		"headers" :
    		{
    			"Authorization" : `Token ${credReadwise.getValue("token")}`
    		}
    	});
    
    	if (respMain.success) return true;
    	else
    	{
    		console.log(`[${respMain.statusCode}] ${respMain.error}`);
    		return false;
    	}
    }
    
    function selectBook(data) {
        
        let actionPrompt = new Prompt();
        actionPrompt.isCancellable = true;
        actionPrompt.title = "Export to Readwise";
        actionPrompt.message = "Books to select from: "+data.count;
        data.results.forEach(function(book) {
            actionPrompt.addButton(book.title);
        });
    
        actionPrompt.show();
    
        let selectedBook = actionPrompt.buttonPressed;
    
        return selectedBook;
    }
    
    function getBooksFromReadwise()
    {
    	const BASEURL = "https://readwise.io/api/v2/books/";
    
    	let credReadwise = Credential.create("Readwise", "Highlight surfacing service.");
    	credReadwise.addPasswordField("token", "API Token");
    	credReadwise.authorize();
    
    	let httpMain = HTTP.create();
    	let respMain = httpMain.request(
    	{
    		"url" : BASEURL,
            "method" : "GET",
    		"headers" :
    		{
    			"Authorization" : `Token ${credReadwise.getValue("token")}`
    		}
    	});
    
        if(respMain.success) {
            let responseData = JSON.parse(respMain.responseText);
    			return responseData;
        } else {
            console.log(`[${respMain.statusCode}] ${respMain.error}`);
            return false;
        }
    
    
    }
    
    function selectedText() {
        let selectedText;
        if (editor.getSelectedText().length > 0) {
            selectedText = editor.getSelectedText();
        } else {
            selectedText = draft.content;
        }
        return selectedText;
    }

Options

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