| 1 |
const utils = require( './utils.js' ); |
| 2 |
|
| 3 |
function init( $pageContainer ) { |
| 4 |
$pageContainer.find( '.ext-discussiontools-init-timestamplink' ).on( 'click', ( e ) => { |
| 5 |
if ( !utils.isUnmodifiedLeftClick( e ) ) { |
| 6 |
|
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
let link = e.currentTarget.href; |
| 13 |
try { |
| 14 |
|
| 15 |
const decodedLink = decodeURI( link ); |
| 16 |
|
| 17 |
|
| 18 |
if ( new URL( decodedLink ).toString() === link ) { |
| 19 |
link = decodedLink; |
| 20 |
} |
| 21 |
} catch ( err ) {} |
| 22 |
copyLink( link ); |
| 23 |
} ).attr( 'data-event-name', 'discussiontools.permalink-copied' ); |
| 24 |
} |
| 25 |
|
| 26 |
function copyLink( link ) { |
| 27 |
const $win = $( window ); |
| 28 |
const scrollTop = $win.scrollTop(); |
| 29 |
|
| 30 |
const $tmpInput = $( '<input>' ) |
| 31 |
.val( link ) |
| 32 |
.addClass( 'noime' ) |
| 33 |
.css( { |
| 34 |
position: 'fixed', |
| 35 |
top: 0 |
| 36 |
} ) |
| 37 |
.appendTo( 'body' ) |
| 38 |
.trigger( 'focus' ); |
| 39 |
$tmpInput[ 0 ].setSelectionRange( 0, link.length ); |
| 40 |
let copied; |
| 41 |
try { |
| 42 |
copied = document.execCommand( 'copy' ); |
| 43 |
} catch ( err ) { |
| 44 |
copied = false; |
| 45 |
} |
| 46 |
if ( copied ) { |
| 47 |
mw.notify( mw.msg( 'discussiontools-permalink-comment-copied' ) ); |
| 48 |
} |
| 49 |
$tmpInput.remove(); |
| 50 |
|
| 51 |
|
| 52 |
function afterNextScroll() { |
| 53 |
|
| 54 |
|
| 55 |
$win.scrollTop( scrollTop ); |
| 56 |
|
| 57 |
|
| 58 |
setTimeout( () => { |
| 59 |
$win.scrollTop( scrollTop ); |
| 60 |
} ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
$win.one( 'scroll', afterNextScroll ); |
| 66 |
|
| 67 |
|
| 68 |
setTimeout( () => { |
| 69 |
$win.off( 'scroll', afterNextScroll ); |
| 70 |
}, 1000 ); |
| 71 |
} |
| 72 |
|
| 73 |
module.exports = { |
| 74 |
init: init |
| 75 |
}; |