@@ -110,6 +110,16 @@ export class PfeClipboard extends LitElement {
110110 @property ( { type : String , reflect : true , attribute : 'copy-from' } )
111111 copyFrom : string | 'url' | 'property' = 'url' ;
112112
113+ /**
114+ * Specify if current site is behind [https]
115+ */
116+ private _secure = window . isSecureContext ;
117+
118+ /**
119+ * Specify the copy type to be used depending on security and browser support
120+ */
121+ private _copyType : 'navigator' | 'queryCommand' | null = null ;
122+
113123 /**
114124 * A setter to set the content you would like to copy.
115125 * Only works if copy-from attribute is set to property.
@@ -143,6 +153,8 @@ export class PfeClipboard extends LitElement {
143153
144154 this . dispatchEvent ( deprecatedCustomEvent ( 'pfe-clipboard:connected' , { component : this } ) ) ;
145155
156+ this . _setCopyType ( ) ;
157+
146158 // This prevents a regression, default text used to be "Copy URL".
147159 // Now that component can copy _anything_ that's not ideal default text
148160 if ( this . copyFrom === 'url' && ! this . slots . hasSlotted ( 'text' ) ) {
@@ -219,7 +231,7 @@ export class PfeClipboard extends LitElement {
219231 /**
220232 * Checks to make sure the thing we may copy exists
221233 */
222- @ bound private _checkForCopyTarget ( ) {
234+ private _checkForCopyTarget ( ) {
223235 if ( this . copyFrom === 'property' ) {
224236 if ( ! this . contentToCopy ) {
225237 this . setAttribute ( 'disabled' , '' ) ;
@@ -235,6 +247,17 @@ export class PfeClipboard extends LitElement {
235247 }
236248 }
237249
250+ /**
251+ * Sets copy type depending on whether or not the current site is secure
252+ */
253+ private _setCopyType ( ) {
254+ if ( this . _secure && navigator . clipboard ) {
255+ this . _copyType = 'navigator' ;
256+ } else {
257+ this . _copyType = 'queryCommand' ;
258+ }
259+ }
260+
238261 /**
239262 * Event handler for any activation of the copy button
240263 */
@@ -380,22 +403,22 @@ export class PfeClipboard extends LitElement {
380403 if ( ! text ) {
381404 this . logger . error ( 'Copy function called, but no text was given to copy.' ) ;
382405 }
383- if ( navigator . clipboard ) {
384- // If the Clipboard API is available then use that
385- await navigator . clipboard . writeText ( text ) ;
386- return text ;
387- } else if ( document . queryCommandEnabled ( 'copy' ) ) {
388- // If execCommand("copy") exists then use that method
389- const dummy = document . createElement ( 'input' ) ;
390- document . body . appendChild ( dummy ) ;
391- dummy . value = text ;
392- dummy . select ( ) ;
393- document . execCommand ( 'copy' ) ;
394- document . body . removeChild ( dummy ) ;
395- return text ;
396- } else {
397- throw new Error ( 'Current browser does not support copying to the clipboard.' ) ;
406+
407+ switch ( this . _copyType ) {
408+ case 'navigator' :
409+ await navigator . clipboard . writeText ( text ) ;
410+ break ;
411+ case 'queryCommand' : {
412+ const dummy = document . createElement ( 'input' ) ;
413+ document . body . appendChild ( dummy ) ;
414+ dummy . value = text ;
415+ dummy . select ( ) ;
416+ document . execCommand ( 'copy' ) ;
417+ document . body . removeChild ( dummy ) ;
418+ break ;
419+ }
398420 }
421+ return text ;
399422 }
400423}
401424
0 commit comments