|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// based on https://github.com/Aly-ve/Mastodon-share-button |
| 5 | + |
| 6 | +const COOKIE_NAME = 'mastodon-instance-address'; |
| 7 | +const URL_REGEX = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([\/\w .-]*)*\/?$/; |
| 8 | + |
| 9 | +const msbConfig = { |
| 10 | + openModal() { |
| 11 | + jQuery( '#mastodonShareModal' ).modal( 'show' ); |
| 12 | + }, |
| 13 | + closeModal() { |
| 14 | + jQuery( '#mastodonShareModal' ).modal( 'hide' ); |
| 15 | + }, |
| 16 | + addressFieldSelector: '#msb-address', |
| 17 | + buttonModalSelector: '#msb-share', |
| 18 | + memorizeFieldId: 'msb-memorize-instance', |
| 19 | +}; |
| 20 | + |
| 21 | +/* Mastodon Share Modal */ |
| 22 | +jQuery( '#mastodonShareModal' ).on( 'shown.bs.modal', function () { |
| 23 | + jQuery( '#msb-address' ).trigger( 'focus' ); |
| 24 | +} ); |
| 25 | + |
| 26 | +function msbShareButtonAction( name, target ) { |
| 27 | + let msbInstanceAddress = ''; |
| 28 | + |
| 29 | + msbInstanceAddress = msbGetCookie( 'mastodon-instance-address' ); |
| 30 | + if ( msbInstanceAddress.length > 0 ) { |
| 31 | + window.open( |
| 32 | + `${ msbInstanceAddress }/share?text=${ name }%20${ target }`, |
| 33 | + `__blank` |
| 34 | + ); |
| 35 | + } else if ( |
| 36 | + msbConfig && |
| 37 | + msbConfig.openModal && |
| 38 | + msbConfig.addressFieldSelector |
| 39 | + ) { |
| 40 | + if ( document.querySelector( msbConfig.buttonModalSelector ) ) { |
| 41 | + const bms = document.querySelector( msbConfig.buttonModalSelector ); |
| 42 | + bms.data = { target, name }; |
| 43 | + bms.addEventListener( 'click', () => msbOnShare(), false ); |
| 44 | + } |
| 45 | + msbConfig.openModal( name, target ); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function msbOnShare( _name, _target ) { |
| 50 | + if ( |
| 51 | + msbConfig && |
| 52 | + msbConfig.addressFieldSelector && |
| 53 | + msbConfig.buttonModalSelector |
| 54 | + ) { |
| 55 | + const name = !! _name |
| 56 | + ? _name |
| 57 | + : document.querySelector( msbConfig.buttonModalSelector ).data.name; |
| 58 | + const target = !! _target |
| 59 | + ? _target |
| 60 | + : document.querySelector( msbConfig.buttonModalSelector ).data |
| 61 | + .target; |
| 62 | + let msbInstanceAddress = document.querySelector( |
| 63 | + `${ msbConfig.addressFieldSelector }` |
| 64 | + ).value; |
| 65 | + |
| 66 | + if ( ! msbInstanceAddress.startsWith( 'http' ) ) { |
| 67 | + msbInstanceAddress = 'https://' + msbInstanceAddress; |
| 68 | + } |
| 69 | + if ( msbInstanceAddress.match( URL_REGEX ) ) { |
| 70 | + if ( msbConfig.memorizeFieldId ) { |
| 71 | + const msbMemorizeIsChecked = document.querySelector( |
| 72 | + `#${ msbConfig.memorizeFieldId }` |
| 73 | + ).checked; |
| 74 | + if ( |
| 75 | + msbConfig.memorizeFieldId && |
| 76 | + ! msbGetCookie( COOKIE_NAME ).length > 0 && |
| 77 | + msbMemorizeIsChecked |
| 78 | + ) { |
| 79 | + msbSetCookie( COOKIE_NAME, msbInstanceAddress, 7 ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + window.open( |
| 84 | + `${ msbInstanceAddress }/share?text=${ name }%20${ target }`, |
| 85 | + `__blank` |
| 86 | + ); |
| 87 | + if ( msbConfig && msbConfig.openModal && msbConfig.closeModal ) { |
| 88 | + msbConfig.closeModal(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function msbGetCookie( cname ) { |
| 95 | + const name = cname + '='; |
| 96 | + const ca = document.cookie.split( ';' ); |
| 97 | + for ( let i = 0; i < ca.length; i++ ) { |
| 98 | + let c = ca[ i ]; |
| 99 | + while ( c.charAt( 0 ) === ' ' ) { |
| 100 | + c = c.substring( 1 ); |
| 101 | + } |
| 102 | + if ( c.indexOf( name ) === 0 ) { |
| 103 | + return c.substring( name.length, c.length ); |
| 104 | + } |
| 105 | + } |
| 106 | + return ''; |
| 107 | +} |
| 108 | + |
| 109 | +function msbSetCookie( name, value, days ) { |
| 110 | + const d = new Date(); |
| 111 | + d.setTime( d.getTime() + days * 86400000 ); |
| 112 | + const expires = 'expires=' + d.toUTCString(); |
| 113 | + document.cookie = `${ name }=${ value }; ${ expires }; path=/`; |
| 114 | +} |
| 115 | + |
| 116 | +( function () { |
| 117 | + const msbButtons = document.querySelectorAll( '.mastodon-share-button' ); |
| 118 | + |
| 119 | + for ( let i = 0; i < msbButtons.length; i++ ) { |
| 120 | + ( function ( j ) { |
| 121 | + const msbTarget = msbButtons[ j ].dataset.target; |
| 122 | + let msbName = msbButtons[ j ].dataset.name; |
| 123 | + |
| 124 | + // Replace hashtab by html code |
| 125 | + msbName = msbName.replace( /#/g, '%23' ); |
| 126 | + |
| 127 | + /** |
| 128 | + * Set the listener in each button |
| 129 | + */ |
| 130 | + msbButtons[ j ].addEventListener( |
| 131 | + 'click', |
| 132 | + () => { |
| 133 | + msbShareButtonAction( msbName, msbTarget ); |
| 134 | + }, |
| 135 | + true |
| 136 | + ); |
| 137 | + } )( i ); |
| 138 | + } |
| 139 | +} )(); |
| 140 | +/* eslint-enable no-undef */ |
0 commit comments