An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
August 23, 2019 06:24 pm GMT
Original Link: https://dev.to/codepo8/a-simple-bookmarklet-to-tweet-the-current-page-1mlk
A simple bookmarklet to tweet the current page
I like tweeting about pages I read in the following format:
"Headline"
Most "share this" buttons add a lot of cruft and if there is none, it is annoying having to copy the text first and then the URL into Twitter.
With this bookmarklet, you can highlight some text, click the bookmarklet and get a tweet window in a new tab:
javascript:(function(){ n = getSelection().anchorNode; t = n.nodeType === 3 ? n.data : n.innerText; window.open(`https://twitter.com/intent/tweet? text="${encodeURIComponent(t+'"\n\n')} ${document.location.href}`); })();
Explanation:
- We wrap the whole thing in an IIFE to avoid the browser redirecting to "javascript://"
- We get the
anchorNode
of the currentSelection
object - We check the type. If it's a text node, we read the
data
, if it is HTML, we get theinnerText
- We open a window (which these days results in a new tab) and call the Twitter intend with the text followed by two linebreaks and the URL of the document.
That's it.
Here's the minified version to add to your bookmarks:
javascript:(function(){n=getSelection().anchorNode;t=n.nodeType===3?n.data:n.innerText;window.open(`https://twitter.com/intent/tweet?text="${encodeURIComponent(t+'"\n\n')}${document.location.href}`); })();)
Copy this, go to your bookmarks toolbar (change in view if it isn't visible), and set the location as the code above:
Voil, happy tweeting.
Original Link: https://dev.to/codepo8/a-simple-bookmarklet-to-tweet-the-current-page-1mlk
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To