Click here to Skip to main content
15,884,629 members
Articles / Web Development / HTML

Quick Twitter Post from List

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
29 Feb 2016CPOL2 min read 6.5K   61   3  
Fast way to post pre-prepared twitter updates, without using the API

Introduction

NB: This is a tiny article, but more than a tip - I'm sure I'll find more to add to it down the road, but for the moment, it is what it is, and provides a fully working solution. :)

I had a requirement recently to be able to click a button, and send a tweet, without having to compose it.. 'one click tweet' let's say (well, in its actually two clicks, but!!) .. anyway, I didn't have the time to hook into the twitter API and do the entire OAuth thing, and frankly, for the needs of this mini-project, that would have been total overkill. My simple requirement was - have a list of potential statuses on a list in front of me, and be able to click one to send. Fast, simple. This is the quick and dirty solution!

Step 1 - Click !

Image 1

Step 2 - Click !

Image 2

Using the Code

Everything about this little solution is very standard - first, setup some simple HTML, and style with our friend Bootstrap. Stick in a class name (in this case 'shoutout') that we will use to hook our JavaScript to.

HTML
<div class="list-group">
<a href="#" class="list-group-item list-group-item-success shoutOut">Weehoo! .. on a roll :) </a>
<a href="#" class="list-group-item list-group-item-info shoutOut">Wow - did you see that?! #howzat!</a>
<a href="#" class="list-group-item list-group-item-warning shoutOut">Keep going, almost 
        there #Motivation</a>
<a href="#" class="list-group-item list-group-item-danger shoutOut">On nos!! somefink 
        wrong! :/ #myBad</a>
</div>

Next, stick in some Javascript/JQuery (whatever your drink of choice) that grabs the context of what was clicked by hooking into anything that uses the 'shoutOut' class, and captures its 'innerText'. Finally, encode the innerText, and open a new browser window with a target of '_blank', to the constructed URL string var 'tweetOut'.

JavaScript
<script>
        $('.shoutOut').click(
         function(){
           var tweetOut = 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(this.innerText)
           window.open(tweetOut, '_blank');
           return false;
          }
        )
</script>

Points of Interest

Three things of note:

  1. You need to have your browser logged into Twitter before using the code for the first time, otherwise it naturally, takes more than two clicks. :)
  2. Ensure you use the 'encodeURIComponent' method so that any hashtags, etc. get rendered correctly.
  3. Make sure that the last line in your function returns false.

That's it, #JobDone.

If you need it, there's a small project attached with the code.

History

  • 29th February, 2016 - V1. Published

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer SocialVoice.AI
Ireland Ireland
Allen is CTO of SocialVoice (https://www.socialvoice.ai), where his company analyses video data at scale and gives Global Brands Knowledge, Insights and Actions never seen before! Allen is a chartered engineer, a Fellow of the British Computing Society, a Microsoft mvp and Regional Director, and C-Sharp Corner Community Adviser and MVP. His core technology interests are BigData, IoT and Machine Learning.

When not chained to his desk he can be found fixing broken things, playing music very badly or trying to shape things out of wood. He currently completing a PhD in AI and is also a ball throwing slave for his dogs.

Comments and Discussions

 
-- There are no messages in this forum --