| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
const hasFeature = navigator.clipboard && 'writeText' in navigator.clipboard; |
| 8 |
if ( hasFeature ) { |
| 9 |
|
| 10 |
const $btn = $( '<button>' ) |
| 11 |
.attr( 'type', 'button' ) |
| 12 |
.text( mw.msg( 'syntaxhighlight-button-copy' ) ) |
| 13 |
.on( 'click', function () { |
| 14 |
const btn = this; |
| 15 |
const wrapper = btn.closest( '.mw-highlight-copy' ); |
| 16 |
const preNode = wrapper && wrapper.querySelector( 'pre' ); |
| 17 |
const content = preNode && preNode.textContent.trim(); |
| 18 |
try { |
| 19 |
navigator.clipboard.writeText( content ); |
| 20 |
} catch ( e ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
btn.textContent = mw.msg( 'syntaxhighlight-button-copied' ); |
| 24 |
setTimeout( () => { |
| 25 |
btn.textContent = mw.msg( 'syntaxhighlight-button-copy' ); |
| 26 |
}, 5000 ); |
| 27 |
} ); |
| 28 |
|
| 29 |
mw.hook( 'wikipage.content' ).add( ( $content ) => { |
| 30 |
$content.find( '.mw-highlight-copy:not(.mw-highlight-copy--bound)' ) |
| 31 |
.append( $btn.clone( true ) ) |
| 32 |
.addClass( 'mw-highlight-copy--bound' ); |
| 33 |
} ); |
| 34 |
} |