Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
shr-buttons
Advanced tools
Simple, clean, customizable sharing buttons.
The default share buttons used by the social networks are not only ugly to look at (sorry, they just are) but they usually depend on iframes and all sorts of other horrible, slow code. That led to creating shr (short for share).
<span>
as button type hacks.Oh and yes, it works with Bootstrap.
Check out the changelog
If you have any cool ideas or features, please let me know by creating an issue or of course, forking and sending a pull request.
To set up Shr, you first must include the Shr CSS and JS along with the sprite that contains the social icons. There are two ways you can load the CSS and JS:
If you want to use our CDN, you can use the following:
<link rel="stylesheet" href="https://shr.one/2.0.0-beta.1/shr.css" />
<script src="https://shr.one/2.0.0-beta.1/shr.js"></script>
If you want to use the default css, add the shr.css
file from /dist into your head, or even better use shr.less
or shr.scss
file included in /src
in your build to save an HTTP request.
<link rel="stylesheet" href="dist/shr.css" />
You will also need to add the shr.js
file from the /dist into your head.
<script src="dist/shr.js"></script>
To get the beautiful images for each social network, you must include the sprite.svg
that is distributed with Shr. To do this, add the following script to your document near the closing body tag:
<script>
(function(d, u) {
var x = new XMLHttpRequest();
var b = d.body;
// Check for CORS support
// If you're loading from same domain, you can remove the if statement
// XHR for Chrome/Firefox/Opera/Safari
if ('withCredentials' in x) {
x.open('GET', u, true);
}
// XDomainRequest for older IE
else if (typeof XDomainRequest != 'undefined') {
x = new XDomainRequest();
x.open('GET', u);
} else {
return;
}
x.send();
x.onload = function() {
var c = d.createElement('div');
c.setAttribute('hidden', '');
c.innerHTML = x.responseText;
b.insertBefore(c, b.childNodes[0]);
};
})(document, '../dist/sprite.svg');
</script>
What this does, is creates a new XMLHttpRequest
and loads the sprite.svg
file. Make sure you update the file path to the right location for your project.
The sprite.svg
contains all of the SVG icons for the social networks. If you jump ahead to any of the social networks you will see a <use>
tag (Ex: Google: <svg><use xlink:href="#shr-google"></use></svg>+1
). The href
attribute in the use
references a single SVG within the sprite. We load the entire sprite so it's only one HTTP request and it speeds up your site!
Now that everything has been loaded correctly, all you have to do is call:
shr.setup({});
You can pass a JSON object of all of the different options to this setup function.
There are a ton of options that ship with Shr. These allow you to customize the library to your needs.
Default: false
If you are are just debugging Shr in a development environment, you can turn on debugging when you setup.
shr.setup({
debug: true,
});
Default: data-shr-network
The selector is the attribute on the element that Shr is binding to. This contains the network for the button (ex: data-shr-network="google"
). You can change this if you want to use a different selector.
shr.setup({
selector: 'data-your-selector-name',
});
Default: share-count
When adding the share count element to the screen, this is the className used to style it.
This is a nested option within the count
object.
shr.setup({
count: {
className: 'your-class-name',
},
});
Default: false
Sometimes your URL has not been shared yet. You can choose whether or not you want to display 0 shares or not. Some APIs don't allow for the actual count, so those APIs will just be a link to share (such as Twitter) and won't show the count if this is turned on or not.
This is a nested option within the count
object.
shr.setup({
count: {
displayZero: true,
},
});
Default: true
By default, Shr shortens the amount of shares to an easier to read number. Say you have a URL that went viral and you have over 1 million shares. By default, Shr shows this as 1 M. You can, however, turn this off and show the exact amount of shares.
This is a nested option within the count
object.
shr.setup({
count: {
format: false,
},
});
Default: after
By default, the number of shares shows up after the social icon. This means it's to the right of the icon. You can change this to be before the social icon (the left of the icon) by setting this value to before
.
This is a nested option within the count
object.
shr.setup({
count: {
position: 'before',
},
});
Default: true
When the user clicks the share icon, we automatically update the count. However, this is assuming that the user went through with the sharing. This is for speed and reactivity. If you don't want this behavior, you can set this value to false.
This is a nested option within the count
object.
shr.setup({
count: {
increment: false,
},
});
Default:
function(count, classname, position) {
return '<span class="' + classname + ' ' + classname + '--' + position + '">' + count + '</span>';
},
This is the method that builds the <span>
to display the number of the share. If you want, you can override this method to add certain elements or parameters, however it's not recommended. Shr, by default, is very semantic and uses the proper elements when needed. You should be able to style this to get what you are looking for.
This is a nested option within the count
object.
shr.setup({
count: {
html: function(count, classname, position) {
return '{YOUR CONSTRUCTED HTML HERE}';
},
},
});
Default:
popup: {
width: 500,
height: 500
}
For Google, you can define the width and height of the popup used to complete your share. By default, the popup is 500px
by 500px
. To change these values, set them on setup:
shr.setup({
google: {
popup: {
width: 1024,
height: 768,
},
},
});
Default:
popup: {
width: 640,
height: 270
}
For Facebook, you can define the width and height of the popup used to complete your share. By default, the popup is 640px
by 270px
. To change these values, set them on setup:
shr.setup({
facebook: {
popup: {
width: 1024,
height: 768,
},
},
});
Default:
popup: {
width: 640,
height: 270
}
For Twitter, you can define the width and height of the popup used to complete your share. By default, the popup is 640px
by 270px
. To change these values, set them on setup:
shr.setup({
twitter: {
popup: {
width: 1024,
height: 768,
},
},
});
Default:
popup: {
width: 750,
height: 550
}
For Pinterest, you can define the width and height of the popup used to complete your share. By default, the popup is 750px
by 550px
. To change these values, set them on setup:
shr.setup({
pinterest: {
popup: {
width: 1024,
height: 768,
},
},
});
Default:
storage: {
key: `shr`,
ttl: 300000
}
To save requests and speed up your site, Shr saves all of the values used to local storage. The two keys you can set are the key
of the local storage and the time to live (AKA: how long do you want these values to last before we refresh). These can be customized to your liking in the setup like.
The storage key
must be a valid JSON
type key meaning it can not contain special characters or whitespace.
The storage ttl
must be an integer representin the number of seconds the storage is valid for.
shr.setup({
storage: {
key: `your_key`
ttl: 100
}
});
Shr provides a ton of networks that can be used on your site. Each button has certain attributes that need to be defined in order for Shr to operate efficiently and effictively. Below are descriptions and an example of each button and how to use it.
This button allows you to share your URL on Google Plus. A count is not available for this button.
<a
href="https://plus.google.com/share?url={YOUR_URL_ENCODED_URL}"
target="_blank"
class="shr-button shr-button-google"
data-shr-network="google"
>
<svg><use xlink:href="#shr-google"></use></svg>+1
</a>
When entering the URL you wish to share on Google Plus, make sure that it's properly URL encoded!
This button allows you to tweet a URL on Twitter. A count is not available for this button.
<a
href="https://twitter.com/intent/tweet?text={URL_ENCODED_TWEET_TEXT}&url={URL_ENCODED_SHARE_URL}&via={TWITTER_USERNAME}"
target="_blank"
class="shr-button shr-button-twitter"
data-shr-network="twitter"
>
<svg><use xlink:href="#shr-twitter"></use></svg>Tweet
</a>
There are 3 variables you can add to your Twitter share:
text
- This is the text of your tweet. Makes ure it's properly URL encoded.url
- This is the URL you wish to share via Twitter. The URL needs to be properly encoded as well.via
- This is who is sharing the tweet (your username)This button allows you to post a pin to Pinterest. A count is not available for this button.
<a
href="http://pinterest.com/pin/create/button/?url={URL_ENCODED_URL}&media={URL_ENCODED_IMAGE_URL}&description={URL_ENCODED_DESCRIPTION}."
target="_blank"
class="shr-button shr-button-pinterest"
data-shr-network="pinterest"
>
<svg><use xlink:href="#shr-pinterest"></use></svg>Pin it
</a>
There are 3 variables you can add to your Pinterest pin:
url
- This is the URL you wish to pin on Pinterest. Make sure it's properly URL encoded.media
- This is the URL to an image you wish to pin. Make sure it's properly URL encoded.description
- This is a URL encoded description for your pin.This button allows you to share on Facebook.
<a
href="https://www.facebook.com/sharer/sharer.php?u={URL_ENCODED_URL}"
target="_blank"
class="shr-button shr-button-facebook"
data-shr-network="facebook"
>
<svg><use xlink:href="#shr-facebook"></use></svg>Share
</a>
When entering your URL for Facebook, make sure it's properly URL encoded! The number of shares will appear next to the button for the URL you are sharing.
This button allows you to star a repo on GitHub and shows the current number of stars for the project.
<a href="{{ URL_OF_REPO }}" target="_blank" class="shr-button shr-button-github" data-shr-network="github">
<svg><use xlink:href="#shr-github"></use></svg>Star
</a>
Safari | Firefox | Chrome | Opera | IE9 | IE10+ |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
developed by @sam_potts / sampotts.me
to run, not my time - I donate that for free but domains, hosting and more. Any help is appreciated... Donate to support Shr
Thanks to Fastly for providing the CDN services.
FAQs
Simple, customizable sharing buttons
The npm package shr-buttons receives a total of 0 weekly downloads. As such, shr-buttons popularity was classified as not popular.
We found that shr-buttons demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.