Action

Replace Twitter URL with Content

Posted by sylumer, Last update over 1 year ago - Unlisted

UPDATES

over 1 year ago

Added a line to save a version of the draft.

Replace selected Twitter tweet (post) URL in a draft with the content of the tweet.

Utilises Twitter v1.1 API.

Steps

  • script

    // Twitter API (v1.1) call to get content of a Tweet by ID
    function getTweet(p_strID)
    {
    	let objTwitter = Twitter.create();
    	let objResponse = objTwitter.request({"url": "https://api.twitter.com/1.1/statuses/show.json?id=" + p_strID, "method": "GET"});
    	if (objResponse.statusCode == 200) return objResponse.responseData.text;
    	else
    	{
    		console.log("Twitter API Call Error: " + objResponse.statusCode.toString());
    		context.fail("");
    		return;
    	}
    }
    
    // First, we'll explicitly save a version, just in case; we are after all modifying the draft
    draft.saveVersion();
    
    //Get the selection and process it as a Twitter URL. Get the tweet ID from the URL and use the function above to get the content
    let strTweetContent = getTweet(editor.getSelectedText().split("/").pop());
    
    // If we got a response, replace the selected text witht he content of the tweet.
    if (strTweetContent != undefined) editor.setSelectedText(strTweetContent);
    
    // Move the cursor to the end of the current selection and activate the editor.
    let [intStart, intEnd] = editor.getSelectedRange();
    editor.setSelectedRange(intStart + intEnd, 0);
    editor.activate();

Options

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