Debian Codegraph @

extensions/DiscussionTools/modules/permalinks.js

raw symbols SCIP
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 // Only handle unmodified left clicks
7 return;
8 }
9 // Try to percent-decode the URL, so that non-Latin characters don't look so ugly (T357021)
10 // Use currentTarget rather than target to avoid conflicts with userscripts that do their
11 // own timestamp-wrapping. (T368701)
12 let link = e.currentTarget.href;
13 try {
14 // decodeURI() may throw
15 const decodedLink = decodeURI( link );
16 // Check that the decoded URL is parsed to the same canonical URL
17 // new URL() may throw
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 // Restore scroll position, can be changed by setSelectionRange, or hash navigation
52 function afterNextScroll() {
53 // On desktop we can restore scroll immediately after the scroll
54 // event, preventing a scroll flicker.
55 $win.scrollTop( scrollTop );
56 // On mobile, we need to wait another execution cycle (setTimeout)
57 // before the scroll is rendered (and not requestAnimationFrame).
58 setTimeout( () => {
59 $win.scrollTop( scrollTop );
60 } );
61 }
62 // Restore scroll position when the scroll event fires.
63 // setTimeout does't reliably wait long enough for the native
64 // scroll to happen.
65 $win.one( 'scroll', afterNextScroll );
66 // If we happened to be in the exact correct position, 'scroll' won't fire,
67 // so clear the listener after a short delay
68 setTimeout( () => {
69 $win.off( 'scroll', afterNextScroll );
70 }, 1000 );
71 }
72
73 module.exports = {
74 init: init
75 };