Get Bible Verse (ESV)

Posted by nate561, Last update 5 months ago

This action is powered by the API provided at ESV.org. You can create an account there (api.esv.org) to get an API token for this action. When you first run this action please enter the token you received from ESV.org to authenticate.

To use this script either place your cursor in the draft and run the action or select a passage that is already typed out and run the action. If you have no text selected the action will prompt for you to enter a passage reference (ex. John 3:16). If you have a verse reference already written in the draft you can select the text and run the action. The action will see that text is selected and use that to search for a passage.

Steps

  • script

    /*
    *
    
    This script use the api located at api.esv.org. You must first create an account at this site and then create a project to gain an API token. Once you have the token you can run this scirpt and enter your token when promted to store it for future use. 
    
    *
    */
    
    var credential = Credential.create("ESV API", "An API Used to search for bible verses in from the ESV bible translation");
    
    credential.addPasswordField("token", "Token");
    
    credential.authorize();
    
    var sel = editor.getSelectedText(); // retrieve selected text
    
    var selRange = editor.getSelectedRange();
    
    if (!sel || sel.length == 0) {
    let p = Prompt.create();
    
    p.title = "Please enter a verse refrence";
    p.addTextField("Verse", "Verse","",{'wantsFocus':true});
    p.addButton("Search","Search","isDefault");
    
    
    p.show();
    if (p.buttonPressed == "Search") {
       var sel = p.fieldValues["Verse"];
     }
    }
    
    var http = HTTP.create();
    
    //You can set these to "true" or "false" to add different pieces to the verse output
    var include_copyright = "false";
    var include_headings = "false"; 
    var include_passage_ref = "false"
    var include_footnote = "false"
    
    
    var endpoint = "https://api.esv.org/v3/passage/text/?q="+ encodeURI (sel)+"&include-copyright="+include_copyright+"&include-headings="+include_headings+"&include-passage-references="+include_passage_ref+"&include-footnote-body="+include_footnote;
    
    var response = http.request({
      	"method":"GET", 
    	"url": endpoint,
    	"headers": {
    		"Authorization": "Token " + credential.getValue("token")
    	}
    });
    
    if (response.success) {
      var text = response.responseText;
      var data = response.responseData;
    }
    else {
    	console.log(response.statusCode);
    	console.log(response.error); 
    }
    
    var passage = JSON.parse(text).passages.toString();
    var ref = JSON.parse(text).canonical;
    
    var ParseNewLine = /\n/gi;
    var ParseTabs = /   |\v|  /gi;
    passage = passage.replace(ParseNewLine, '');
    passage = passage.replace(ParseTabs, '');
    var returnVal = passage + "\n" + ref; 
    editor.setSelectedText(returnVal);
    
    var selRange = editor.getSelectedRange();
    editor.setSelectedRange(selRange[0]+selRange[1],0);

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.