Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The twitter share can now use a short URL, specified using a link tag… #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The url shared can be set using the og:url tag:

<meta property="og:url" content="http://your.url/to/share" />

The url shared via twitter can be set using a link tag with the "shortlink" rel attribute -- see http://microformats.org/wiki/rel-shortlink for more information:

<link rel="shortlink" href="http://bit.ly/1VV1wxD" />

Or if you are using [requirejs](http://requirejs.org), you can do:

Expand All @@ -49,6 +52,7 @@ Please let me know if you install this script on your site. Just star this repo

- Images are included inline in the CSS as SVG (perfect for Retina displays, loading time and to easily create new color schemes)
- Total size gzipped minified: 3.5K (equally split between css and javascript)
- This branch differs from xdamman's upstream branch by slightly modifying the way the Facebook link sharer is called. In particular, it now modifies the "description" property instead of the "caption" property, and populates values for the page using schema.org and microformats.org microformat data.

## Demos

Expand Down
2 changes: 1 addition & 1 deletion dist/selection-sharer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/selection-sharer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

this.appId = $('meta[property="fb:app_id"]').attr("content") || $('meta[property="fb:app_id"]').attr("value");
this.url2share = $('meta[property="og:url"]').attr("content") || $('meta[property="og:url"]').attr("value") || window.location.href;
this.shorturl = $('link[rel="shortlink"]').attr("href") || this.url2share;

this.getSelectionText = function(sel) {
var html = "", text = "";
Expand Down Expand Up @@ -192,7 +193,6 @@
var creator = $('meta[name="twitter:creator"]').attr("content") || $('meta[name="twitter:creator"]').attr("value");
if(creator) usernames.push(creator);


// We scrape the page to find a link to http(s)://twitter.com/username
var anchors = document.getElementsByTagName('a');
for(var i=0, len=anchors.length;i<len;i++) {
Expand All @@ -213,7 +213,7 @@
e.preventDefault();

var text = "“"+self.smart_truncate(self.textSelection.trim(), 114)+"”";
var url = 'http://twitter.com/intent/tweet?text='+encodeURIComponent(text)+'&related='+self.relatedTwitterAccounts+'&url='+encodeURIComponent(window.location.href);
var url = 'http://twitter.com/intent/tweet?text='+encodeURIComponent(text)+'&related='+self.relatedTwitterAccounts+'&url='+encodeURIComponent(self.shorturl);

// We only show the via @twitter:site if we have enough room
if(self.viaTwitterAccount && text.length < (120-6-self.viaTwitterAccount.length))
Expand All @@ -231,10 +231,18 @@
e.preventDefault();
var text = self.htmlSelection.replace(/<p[^>]*>/ig,'\n').replace(/<\/p>| /ig,'').trim();

var text = self.htmlSelection.replace(/<p[^>]*>/ig,'\n').replace(/<\/p>| /ig,'').trim();
var name = $('h1[itemprop="headline"]').text() || $('h1.p-name').text() || $('h1').text();
var img = $('img.u-img').attr('src') || $('img[itemprop="image"]').attr('src');
var picture = document.location.origin + img;

var url = 'https://www.facebook.com/dialog/feed?' +
'app_id='+self.appId +
'&display=popup'+
'&caption='+encodeURIComponent(text)+
'&picture='+encodeURIComponent(picture)+
'&caption='+document.domain+
'&name='+encodeURIComponent(name)+
'&description='+encodeURIComponent(text)+
'&link='+encodeURIComponent(self.url2share)+
'&href='+encodeURIComponent(self.url2share)+
'&redirect_uri='+encodeURIComponent(self.url2share);
Expand Down