Skip to content

Commit 6b42547

Browse files
Copy link to clipboard & WebShare API support (#31)
1 parent 75825c1 commit 6b42547

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Simple, powerful, customizable and super lightweight (1 Kb Gzip) social buttons
1414
* Android
1515
* iOS
1616

17+
Copy to clipboard is not supported on IE, see [browser compatibility for more information](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#browser_compatibility)
18+
19+
WebShare API is only partially supported, see [browser compatibility for more information](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#browser_compatibility)
20+
1721
## Install
1822

1923
### Available in NPM
@@ -83,6 +87,8 @@ Hacker News | hn
8387
Xing | xi
8488
EMail | mail
8589
Print | print
90+
Copy | copy
91+
WebShare API | shareSheet
8692

8793
## Customizing
8894

example/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
<a class="btn-xing" data-id="xi"><i class="fab fa-xing"></i> Xing</a>
4646
<a class="btn-mail" data-id="mail"><i class="fas fa-at"></i> EMail</a>
4747
<a class="btn-print" data-id="print"><i class="fas fa-print"></i> Print</a>
48+
<a class="btn-copy" data-id="copy"><i class="fas fa-copy"></i> Copy</a>
49+
<a class="btn-shareSheet" data-id="shareSheet"><i class="fas fa-ellipsis-h"></i> ShareSheet</a>
4850
</div>
4951

5052
<script src="../src/share-buttons.js"></script>

src/share-buttons.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
HN_CLASS_NAME = 'hn',
4545
XI_CLASS_NAME = 'xi',
4646
MAIL_CLASS_NAME = 'mail',
47-
PRINT_CLASS_NAME = 'print';
47+
PRINT_CLASS_NAME = 'print',
48+
COPY_CLASS_NAME = 'copy',
49+
SHARESHEET_CLASS_NAME = 'shareSheet';
4850

4951
/**
5052
* Method for get string in the special format by arguments
@@ -75,6 +77,24 @@
7577
for (i = share.length; i--;) {
7678
initForElement(share[i]);
7779
}
80+
81+
// Check if navigator.clipboard is supported. If not, hide all shareSheet buttons.
82+
if(!navigator.clipboard) {
83+
var buttons = document.querySelectorAll(`[data-id="${COPY_CLASS_NAME}"]`);
84+
for (i = 0; i < buttons.length; i++) {
85+
buttons[i].style.display = 'none';
86+
}
87+
console.log('navigator.clipboard(): This feature is not supported on this browser or operating system.');
88+
}
89+
90+
// Check if navigator.share is supported. If not, hide all shareSheet buttons.
91+
if(!navigator.canShare) {
92+
var buttons = document.querySelectorAll(`[data-id="${SHARESHEET_CLASS_NAME}"]`);
93+
for (i = 0; i < buttons.length; i++) {
94+
buttons[i].style.display = 'none';
95+
}
96+
console.log('navigator.share(): This feature is not supported on this browser or operating system.');
97+
}
7898
};
7999

80100
/**
@@ -208,6 +228,14 @@
208228
return encodeURIComponent(text);
209229
};
210230

231+
/**
232+
* Method for decoding URL format to text
233+
* @param {string} text
234+
*/
235+
var decode = function (text) {
236+
return decodeURIComponent(text);
237+
};
238+
211239
/**
212240
* Method for handling chosen links
213241
* @param {string} id
@@ -361,6 +389,25 @@
361389
window.print();
362390
break;
363391

392+
case COPY_CLASS_NAME:
393+
navigator.clipboard.writeText(decode(url));
394+
break;
395+
396+
case SHARESHEET_CLASS_NAME:
397+
text = decode(mergeForTitle([title, desc]));
398+
var shareData = {
399+
title: text,
400+
text: text,
401+
url: decode(url),
402+
};
403+
404+
navigator.share(shareData).then(() => {
405+
console.log('navigator.share(): Success');
406+
}).catch((err) => {
407+
console.log('navigator.share(): Error', err);
408+
});
409+
break;
410+
364411
default:
365412
break;
366413
}

0 commit comments

Comments
 (0)